C# 将 Excel 表格导入 Word

2025-11-10 04:45:57

1、下载并安装Spire.Office for .NET, 下载地址:http://www.e-iceblue.cn/Introduce/Spire-Office-NET.html

2、引用Spire.Xls.dll 和 Spire.Doc.dll文件到项目

3、将代码放到Visual Studio中

【C#】

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Spire.Doc;

using Spire.Xls;

using Spire.Doc.Fields;

using Spire.Doc.Documents;

namespace excel_to_word

{

    class Program

    {

        static void Main(string[] args)

        {

         //创建Workbook对象

         Workbook workbook = new Workbook();

         //加载Excel文档

         workbook.LoadFromFile(@"Sample.xlsx");

         //获取第一个工作表

         Worksheet sheet = workbook.Worksheets[0];

         //创建Document对象,即Word文档

         Document doc = new Document();

         //在Word中添加表格

         Table table = doc.AddSection().AddTable(true);

         //根据Excel表格数据所占的行数和列数设置表格的行和列

         table.ResetCells(sheet.LastRow, sheet.LastColumn);

         //遍历Excel表格的行

          for (int r = 1; r <= sheet.LastRow; r++)

          {

           //遍历Excel表格的列

            for (int c = 1; c <= sheet.LastColumn; c++)

            {

            //获取Excel表格的单元格

            CellRange xCell = sheet.Range[r, c];

            //获取Word表格的单元格

            TableCell wCell = table.Rows[r - 1].Cells[c - 1];

            //将Excel单元格数据填充到对应的Word单元格

            TextRange textRange = wCell.AddParagraph().AppendText(xCell.NumberText);

            //复制单元格格式

            CopyStyle(textRange, xCell, wCell);

        }

    }

    //保存文档

    doc.SaveToFile("result.docx", Spire.Doc.FileFormat.Docx);

}

        private static void CopyStyle(TextRange wTextRange, CellRange xCell, TableCell wCell)

        {

            //复制字体样式

            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;

            //复制单元格背景色

            wCell.CellFormat.BackColor = xCell.Style.Color;

            //复制文字对齐方式

            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;

            }

        }

   }

4、调试并运行代码后,生成word文档,前后效果对比图如下:

C# 将 Excel 表格导入 Word

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢