本指南介绍如何使用Spire.Doc在 Word 中使用 C#/VB.NET 设置表格样式,欢迎查阅!
Microsoft Word 中的表格用于呈现有助于解释特定段落内容的数据信息。为了有更好的外观,人们可以设置Word表格样式。本指南介绍如何使用Spire.Doc在 Word 中使用 C#/VB.NET 设置表格样式。
同时下载 Spire.Doc(或Spire.Office)和 .NET Framework 2.0(或以上)。确保 Spire.Doc(或 Spire.Office)在系统上正确安装后,请按照以下步骤设置 Word 表格样式
在本例中,准备了一个带表格的 Word 文档。这是来自 Office.com 的学生成绩单模板。
第 1 步: 在 Visual Studio 中创建一个 C#/VB.NET 项目。添加 Spire.Doc.dll 作为参考。
[C#]
Document document = new Document();document.LoadFromFile(@"E:workDocumentsStudent Transcript.docx");
[VB.NET]
Dim document As New Document()document.LoadFromFile("E:workDocumentsStudent Transcript.docx")
第 2 步:设置表格样式
获取要设置样式的表
因为table1类型不同于document.Sections[0].Tables[1]类型,所以使用(Table)强制转换。
[C#]
Table table1 = (Table)document.Sections[0].Tables[1];
[VB.NET]
Dim table1 As Table = CType(document.Sections(0).Tables(1), Table)
设置表格行高。
[C#]
table1.Rows[0].Height = 25;
[VB.NET]
table1.Rows(0).Height = 25
设置表格样式
为了有区别。像以前一样将第一个单元格保留在第一行,并为第二个单元格设置样式。首先,为第二个单元格设置对齐方式和背景颜色。其次,声明一个段落样式,包括字体大小,颜色,并在单元格中应用这个样式。
[C#]
table1.Rows[0].Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Middle;table1.Rows[0].Cells[1].CellFormat.BackColor = Color.LimeGreen;ParagraphStyle style = new ParagraphStyle(document);style.Name = "TableStyle";style.CharacterFormat.FontSize = 14;style.CharacterFormat.TextColor = Color.GhostWhite;document.Styles.Add(style);table1.Rows[0].Cells[1].Paragraphs[0].ApplyStyle(style.Name);
[VB.NET]
table1.Rows(0).Cells(1).CellFormat.VerticalAlignment = VerticalAlignment.Middletable1.Rows(0).Cells(1).CellFormat.BackColor = Color.LimeGreenDim style As New ParagraphStyle(document)style.Name = "TableStyle"style.CharacterFormat.FontSize = 14style.CharacterFormat.TextColor = Color.GhostWhitedocument.Styles.Add(style)table1.Rows(0).Cells(1).Paragraphs(0).ApplyStyle(style.Name)
第 3 步:保存并启动
[C#]
document.SaveToFile("WordTable.docx", FileFormat.Docx);System.Diagnostics.Process.Start("WordTable.docx");
[VB.NET]
document.SaveToFile("WordTable.docx", FileFormat.Docx)System.Diagnostics.Process.Start("WordTable.docx")
有效截图:

以上便如何在C#、VB.NET中设置Word表格样式,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。
欢迎下载|体验更多E-iceblue产品
获取更多信息请咨询在线客服 ;
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!