学习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

相关推荐
chatexcel9 小时前
ChatExcel AI Word 升级:多模态读资料、按模板生成、智能排版配图,还能批注修订和联动Excel 复核
人工智能·ai·word·excel·ai写作
Metaphor6921 天前
使用 Python 在 Word 文档中添加或删除文本框
python·word
Sunflower023 天前
如何解决word/WPS word 中表格跳到下一页(上一页有大篇幅空白)
word·wps
AI导出鸭4 天前
如何让deepseek生成word文档 ?「AI 导出鸭」苹果版:从API流式解析到Pages级渲染,硬核攻克公式裂变与表格回流的终极方案。
人工智能·chatgpt·word·cocoa·ai导出鸭
开开心心就好4 天前
Word双击预览图片插件弥补Word功能缺失
人工智能·python·智能手机·ocr·电脑·word·音视频
一棵星5 天前
内网 MySQL 表结构一键导出 Word 文档:直连 + Agent 双模式实战
数据库·mysql·word
bug嘛我经常写5 天前
如何批量删除word文档中存在的无用样式
经验分享·python·word
Am-Chestnuts6 天前
Kimi长回答批量导出Word:DS随心转实践
word
曹牧6 天前
Word:更改列表级别
word
wtsolutions7 天前
Sheet-to-Doc Skill: Let AI Assistants Generate Word Documents for You
人工智能·word