使用 Apache POI 替换 WinWord 文档中的文本

发布于 2024-09-12 08:44:28 字数 1163 浏览 3 评论 0原文

我需要替换 WinWord 文档中的某些文本。问题是,我使用replaceText函数在Range上进行的任何文本替换都会创建一个损坏的WinWord文件,除非替换字符串和替换字符串的长度完全相同。我们将处理动态内容,所以这是行不通的。

范围对象规格: http:// poi.apache.org/apidocs/org/apache/poi/hwpf/usermodel/Range.html#replaceText(java.lang.String, java.lang.String)

ReplaceText 函数有一个可选的第三个参数, int,指定某种偏移量。我想这可能是解决方案,但参数甚至无法处理负值,这使得很难或不可能进行替换,除非偏移量 (replacement.length() -然而,我可能需要它是负数。无论如何,如果其他两个参数长度不相等,文档中似乎没有任何内容暗示需要此偏移参数

。 (假设 a.doc 仅包含“caaaaaaake”)

      String inputFilename = "C:\\\a.doc"; 

      String outputFilename = "C:\\b.doc";
      POIFSFileSystem fs = null;
      FileInputStream fis = new FileInputStream(inputFilename);
      fs = new POIFSFileSystem(fis);

      HWPFDocument doc = new HWPFDocument(fs);

      Range range = doc.getRange();
      range.replaceText("caaaaaaake", "piiiie");


      FileOutputStream fos = new FileOutputStream(outputFilename);
      doc.write(fos);

      fis.close();
      fos.close();

代码执行没有问题,但它创建了一个损坏的 word 文件。 我能做些什么?

I need to replace certain text in a WinWord document. The problem is, any text replacement I do on a Range with the replaceText function creates a broken WinWord file unless the replacement and replaced strings are both the exact same length. We will be dealing with dynamic content, so this will not do.

Range object specs:
http://poi.apache.org/apidocs/org/apache/poi/hwpf/usermodel/Range.html#replaceText(java.lang.String, java.lang.String)

The replaceText function has an optional third parameter, an int, to specify some sort of offset. I thought maybe this could be the solution, but the param can't even handle a negative value, which makes it hard or impossible to do a replacement unless the offset (replacement.length() - replaced.length()) is positive. However, I may need it to be negative. Anyhow, nothing in the docs would seem to imply that this offset param is needed if the two other params aren't equal length.

Here's my code:
(let's say a.doc just contains "caaaaaaake")

      String inputFilename = "C:\\\a.doc"; 

      String outputFilename = "C:\\b.doc";
      POIFSFileSystem fs = null;
      FileInputStream fis = new FileInputStream(inputFilename);
      fs = new POIFSFileSystem(fis);

      HWPFDocument doc = new HWPFDocument(fs);

      Range range = doc.getRange();
      range.replaceText("caaaaaaake", "piiiie");


      FileOutputStream fos = new FileOutputStream(outputFilename);
      doc.write(fos);

      fis.close();
      fos.close();

The code executes no problem, but it creates a broken word file.
What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-09-19 08:44:28

我相信当你到了 poi 3.8 时,这个问题就得到解决了。当使用 beta5 时,这段代码对我有用。生成的word文件可以在你的caaaaaaake所在的文件中用piiiie打开

I believe that when you get to poi 3.8, this problem is addressed. When working with beta5, this code works for me. The word file generated can be opened just fine with piiiie in the file where your caaaaaaake used to be

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文