在Spire.Doc.Table的类中,它提供了Table.ApplyStyle()方法,使我们能够轻松地更改具有默认样式的表格布局。如下所示,这是一个 Word 文档,第一页包含两个普通表格。让我们看看如何用几行代码来格式化表格样式。
测试文件:

代码片段:
第 1 步:创建一个新的 Word 文档并加载测试文件。
Document doc = new Document("table.docx", FileFormat.Docx2010);
第 2 步:从文档中获取两个表。
Section section = doc.Sections[0];
Section section = doc.Sections[0];Table table1 = section.Tables[0] as Table;Table table2 = section.Tables[1] as Table;
第3 步:单独应用内置表格样式的表格。
table1.ApplyStyle(DefaultTableStyle.MediumShading1Accent2);table2.ApplyStyle(DefaultTableStyle.MediumShading2Accent1);
第 4 步:保存文件。
doc.SaveToFile("result.docx", FileFormat.Docx);
输出:

完整代码:
[C#]
using Spire.Doc;using Spire.Doc.Documents;namespace ApplyTableStyles{class Program{static void Main(string[] args){Document doc = new Document("table.docx", FileFormat.Docx2010);Section section = doc.Sections[0];Table table1 = section.Tables[0] as Table;Table table2 = section.Tables[1] as Table;table1.ApplyStyle(DefaultTableStyle.MediumShading1Accent2);table2.ApplyStyle(DefaultTableStyle.MediumShading2Accent1);doc.SaveToFile("result.docx", FileFormat.Docx);}}}
[VB.NET]
Imports Spire.DocImports Spire.Doc.DocumentsNamespace ApplyTableStylesClass ProgramPrivate Shared Sub Main(args As String())Dim doc As New Document("table.docx", FileFormat.Docx2010)Dim section As Section = doc.Sections(0)Dim table1 As Table = TryCast(section.Tables(0), Table)Dim table2 As Table = TryCast(section.Tables(1), Table)table1.ApplyStyle(DefaultTableStyle.MediumShading1Accent2)table2.ApplyStyle(DefaultTableStyle.MediumShading2Accent1)doc.SaveToFile("result.docx", FileFormat.Docx)End SubEnd ClassEnd Namespace
以上便是 如何 将内置表格样式应用于 C#、VB.NET 中的现有 Word 表格,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。
欢迎下载|体验更多E-iceblue产品
获取更多信息请咨询在线客服 ;
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!