如何使用 Apache POI 3.7 API 更新 .docx
文件的页眉?
最佳答案
由于您的文档是.docx
格式,因此您需要使用POI 项目的XWPF 组件API。您可能会发现 org.apache.poi.xwpf.usermodel.XWPFHeader
类很有用 (Javadoc),但我自己从未使用过它。
我找不到使用 XWPF 执行此操作的良好引用,但以下说明描述了使用 HWPF 访问 header ,这是旧 Word 文档(又名 .doc
文档)的类似界面:
To get at the headers and footers of a Word document, first create a
org.apache.poi.hwpf.HWPFDocument
. Next, you need to create aorg.apache.poi.hwpf.usermodel.HeaderStores
, passing it yourHWPFDocument
. Finally, theHeaderStores
gives you access to the headers and footers, including first / even / odd page ones if defined in your document. Additionally,HeaderStores
provides a method for removing any macros in the text, which is helpful as many headers and footers do end up with macros in them.
page those instructions are from暗示在 HWPF 中头文件支持从来没有那么好,更不用说 XWPF 了。更多坏消息,this other Apache page听起来好像 XWPF 开发几乎停滞不前。您想要执行的操作可能已计划但尚未得到支持。
关于java - 使用 Apache POI 更新 .docx 文件的页眉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9430198/