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表格行或列

  • excel2010如何选择图表尖形蓝色图
  • Word中插入英文日期和时间
  • excel2016中如何使功能区的间隔变大
  • EXCEL中如何进行横向排序?
  • 教你监视Excel2003的窗口,实现动态监控
  • 热门搜索
    旅游船 北京旅游景点介绍 凉拌土豆丝的家常做法 报到证怎么用 怎么算婚姻 银杏果怎么去皮 德胜门中医院怎么样 雨滴桌面怎么用 虚拟键盘怎么打开 中国封建社会的基本特点