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

相关推荐
Am-Chestnuts1 小时前
豆包、DeepSeek公式怎么导出Word?用DS随心转保留可编辑格式
word
古少侠1 小时前
DeepSeek长回答怎么保存成Word?避免逐段复制和漏内容
c#·word
laoban-yu21 小时前
Word与PDF文档互转
pdf·word·文档转换
古少侠1 天前
用 DS随心转 把 AI 给的表格整理成排版正常、能改的 Word
word
2601_962295331 天前
Python自动化批量文档生成工具:Word与Excel的结合应用
python·自动化·word·excel·文档生成
古少侠3 天前
用 DS随心转 把 AI 画的 Mermaid 流程图转成 Word 里的高清图
开发语言·c#·word
开开心心_Every4 天前
实现鼠标键盘动作录制与重复播放的工具
微信·jupyter·pycharm·pdf·计算机外设·word·excel
ZnS_oscar4 天前
不用下载字体,获得仿宋字体
windows·word
SunnyDays10114 天前
如何使用 Python 从 Word 文档中提取图片
python·word
Felicia-侧听4 天前
PDF怎么转换成Word?3种方法实现PDF转可编辑Word
pdf·word·pdf转word