更改字处理文档中的表文本
本主题演示如何使用 Open XML SDK for Office 以编程方式更改现有字处理文档中表中的文本。
打开现有文档
若要打开现有文档,请实例化 类, WordprocessingDocument 如以下 using
语句所示。 在同一语句中,使用 Open
方法在指定的 filepath
处打开字处理文件,并将布尔参数设置为 true
以启用编辑文档。
using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true))
在 v3.0.0+ 中, Close() 已删除 方法,转而依赖于 using 语句。
它确保在 Dispose() 到达右大括号时自动调用 方法。 using 语句后面的块为 using 语句中创建或指定的对象设定范围。
WordprocessingDocument由于 Open XML SDK 中的 类会自动保存并关闭对象作为其IDisposable实现的一using
部分,并且由于Dispose()在退出块时会自动调用,因此无需显式调用 Save() 或 Dispose() ,只要使用 语句。
表格的结构
文档的基本文档结构WordProcessingML
由 和 body
元素组成document
,后跟一个或多个块级元素(例如 p
表示段落)。 段落包含一个或多个 r
元素。
r
代表 run,它是具有一组通用属性(如格式设置)的文本区域。 运行包含一个或多个 t
元素。 元素 t
包含文本范围。
文档可能包含如本示例所示的表格。 是table
一组段落, (和其他块级内容) 排列在 和 columns
中rows
。 中的 WordprocessingML
表通过 tbl
元素定义,这类似于 HTML 表标记。 请考虑一个具有一个单元格的空表格(即表格具有一行和一列),并且所有边均为 1 磅的边框。 此表由以下 WordprocessingML
代码示例表示。
<w:tbl>
<w:tblPr>
<w:tblW w:w="5000" w:type="pct"/>
<w:tblBorders>
<w:top w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:left w:val="single" w:sz="4 w:space="0" w:color="auto"/>
<w:bottom w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:right w:val="single" w:sz="4" w:space="0" w:color="auto"/>
</w:tblBorders>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="10296"/>
</w:tblGrid>
<w:tr>
<w:tc>
<w:tcPr>
<w:tcW w:w="0" w:type="auto"/>
</w:tcPr>
<w:p/>
</w:tc>
</w:tr>
</w:tbl>
此表使用 元素指定 100% 页面宽度的表范围属性、使用 tblW
tblBorders
元素的一组表边框、使用 元素定义表中 tblGrid
的一组共享垂直边缘的表网格,以及使用 元素的 tr
单个表行。
示例代码的工作方式
在示例代码中,在 语句中 using
打开文档后,找到文档中的第一个表。 然后通过查找索引为 1 的行来查找表格中的第二行。 接下来,查找该行中索引为 2 的第三个单元格,如以下代码示例所示。
// Find the first table in the document.
Table table = doc.MainDocumentPart.Document.Body.Elements<Table>().First();
// Find the second row in the table.
TableRow row = table.Elements<TableRow>().ElementAt(1);
// Find the third cell in the row.
TableCell cell = row.Elements<TableCell>().ElementAt(2);
找到目标单元格后,查找该单元格的第一个段落中的第一段连续文本并使用传入的文本替换该文本。 以下代码示例演示这些操作。
// Find the first paragraph in the table cell.
Paragraph p = cell.Elements<Paragraph>().First();
// Find the first run in the paragraph.
Run r = p.Elements<Run>().First();
// Set the text for the run.
Text t = r.Elements<Text>().First();
t.Text = txt;
更改表中单元格内的文本
下面的代码示例演示如何更改字处理文档中的指定表格单元格内的文本。 代码示例要求文档(其文件名和路径作为参数 ChangeTextInCell
传递给 方法)包含一个表。
此代码示例还预期该表至少有两行三列,并且该表在位于第二行、第三列位置的单元格中包含文本。 在程序中调用 ChangeTextInCell
方法时,指定位置的单元格中的文本将替换为作为方法的第二个参数 ChangeTextInCell
传入的文本。
一些文字 | 一些文字 | 一些文字 |
---|---|---|
一些文字 | 一些文字 | 第二个参数中的文本 |
示例代码
方法 ChangeTextInCell
更改文件中第一个表的第二行和第三列中的文本。 您通过将文件的完整路径作为第一个参数传递并将要使用的文本作为第二个参数传递来调用该方法。 例如,对 方法的以下调用 ChangeTextInCell
会将指定单元格中的文本更改为“API 示例中的文本”。
ChangeTextInCell(args[0], args[1]);
以下是完整代码示例。
// Change the text in a table in a word processing document.
static void ChangeTextInCell(string filePath, string txt)
{
// Use the file name and path passed in as an argument to
// open an existing document.
using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true))
{
if (doc.MainDocumentPart is null || doc.MainDocumentPart.Document.Body is null)
{
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}
// Find the first table in the document.
Table table = doc.MainDocumentPart.Document.Body.Elements<Table>().First();
// Find the second row in the table.
TableRow row = table.Elements<TableRow>().ElementAt(1);
// Find the third cell in the row.
TableCell cell = row.Elements<TableCell>().ElementAt(2);
// Find the first paragraph in the table cell.
Paragraph p = cell.Elements<Paragraph>().First();
// Find the first run in the paragraph.
Run r = p.Elements<Run>().First();
// Set the text for the run.
Text t = r.Elements<Text>().First();
t.Text = txt;
}
}