本文将介绍如何使用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)