将 Excel 工作表导出到 Word,在 C# 中使用 Spire.Office 维护合并单元格

Exporting Excel worksheets to Word maintaining merged cells with Spire.Office in C#

作为我的文档过程自动化项目的一部分,我使用 Spire.Office 将包含多个工作表的 .xlsx 文档转换为 .doc 文档。

总的来说,自动复制粘贴效果很好,我修改了这段代码以跳过几个工作表: https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Document-Operation/How-to-Export-Excel-Data-to-Word-Table-Maintaining-Formatting-in-C.html

我要解决的问题是合并的单元格被忽略并被复制为单独的单元格。 这是代码:

class ToWord
    {
        public void CopyToWord(string fpath, int qnumber)
        {
            Workbook spirewb = new Workbook();
            spirewb.LoadFromFile(fpath);

            Document doc = new Document();
            for (int i = 1; i <= qnumber; i++)
            {
                Spire.Xls.Worksheet sheet = spirewb.Worksheets[i];

                Table table = doc.AddSection().AddTable(true);
                table.ResetCells(sheet.LastRow, sheet.LastColumn);

                for (int r = 1; r <= sheet.LastRow; r++)
                {
                    for (int c = 1; c <= sheet.LastColumn; c++)
                    {
                        CellRange xCell = sheet.Range[r, c];
                        TableCell wCell = table.Rows[r - 1].Cells[c - 1];
                        //fill data to word table
                        TextRange textRange = wCell.AddParagraph().AppendText(xCell.NumberText);
                        //copy font and cell style from excel to word
                        CopyStyle(textRange, xCell, wCell);
                    }
                }
            }
                
            doc.SaveToFile("result.doc", Spire.Doc.FileFormat.Doc);
            System.Diagnostics.Process.Start("result.doc");
        }

        private static void CopyStyle(TextRange wTextRange, CellRange xCell, TableCell wCell)
        {
            //copy font stlye
            wTextRange.CharacterFormat.TextColor = xCell.Style.Font.Color;
            wTextRange.CharacterFormat.FontSize = (float)xCell.Style.Font.Size;
            wTextRange.CharacterFormat.FontName = xCell.Style.Font.FontName;
            wTextRange.CharacterFormat.Bold = xCell.Style.Font.IsBold;
            wTextRange.CharacterFormat.Italic = xCell.Style.Font.IsItalic;
            //copy backcolor
            wCell.CellFormat.BackColor = xCell.Style.Color;
            //copy text alignment
            switch (xCell.HorizontalAlignment)
            {
                case HorizontalAlignType.Left:
                    wTextRange.OwnerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
                    break;
                case HorizontalAlignType.Center:
                    wTextRange.OwnerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center;
                    break;
                case HorizontalAlignType.Right:
                    wTextRange.OwnerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
                    break;
            }
        }
    }

工作解决方案已在此处发布: https://www.e-iceblue.com/forum/post37260.html