Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Mar 26, 2019 4:21 pm

Hello,

I'm trying to insert Excel data into a word document using the code from the guide
" How to Export Excel Data to Word Table Maintaining Formatting in C# ", but it does not reproduce merged cells.


Thanks,

Albin

albinmonsimier@gmail.com
 
Posts: 1
Joined: Tue Mar 26, 2019 4:03 pm

Wed Mar 27, 2019 4:00 am

Hello,

Thanks for your post.
I have noticed the issue and submitted it to our Dev team for investigating and fixing. If there is any progress, we will let you know immediately. Sorry for the inconvenience caused.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Tue Apr 02, 2019 9:13 am

Hello,

Greetings from E-iceblue.
After further investigation, we found there isn't a direct way to maintain the merged cells when export Excel data to Word table. However, to achieve it, you can refer to the following sample code to define the functions by yourself. If there is any question, just feel free to write back.
Code: Select all
   static void Main(string[] args)
  {
      .......
       //judge if sheet has merged cells
       if (HasMergedCells(sheet) == true)
       {
           applyMerge(sheet, table);
       }
      ......
   }
   public static bool HasMergedCells(Worksheet sheet)
   {
       CellRange[] mergedCellRange = sheet.MergedCells;
       if (mergedCellRange == null)
       {
           return false;
       }
       else
           return true;
   }
   private static void applyMerge(Worksheet sheet, Table table)
   {
       CellRange[] mergedCellRange = sheet.MergedCells;
       for (int i = 0; i < mergedCellRange.Length; i++)
       {
           CellRange[] columns = mergedCellRange[i].Columns;
           CellRange[] rows = mergedCellRange[i].Rows;
           //has vertical merge
           if (columns.Length == 1)
           {
               int startRowIndex = mergedCellRange[i].Row - 1;
               int endRowIndex = startRowIndex + (mergedCellRange[i].RowCount) - 1;
               int columnIndex = mergedCellRange[i].Column - 1;
               //apply VerticalMerge for word table
               table.ApplyVerticalMerge(columnIndex, startRowIndex, endRowIndex);
           }
           //has horizontal merge
           if (rows.Length == 1)
           {
               int startCellIndex = mergedCellRange[i].Column - 1;
               int endCellIndex = startCellIndex + (mergedCellRange[i].ColumnCount) - 1;
               int rowIndex = mergedCellRange[i].Row - 1;
               //apply HorizontalMerge for word table
               table.ApplyHorizontalMerge(rowIndex, startCellIndex, endCellIndex);
           }
       }
   }

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc