tab 1
此 demo 展示如何在 word 文档中创建带数据的表格,以及如何设置表格的边框和背景颜色。
如果这不是您想要的 demo,您可以通过填写表格获取免费定制 demo。
如您有与我们产品相关的其他技术问题,请联系 该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。;销售相关的问题,请联系 该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。。
tab 2
using system;
using system.data;
using system.drawing;
using spire.doc;
using spire.doc.documents;
using spire.doc.formatting;
namespace demoonlinecode
{
class table
{
public void demotable(string docfile, datatable datatable,
string bordercolor = null,
string headerbackcolor = null,
string rowbackcolor = null,
string alternationrowcolor = null)
{
document document = new document(docfile, fileformat.auto);
addtable(document.sections[0], datatable,
convertstrtocolor(bordercolor),
convertstrtocolor(headerbackcolor),
convertstrtocolor(rowbackcolor),
convertstrtocolor(alternationrowcolor));
document.savetofile("demo.doc", fileformat.doc);
}
private void addtable(section section,
datatable datatable,
color bordercolor,
color headerbackcolor,
color rowbackcolor,
color alternationrowcolor)
{
spire.doc.table table = section.addtable();
int rowcount = datatable.rows.count;
int columncount = datatable.columns.count;
table.defaultrowheight = 25;
table.defaultcolumnwidth = 0;
table.resetcells(rowcount 1, columncount);
table.tableformat.borders.left.bordertype
= spire.doc.documents.borderstyle.hairline;
table.tableformat.borders.left.color = bordercolor;
table.tableformat.borders.top.bordertype
= spire.doc.documents.borderstyle.hairline;
table.tableformat.borders.top.color = bordercolor;
table.tableformat.borders.right.bordertype
= spire.doc.documents.borderstyle.hairline;
table.tableformat.borders.right.color = bordercolor;
table.tableformat.borders.bottom.bordertype
= spire.doc.documents.borderstyle.hairline;
table.tableformat.borders.bottom.color = bordercolor;
table.tableformat.borders.horizontal.bordertype
= spire.doc.documents.borderstyle.hairline;
table.tableformat.borders.horizontal.color = bordercolor;
table.tableformat.borders.vertical.bordertype
= spire.doc.documents.borderstyle.hairline;
table.tableformat.borders.vertical.color = bordercolor;
tablerow headerrow = table.rows[0];
headerrow.isheader = true;
for (int c = 0; c < columncount; c )
{
paragraph p = headerrow.cells[c].addparagraph();
p.format.horizontalalignment = horizontalalignment.center;
spire.doc.fields.textrange headertext = p.appendtext(datatable.columns[c].columnname);
headertext.characterformat.bold = true;
cellformat cellstyle = headerrow.cells[c].cellformat;
cellstyle.verticalalignment = verticalalignment.middle;
headerrow.cells[c].cellformat.backcolor = headerbackcolor;
}
for (int i = 0; i < rowcount; i )
{
object[] rowcontent = datatable.rows[i].itemarray;
datarow row = datatable.rows[i];
for (int j = 0; j < columncount; j )
{
paragraph p = table.rows[i 1].cells[j].addparagraph();
if (rowcontent[j] is byte[])
{
p.format.horizontalalignment = horizontalalignment.center;
p.appendpicture(rowcontent[j] as byte[]);
}
else
{
p.appendtext(rowcontent[j].tostring());
}
cellformat cellstyle = table.rows[i 1].cells[j].cellformat;
cellstyle.verticalalignment = verticalalignment.middle;
cellstyle.backcolor = rowbackcolor;
if (i % 2 == 1 && alternationrowcolor != color.empty)
{
cellstyle.backcolor = alternationrowcolor;
}
}
}
}
private color convertstrtocolor(string strcolor)
{
if (string.isnullorwhitespace(strcolor))
{
return color.empty;
}
else
{
return colortranslator.fromhtml("#" strcolor);
}
}
}
}
tab 3
imports system.data
imports system.drawing
imports spire.doc
imports spire.doc.documents
imports spire.doc.formatting
namespace demoonlinecode
class table
public sub demotable(docfile as [string],
datatable as datatable,
optional bordercolor as string = nothing,
optional headerbackcolor as string = nothing,
optional rowbackcolor as string = nothing,
optional alternationrowcolor as string = nothing)
dim document as new document(docfile, fileformat.auto)
addtable(document.sections(0),
datatable, convertstrtocolor(bordercolor),
convertstrtocolor(headerbackcolor),
convertstrtocolor(rowbackcolor),
convertstrtocolor(alternationrowcolor))
document.savetofile("demo.doc", fileformat.doc)
end sub
private sub addtable(section as section,
datatable as datatable,
bordercolor as color,
headerbackcolor as color,
rowbackcolor as color,
alternationrowcolor as color)
dim table as spire.doc.table = section.addtable()
dim rowcount as integer = datatable.rows.count
dim columncount as integer = datatable.columns.count
table.defaultrowheight = 25
table.defaultcolumnwidth = 0
table.resetcells(rowcount 1, columncount)
table.tableformat.borders.left.bordertype = spire.doc.documents.borderstyle.hairline
table.tableformat.borders.left.color = bordercolor
table.tableformat.borders.top.bordertype = spire.doc.documents.borderstyle.hairline
table.tableformat.borders.top.color = bordercolor
table.tableformat.borders.right.bordertype = spire.doc.documents.borderstyle.hairline
table.tableformat.borders.right.color = bordercolor
table.tableformat.borders.bottom.bordertype = spire.doc.documents.borderstyle.hairline
table.tableformat.borders.bottom.color = bordercolor
table.tableformat.borders.horizontal.bordertype = spire.doc.documents.borderstyle.hairline
table.tableformat.borders.horizontal.color = bordercolor
table.tableformat.borders.vertical.bordertype = spire.doc.documents.borderstyle.hairline
table.tableformat.borders.vertical.color = bordercolor
dim headerrow as tablerow = table.rows(0)
headerrow.isheader = true
for c as integer = 0 to columncount - 1
dim p as paragraph = headerrow.cells(c).addparagraph()
p.format.horizontalalignment = horizontalalignment.center
dim headertext as spire.doc.fields.textrange _
= p.appendtext(datatable.columns(c).columnname)
headertext.characterformat.bold = true
dim cellstyle as cellformat = headerrow.cells(c).cellformat
cellstyle.verticalalignment = verticalalignment.middle
headerrow.cells(c).cellformat.backcolor = headerbackcolor
next
for i as integer = 0 to rowcount - 1
dim rowcontent as object() = datatable.rows(i).itemarray
dim row as datarow = datatable.rows(i)
for j as integer = 0 to columncount - 1
dim p as paragraph = table.rows(i 1).cells(j).addparagraph()
if typeof rowcontent(j) is byte() then
p.format.horizontalalignment = horizontalalignment.center
p.appendpicture(trycast(rowcontent(j), byte()))
else
p.appendtext(rowcontent(j).tostring())
end if
dim cellstyle as cellformat = table.rows(i 1).cells(j).cellformat
cellstyle.verticalalignment = verticalalignment.middle
cellstyle.backcolor = rowbackcolor
if i mod 2 = 1 andalso alternationrowcolor <> color.empty then
cellstyle.backcolor = alternationrowcolor
end if
next
next
end sub
private function convertstrtocolor(strcolor as string) as color
if [string].isnullorwhitespace(strcolor) then
return color.empty
else
return colortranslator.fromhtml("#" & strcolor)
end if
end function
end class
end namespace