本文将介绍如何使用Spire.Doc组件给Word表格设置格式。
表格样式设置
内置样式设置
C#
//载入文档
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);
VB.NET
'载入文档 Dim document As Document = New Document("Table.docx") '获取第一个节
Dim section As Section = document.Sections(0)
'获取第一个表格 Dim table As Table = CType(section.Tables(0),Table) '给表格应用内置样式
table.ApplyStyle(DefaultTableStyle.LightGridAccent3)
'保存文档 document.SaveToFile("BuiltinStyle.docx",FileFormat.Docx2013)
自定义段落样式设置
C#
//载入文档 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);
VB.NET
'载入文档
Dim document As Document = New Document("Table.docx")
'获取第一个节 Dim section As Section = document.Sections(0) '获取第一个表格
Dim table As Table = CType(section.Tables(0),Table)
'设置自定义样式 Dim style As ParagraphStyle = 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)
自适应方式设置
C#
//载入文档
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);
VB.NET
'载入文档 Dim document As Document = New Document("Table.docx") '获取第一个节
Dim section As Section = document.Sections(0)
'获取第一个表格 Dim table As Table = CType(section.Tables(0),Table) '自适应内容
table.AutoFit(AutoFitBehaviorType.AutoFitToContents)
'自适应窗口 'table.AutoFit(AutoFitBehaviorType.AutoFitToWindow);
'固定列宽 'table.AutoFit(AutoFitBehaviorType.FixedColumnWidths);
'保存文档 document.SaveToFile("AutofitMode.docx",FileFormat.Docx2013)
表格对齐方式设置
C#
//载入文档 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);
VB.NET
'载入文档
Dim document As Document = New Document("Tables.docx")
'获取第一个节 Dim section As Section = document.Sections(0) '获取第一、二、三个表格
Dim table1 As Table = CType(section.Tables(0),Table)
Dim table2 As Table = CType(section.Tables(1),Table)
Dim table3 As Table = CType(section.Tables(2),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)
边框设置
C#
//载入文档
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);
VB.NET
'载入文档 Dim document As Document = New Document("Table.docx") '获取第一个节
Dim section As Section = document.Sections(0)
'获取第一个表格 Dim table As Table = CType(section.Tables(0),Table) '设置表格的上边框
Dim table.TableFormat.Borders.Top.BorderType As F = BorderStyle.Double
table.TableFormat.Borders.Top.LineWidth = 1
table.TableFormat.Borders.Top.Color = Color.YellowGreen
'设置表格的左边框 table.TableFormat.Borders.Left.BorderType = BorderStyle.Double table.TableFormat.Borders.Left.LineWidth = 1 table.TableFormat.Borders.Left.Color = Color.YellowGreen '设置表格的右边框
table.TableFormat.Borders.Right.BorderType = BorderStyle.Double
table.TableFormat.Borders.Right.LineWidth = 1
table.TableFormat.Borders.Right.Color = Color.YellowGreen
'设置表格的下边框 table.TableFormat.Borders.Bottom.BorderType = BorderStyle.Double table.TableFormat.Borders.Bottom.LineWidth = 1 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)
背景颜色设置
行背景颜色设置
C#
//载入文档 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);
VB.NET
'载入文档
Dim document As Document = New Document("Table.docx")
'获取第一个节 Dim section As Section = document.Sections(0) '获取第一个表格
Dim table As Table = CType(section.Tables(0),Table)
'设置第一行的背景颜色 table.Rows(0).RowFormat.BackColor = Color.SeaGreen '保存文档
document.SaveToFile("RowBackColor.docx", FileFormat.Docx2013)
单元格背景颜色设置
C#
//载入文档
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);
VB.NET
'载入文档 Dim document As Document = New Document("Table.docx") '获取第一个节
Dim section As Section = document.Sections(0)
'获取第一个表格 Dim table As Table = CType(section.Tables(0),Table) '设置第一行第一个单元格的背景颜色
table(0, 0).CellFormat.BackColor = Color.SeaGreen
'保存文档 document.SaveToFile("CellBackColor.docx",FileFormat.Docx2013)
单元格段落对齐方式设置
水平对齐方式设置
C#
//载入文档 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);
VB.NET
'载入文档
Dim document As Document = New Document("Table1.docx")
'获取第一个节 Dim section As Section = document.Sections(0) '获取第一个表格
Dim table As Table = CType(section.Tables(0),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)
垂直对齐方式设置
C#
//载入文档
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);
VB.NET
'载入文档 Dim document As Document = New Document("Table1.docx") '获取第一个节
Dim section As Section = document.Sections(0)
'获取第一个表格 Dim table As Table = CType(section.Tables(0),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)