Word控件Spire.Doc 【Table】教程(8): 将内置表格样式应用于 C#、VB.NET 中的现有 Word 表格

本文重点介绍如何使用带有 C#、VB.NET 的 Spire.Doc 将内置表格样式应用于现有 Word 表格。

每次我们在Word文档中创建一个普通的表格时,我们可能都想改变一下表格的样式,使之更生动、更吸引人。在我们之前的文章中,我们已经演示了如何设置自定义样式的Word表格格式。但是,如果我们没有太多时间这样做,我们可以在几分钟内应用内置的表格样式来拥有更好的外观。本文重点介绍如何使用带有 C#、VB.NET 的 Spire.Doc 将内置表格样式应用于现有 Word 表格。

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

测试文件

用C#中的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#、VB.NET 中的现有 Word 表格

完整代码:

[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进行处理,非常感谢!

上一篇 2023年1月10日
下一篇 2023年1月10日

相关推荐

发表回复

登录后才能评论