C#如何将EXCEL文件中的表格复制到word中

如题所述

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Formatting;
using Spire.Xls;
using Spire.Xls.Collections;
using Spire.Xls.Core;

namespace Xls2Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //initialize a workbook and load sample excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\..\DatatableSample.xlsx");
            Worksheet sheet = workbook.Worksheets[0];
            //initialize a word document
            Document doc = new Document();
            //add a table
            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);
                }
            }
            //set column width of word table
            for (int i = 0; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < table.Rows[i].Cells.Count; j++)
                {
                    table.Rows[i].Cells[j].Width = 60f;
                }
            }
            doc.SaveToFile("result.docx", Spire.Doc.FileFormat.Docx);
            System.Diagnostics.Process.Start("result.docx");
        }
        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;
            }
        }
    }
}

出自:C#中如何将Excel表格导入Word

温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜