文档星空最火知名网站>>Spire.Doc系列教程>>Word .NET库组件Spire.Doc系列教程(28):设置 Word 表格的格式
Word .NET库组件Spire.Doc系列教程(28):设置 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);
*自定义段落样式设置
//载入文档
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);
*自适应方式设置
//载入文档
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);
表格对齐方式设置
//载入文档
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);
边框设置
//载入文档
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);
背景颜色设置
*行背景颜色设置
//载入文档
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);
*单元格背景颜色设置
//载入文档
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);
单元格段落对齐方式设置
*水平对齐方式设置
//载入文档
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);
*垂直对齐方式设置
//载入文档
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);
*购买Spire.Doc正版授权的朋友可以点击哦~~


QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线