在word中,文本框可以包含很多种元素,例如文本、图片和表格等。本文主要介绍如何使用spire.doc组件插入表格到word文本框,以及获取和删除word文本框中的表格。
插入表格
c#
//创建document实例
document document = new document();
//添加节
section section = document.addsection();
//添加段落
paragraph paragraph = section.addparagraph();
//添加文本框到段落,并指定文本框的宽度和高度
textbox textbox = paragraph.appendtextbox(300, 100);
//添加文本到文本框
paragraph textboxparagraph = textbox.body.addparagraph();
textrange textboxrange = textboxparagraph.appendtext("table 1");
textboxrange.characterformat.fontname = "arial";
//插入表格到文本框
table table = textbox.body.addtable(true);
//指定表格的行数和列数
table.resetcells(4, 4);
string[,] data = new string[,]
{
{"姓名","年龄","性别","工号" },
{"张三","28","男","0023" },
{"李四","30","男","0024" },
{"王五","26","女","0025" }
};
//将数组内容填充到表格
for (int i = 0; i < 4; i )
{
for (int j = 0; j < 4; j )
{
textrange tablerange = table[i, j].addparagraph().appendtext(data[i, j]);
tablerange.characterformat.fontname = "arial";
}
}
//给表格应用样式
table.applystyle(defaulttablestyle.lightgridaccent3);
//保存文档
document.savetofile("addtable.docx", fileformat.docx2013);
vb.net
'创建document实例
dim document as new document()
'添加节
dim section as section = document.addsection()
'添加段落
dim paragraph as paragraph = section.addparagraph()
'添加文本框到段落,并指定文本框的宽度和高度
dim textbox as textbox = paragraph.appendtextbox(300, 100)
'添加文本到文本框
dim textboxparagraph as paragraph = textbox.body.addparagraph()
dim textboxrange as textrange = textboxparagraph.appendtext("table 1")
textboxrange.characterformat.fontname = "arial"
'插入表格到文本框
dim table as table = textbox.body.addtable(true)
'指定表格的行数和列数
table.resetcells(4, 4)
dim data as string(,) = new string(,) {
{"姓名", "年龄", "性别", "工号"},
{"张三", "28", "男", "0023"},
{"李四", "30", "男", "0024"},
{"王五", "26", "女", "0025"}
}
'将数组内容填充到表格
for i as integer = 0 to 3
for j as integer = 0 to 3
dim tablerange as textrange = table(i, j).addparagraph().appendtext(data(i, j))
tablerange.characterformat.fontname = "arial"
next
next
'给表格应用样式
table.applystyle(defaulttablestyle.lightgridaccent3)
'保存文档
document.savetofile("addtable.docx", fileformat.docx2013)
获取表格
c#
//载入word文档
document document = new document("addtable.docx");
//获取第一个文本框
textbox textbox = document.textboxes[0];
//获取文本框中第一个表格
table table = textbox.body.tables[0] as table;
stringbuilder sb = new stringbuilder();
//遍历表格中的段落并提取文本
foreach (tablerow row in table.rows)
{
foreach (tablecell cell in row.cells)
{
foreach (paragraph paragraph in cell.paragraphs)
{
sb.appendline(paragraph.text);
}
}
}
file.writealltext("text.txt", sb.tostring());
vb.net
'载入word文档
dim document as new document("addtable.docx")
'获取第一个文本框
dim textbox as textbox = document.textboxes(0)
'获取文本框中第一个表格
dim table as table = trycast(textbox.body.tables(0), table)
dim sb as new stringbuilder()
'遍历表格中的段落并提取文本
for each row as tablerow in table.rows
for each cell as tablecell in row.cells
for each paragraph as paragraph in cell.paragraphs
sb.appendline(paragraph.text)
next
next
next
file.writealltext("text.txt", sb.tostring())
删除表格
c#
//创建document实例
document document = new document("addtable.docx");
//获取第一个文本框
textbox textbox = document.textboxes[0];
//删除文本框中第一个表格
textbox.body.tables.removeat(0);
//保存文档
document.savetofile("removetable.docx", fileformat.docx2013);
vb.net
'创建document实例
dim document as new document("addtable.docx")
'获取第一个文本框
dim textbox as textbox = document.textboxes(0)
'删除文本框中第一个表格
textbox.body.tables.removeat(0)
'保存文档
document.savetofile("removetable.docx", fileformat.docx2013)