学习C#调用OpenXml操作word文档的基本用法(37:学习复杂字段相关类)

  openxml中的FieldChar和FieldCode类是构建Word文档中复杂字段(如 PAGE、MERGEFIELD、= 公式等)的两个核心类,其中FieldChar类用于定义复杂字段的结构边界(开始、分隔、结束),而FieldCode类用于存储复杂字段的指令内容(例如PAGE、MERGEFIELD、UserName等)。只有将两者按正确顺序组合在一起,Word文档才能正确识别复杂字段并动态更新字段内容。

  FieldChar类用于构建复杂字段,其主要包含以下属性:
  1)FieldCharType属性:设置字段字符类型,从枚举类FieldCharValues内取值,包括begin(字段的开始标记)、separate(用于分隔字段的"指令代码"和"显示结果"部分)、end(字段的结束标记)等值,用于定义复杂字段的结构;
  2)FieldLock属性:设置是否重新计算字段内容,类型为OnOffValue,值为true时字段值将不再随文档变化而重新计算;
  3)FieldData属性:设置自定义字段数据,类型为FieldData,用于存储与字段关联的自定义数据。

  FieldCode类保存复杂字段结构中的指令代码,其必须与FieldChar类配合使用,否则其内容将被视为普通文本。其主要包含以下属性:
  1)Text属性:设置指令代码;
  2)Space属性:设置空格的处理方式,从枚举类SpaceProcessingModeValues内取值。

  常用的指令代码如下图所示(内容来自DeepSeek):

  下面的代码用于展示插入带格式的复杂字段及简单数学公式。

csharp 复制代码
// 插入页码
run.Append(new Break());
run.AppendChild(new Text("第"));

run.AppendChild(new FieldChar() { FieldCharType = FieldCharValues.Begin });
run.AppendChild(new FieldCode("PAGE"));
run.AppendChild(new FieldChar() { FieldCharType = FieldCharValues.Separate });
run.AppendChild(new Text(" "));
run.AppendChild(new FieldChar() { FieldCharType = FieldCharValues.End });

run.AppendChild(new Text("页 / 共"));

run.AppendChild(new FieldChar() { FieldCharType = FieldCharValues.Begin });
run.AppendChild(new FieldCode("NUMPAGES"));
run.AppendChild(new FieldChar() { FieldCharType = FieldCharValues.Separate });
run.AppendChild(new Text(" "));
run.AppendChild(new FieldChar() { FieldCharType = FieldCharValues.End });

run.AppendChild(new Text("页"));

// 插入数学公式
run.Append(new Break());
string mathExpre = "1 + 2 * 3 - 8";
run.AppendChild(new Text(mathExpre+" = "));

run.AppendChild(new FieldChar() { FieldCharType = FieldCharValues.Begin });
run.AppendChild(new FieldCode("= "+mathExpre) { Space = SpaceProcessingModeValues.Preserve });
run.AppendChild(new FieldChar() { FieldCharType = FieldCharValues.Separate });
run.AppendChild(new Text(" "));
run.AppendChild(new FieldChar() { FieldCharType = FieldCharValues.End });

  运行程序并保存为word文档后,打开word文档复杂字段的内容并不会自动更新,如下图所示,需要手工按F9逐个更新复杂字段的内容。

  可以在保存word文档时增加打开文档时更新字段的设置,代码如下所示,不过这样的话每次打开word文档都会有更新域的提醒,最终效果如下图所示:

csharp 复制代码
DocumentSettingsPart settingsPart = wordDocument.MainDocumentPart.DocumentSettingsPart;
if (settingsPart == null)
{
    settingsPart = wordDocument.MainDocumentPart.AddNewPart<DocumentSettingsPart>();
    settingsPart.Settings = new Settings();
}

if (settingsPart.Settings == null)
{
    settingsPart.Settings = new Settings();
}

settingsPart.Settings.AppendChild(new UpdateFieldsOnOpen());

参考文献

1https://github.com/dotnet/Open-XML-SDK

2https://learn.microsoft.com/zh-cn/office/open-xml/open-xml-sdk

3https://learn.microsoft.com/zh-cn/dotnet/api/documentformat.openxml.wordprocessing.style?view=openxml-3.0.1

4https://blog.csdn.net/i042416/article/details/126228816

相关推荐
andrsted3 小时前
练题簿在线免费刷题:将资料、Word 和 PDF导入小程序练习
学习·微信小程序·小程序·pdf·word·学习方法
ProcessOn官方账号4 小时前
ProcessOn Office 插件上线:在 Word / Excel / PPT 里直接画流程图
word·excel·插件·office·processon
2501_930707784 天前
使用C#代码在 Word 文档中添加、插入或删除页面
开发语言·c#·word
weixin_416660075 天前
AI 生成的 Mermaid 流程图和 LaTeX 公式,怎么一起导出到 Word?
人工智能·word·流程图·豆包
asdzx675 天前
使用 Python 按页或节拆分 Word 文档
python·word
死磕音视频5 天前
Microsoft Copilot 办公入门:在 Word、Excel、PowerPoint 里用 AI 完成日常文档工作
word·excel·copilot·办公效率
2501_930707785 天前
如何使用C#代码合并Word文档
word
芦芭荞6 天前
QT使用xml模版生成word文档
xml·qt·word
Am-Chestnuts6 天前
DeepSeek写教案怎么导出Word?完整步骤
ai·word
jun_bai7 天前
使用Aspose实现word文档合并,内容替换
windows·c#·word