C# 添加Word表格行或列

时间:2026-02-21 13:38:54

1、方法1:通过 eiceblue官网获取dll文件包。下载后,解压文件。打开解压后的文件夹,安装程序。安装后,在程序中添加引用安装路径下bin文件夹中的Spire.Doc.dll文件。添加引用后如下图:

C# 添加Word表格行或列

2、方法2: 可通过Nuget下载。

1、using Spire.Doc;

namespace AddRow_Doc

{

    class Program

    {

        static void Main(string[] args)

        {

            //创建一个Document类对象,并加载Word文档

            Document doc = new Document();

            doc.LoadFromFile("test.docx");

            //获取第一个table

            Table table = doc.Sections[0].Tables[0] as Spire.Doc.Table;

            //在指定位置新插入一行作为第三行

            TableRow row = table.AddRow();

            table.Rows.Insert(2, row);

            //在表格末尾添加一行

            //table.AddRow(true, 4);//带格式添加

            table.AddRow(false, 4);//不带格式添加

            //保存文档

            doc.SaveToFile("Result.docx", FileFormat.Docx2013);

        }

    }

}

2、表格行添加效果:

C# 添加Word表格行或列

1、using Spire.Doc;

namespace AddColumn

{

    class Program

    {

        static void Main(string[] args)

        {

            //加载文档

            Document doc = new Document("Test.docx");

            //获取第一节

            Section section = doc.Sections[0];

            //获取第一个表格

            Table table = section.Tables[0] as Table;

           

            //添加一列到表格,设置单元格的宽度和宽度类型

            for (int i = 0; i < table.Rows.Count; i++)

            {

                TableCell cell = table.Rows[i].AddCell(true);//默认在表格最后添加一列                

                cell.Width = table[0, 0].Width;

                cell.CellWidthType = table[0, 0].CellWidthType;

            }            

            //保存文档

            doc.SaveToFile("AddColumn.docx", FileFormat.Docx2013);            

        }

    }

}

2、表格列添加效果:

C# 添加Word表格行或列

© 2026 指南经验网
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com