Word .NET库组件Spire.Doc系列教程(29):操作 Word 表格行和列

本系列教程将为大家带来Spire.Doc for .NET在使用过程中的各类实际操作,本篇文章介绍了如何设置 Word 表格的格式。

Spire.Doc for .NET是一个专业的Word .NET库,设计用于帮助开发人员高效地开发创建、阅读、编写、转换和打印任何来自.NET( C#, VB.NET, ASP.NET)平台的Word文档文件的功能。

本系列教程将为大家带来Spire.Doc for .NET在使用过程中的各类实际操作,本篇文章介绍了如何设置 Word 表格的格式。>>下载Spire.Doc最新试用版体验

在Word文档中,表格可以帮助我们清晰、直观的分析和整理数据。一个表格通常至少包含一行,每行至少包含一个单元格,一个单元格可以包含多种元素如文本和表格(即嵌套表格)等。


C# 操作 Word 表格行和列

添加和插入行

*添加和插入行

//载入文档Document document = new Document("Table.docx");//获取第一个节Section section = document.Sections[0];//获取第一个表格Table table = section.Tables[0] as Table;//添加一行到表格的最后table.AddRow(true, 4);//插入一行到表格的第三行table.Rows.Insert(2, table.AddRow());//保存文档document.SaveToFile("AddRow.docx", FileFormat.Docx2013);

Word .NET库组件Spire.Doc系列教程:操作 Word 表格行和列

*添加列

//载入文档Document document = new Document("Table.docx");//获取第一个节Section section = document.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;}//保存文档document.SaveToFile("AddColumn.docx", FileFormat.Docx2013);

Word .NET库组件Spire.Doc系列教程:操作 Word 表格行和列

设置行高和列宽

//载入文档Document document = new Document("Table.docx");//获取第一个表格Table table = document.Sections[0].Tables[0] as Table;//设置第一行的行高table.Rows[0].Height = 40;for (int i = 0; i < table.Rows.Count; i++){    //设置第二列的列宽    table.Rows[i].Cells[1].Width = 40;}//保存文档document.SaveToFile("SetRowHeightAndColumnWidth.docx", FileFormat.Docx2013);

Word .NET库组件Spire.Doc系列教程:操作 Word 表格行和列

删除行和列

//载入文档Document doc = new Document("Table.docx");//获取第一个表格Table table = doc.Sections[0].Tables[0] as Table;//删除第二行table.Rows.RemoveAt(1);//删除第二列for (int i = 0; i < table.Rows.Count; i++){    table.Rows[i].Cells.RemoveAt(1);}//保存文档doc.SaveToFile("RemoveRowAndColumn.docx", FileFormat.Docx2013);

Word .NET库组件Spire.Doc系列教程:操作 Word 表格行和列

*购买Spire.Doc正版授权的朋友可以点击“咨询在线客服”哦~~

Spire-850x100.png

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年7月12日
下一篇 2019年7月12日

相关推荐

发表回复

登录后才能评论