C# 将 Excel 表格导入 Word

 时间:2026-02-15 12:38:02

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

  • excel如何返回上半个月单笔最高销售金额
  • EXCEL中背景色的填充 渐变 点式 背景图片
  • INDIRECT函数怎么使用
  • EXCEL如何利用F9把多个单元格内容合并一起
  • 如何在Excel2019设置单元格字体类型
  • 热门搜索
    肉苁蓉是什么 describe是什么意思 作文运动会 狐狸养殖 wearing是什么意思 鸿运当头的养殖方法和注意事项 深圳有什么特产 增资扩股是什么意思 什么是招标 cdr是什么格式