我想获取word文档中表行的所有合并字段。 我使用的代码如下,但我没有得到所有合并字段 -
IEnumerable<Table> tables = docGenerated.MainDocumentPart.Document.Body.Descendants<Table>();
foreach (Table table in tables)
{
IEnumerable<TableRow> rows = table.Descendants<TableRow>();
foreach (TableRow row in rows)
{
IEnumerable<TableCell> cells = row.Descendants<TableCell>();
foreach (TableCell cell in cells)
{
foreach (FieldCode field in cell.Descendants<FieldCode>())
{
String fieldText = field.Text;
if (fieldText.StartsWith(" MERGEFIELD"))
{
Int32 endMerge = fieldText.IndexOf("\\");
Int32 fieldNameLength = fieldText.Length - endMerge;
String fieldName = fieldText.Substring(11, endMerge - 11);
fieldName = fieldName.Trim();
}
}
}
}
}
请建议我解决一下。