我应该如何使用poi将word文档中的字符串替换为另一个字符串?
我是 poi 新手。我需要使用 poi 开发和编辑 Word 文档。请发送上述问题的任何示例。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我是 poi 新手。我需要使用 poi 开发和编辑 Word 文档。请发送上述问题的任何示例。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
XWPF:请参阅我关于 POI 的博客文章的第 1 部分和第 2 部分,此处:http://tkgospodinov.com/writing-microsoft-word-documents-in-java-with-apache-poi/。您可以使用 XWPFDocument#setParagraph 用 POI 替换整个段落,这样您就可以获取要替换文本的段落,并使用结果创建一个新段落并将其设置在原始段落的位置。请记住,这是自 3.7-beta2 以来的新 API。我知道这有点复杂,但目前这是实现这一目标的唯一方法。
XWPF: See part 1 and 2 of my blog post on POI, here: http://tkgospodinov.com/writing-microsoft-word-documents-in-java-with-apache-poi/. You can use XWPFDocument#setParagraph to replace a whole paragraph with POI, so you can get the paragraph where you want to replace text, and create a new paragraph with the result and set it in place of the original. Keep in mind that this is new API since 3.7-beta2. I know it's a little convoluted, but currently that is the only way to accomplish that.