Word .NET库组件Spire.Doc系列教程(28):设置 Word 表格的格式

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

更多资源查看:Spire.XLS系列教程 | Spire.Doc系列教程 | Spire.PDF系列教程

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.ApplyStyle(DefaultTableStyle.LightGridAccent3);//保存文档document.SaveToFile("BuiltinStyle.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;//设置自定义样式ParagraphStyle style = new ParagraphStyle(document);style.Name = "TableStyle";            style.CharacterFormat.FontSize = 14;style.CharacterFormat.TextColor = Color.SeaGreen;style.CharacterFormat.HighlightColor = Color.Yellow;//将自定义样式添加到文档document.Styles.Add(style);//给表格第一行第一个单元格的第一个段落应用自定义样式table[0,0].Paragraphs[0].ApplyStyle(style.Name);//保存文档document.SaveToFile("CustomStyle.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;//自适应内容table.AutoFit(AutoFitBehaviorType.AutoFitToContents);//自适应窗口//table.AutoFit(AutoFitBehaviorType.AutoFitToWindow);//固定列宽//table.AutoFit(AutoFitBehaviorType.FixedColumnWidths);//保存文档document.SaveToFile("AutofitMode.docx", FileFormat.Docx2013);

Word .NET库组件Spire.Doc系列教程:设置 Word 表格的格式


表格对齐方式设置

//载入文档Document document = new Document("Tables.docx");//获取第一个节Section section = document.Sections[0];//获取第一、二、三个表格Table table1 = section.Tables[0] as Table;Table table2 = section.Tables[1] as Table;Table table3 = section.Tables[2] as Table;//设置第一个表格居左table1.TableFormat.HorizontalAlignment = RowAlignment.Left;table1.TableFormat.LeftIndent = 34;//设置第二个表格居中table2.TableFormat.HorizontalAlignment = RowAlignment.Center;table2.TableFormat.LeftIndent = 34;//设置第三个表格居右table3.TableFormat.HorizontalAlignment = RowAlignment.Right;table3.TableFormat.LeftIndent = 34;//保存文档document.SaveToFile("TableAlignment.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;//设置表格的上边框table.TableFormat.Borders.Top.BorderType = BorderStyle.Double;table.TableFormat.Borders.Top.LineWidth = 1.0F;table.TableFormat.Borders.Top.Color = Color.YellowGreen;//设置表格的左边框table.TableFormat.Borders.Left.BorderType = BorderStyle.Double;table.TableFormat.Borders.Left.LineWidth = 1.0F;table.TableFormat.Borders.Left.Color = Color.YellowGreen;//设置表格的右边框table.TableFormat.Borders.Right.BorderType = BorderStyle.Double;table.TableFormat.Borders.Right.LineWidth = 1.0F;table.TableFormat.Borders.Right.Color = Color.YellowGreen;//设置表格的下边框table.TableFormat.Borders.Bottom.BorderType = BorderStyle.Double;table.TableFormat.Borders.Bottom.LineWidth = 1.0F;table.TableFormat.Borders.Bottom.Color = Color.YellowGreen;//设置表格的水平和垂直边框 table.TableFormat.Borders.Horizontal.BorderType = BorderStyle.Hairline;table.TableFormat.Borders.Horizontal.Color = Color.Orange;table.TableFormat.Borders.Vertical.BorderType = BorderStyle.Hairline;            table.TableFormat.Borders.Vertical.Color = Color.Orange;//保存文档document.SaveToFile("TableBorder.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;//设置第一行的背景颜色table.Rows[0].RowFormat.BackColor = Color.SeaGreen;//保存文档document.SaveToFile("RowBackColor.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;//设置第一行第一个单元格的背景颜色table[0,0].CellFormat.BackColor = Color.SeaGreen;//保存文档document.SaveToFile("CellBackColor.docx", FileFormat.Docx2013);

Word .NET库组件Spire.Doc系列教程:设置 Word 表格的格式


单元格段落对齐方式设置

*水平对齐方式设置

//载入文档Document document = new Document("Table1.docx");//获取第一个节Section section = document.Sections[0];//获取第一个表格Table table = section.Tables[0] as Table;//设置表格的第二行水平居左                        table[1, 0].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Left;table[1, 1].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Left;table[1, 2].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Left;//设置表格的第三行水平居中table[2, 0].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Center;table[2, 1].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Center;table[2, 2].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Center;//设置表格的第四行水平居右table[3, 0].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Right;table[3, 1].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Right;table[3, 2].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Right;//保存文档document.SaveToFile("HorizontalAlignment.docx", FileFormat.Docx2013);

Word .NET库组件Spire.Doc系列教程:设置 Word 表格的格式

*垂直对齐方式设置

//载入文档Document document = new Document("Table1.docx");//获取第一个节Section section = document.Sections[0];//获取第一个表格Table table = section.Tables[0] as Table;//设置表格第二行垂直居上table[1,0].CellFormat.VerticalAlignment = VerticalAlignment.Top;table[1,1].CellFormat.VerticalAlignment = VerticalAlignment.Top;table[1,2].CellFormat.VerticalAlignment = VerticalAlignment.Top;//设置表格第三行垂直居中table[2,0].CellFormat.VerticalAlignment = VerticalAlignment.Middle;table[2,1].CellFormat.VerticalAlignment = VerticalAlignment.Middle;table[2,2].CellFormat.VerticalAlignment = VerticalAlignment.Middle;//设置表格第四行垂直居下table[3,0].CellFormat.VerticalAlignment = VerticalAlignment.Bottom;table[3,1].CellFormat.VerticalAlignment = VerticalAlignment.Bottom;table[3,2].CellFormat.VerticalAlignment = VerticalAlignment.Bottom;//保存文档document.SaveToFile("VerticalAlignment.docx", FileFormat.Docx2013);

Word .NET库组件Spire.Doc系列教程:设置 Word 表格的格式

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

Spire-850x100.png

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

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

相关推荐

发表回复

登录后才能评论