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

相关推荐
工具派9 小时前
Word文档太大怎么拆分?段落结构与两种拆法的边界实测
开发语言·c#·word
Metaphor6929 小时前
Python 实现 Word/TXT 互转:附完整代码
python·word·格式转换·txt
小羊没烦恼!10 小时前
【译】利用Asp.net MVC处理文件的上传下载
开发语言·缓存·c#·word·powerpoint·.net
小羊没烦恼!7 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
Am-Chestnuts7 天前
DeepSeek表格复制到Word变竖线错列?用DS随心转对比几种整理方法
人工智能·word·ds随心转
2501_9307077810 天前
使用C#代码为新创建的 Word 文档创建目录
c#·word
SunnyDays101110 天前
Java 拆分 Word 文档教程:按页、分页符和分节符拆分
java·word·分页·拆分
copilot中国版11 天前
Word Copilot|一键提取文档关键信息,告别逐字阅读
word·copilot
天人合一peng13 天前
西北工业大学校外访问系统
word
古少侠13 天前
deepseek转word工具怎么选?DS随心转与4种方案对比实测
人工智能·word·powerpoint