2024年1月2日发(作者:)

C# 插入表格到Word文本框以及获取和删除Word文本框中的表格

在Word中,文本框可以包含很多种元素,例如文本、图片和表格等。本文主要介绍如何使用组件插入表格到Word文本框,以及获取和删除Word文本框中的表格。

插入表格

//创建Document实例

Document document = newDocument();

//添加节

Section section = tion();

//添加段落

Paragraph paragraph = agraph();

//添加文本框到段落,并指定文本框的宽度和高度

TextBox textbox = TextBox(300, 100);

//添加文本到文本框

Paragraph textboxParagraph = agraph();

TextRange textboxRange = Text("Table 1");

me = "Arial";

//插入表格到文本框

Table table = le(true);

//指定表格的行数和列数

ells(4, 4);

string[,] data = newstring[,]

{

{"姓名","年龄","性别","工号" },

{"张三","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]);

me = "Arial";

}

}

//给表格应用样式

tyle(ridAccent3);

//保存文档

File("", 2013);

获取表格

//载入Word文档

Document document = newDocument("");

//获取第一个文本框

TextBox textbox = xes[0];

//获取文本框中第一个表格

Table table = [0] asTable;

StringBuilder sb = newStringBuilder();

//遍历表格中的段落并提取文本

foreach (TableRow row in )

{

foreach (TableCell cell in )

{

foreach (Paragraph paragraph in aphs)

{

Line();

}

}

}

llText("", ng());

删除表格

//创建Document实例

Document document = newDocument("");

//获取第一个文本框

TextBox textbox = xes[0];

//删除文本框中第一个表格

At(0);

//保存文档

File("", 2013);