使用Java:替换MS Word文件中的字符串 [英] With Java: replace string in MS Word file

查看:510
本文介绍了使用Java:替换MS Word文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要一个Java库来替换MS Word文件中的字符串。

We need a Java library to replace strings in MS Word files.

有人可以建议吗?

推荐答案

虽然在Apache POI中 MS Word支持,但它并不是很好。加载然后保存除最基本格式之外的任何文件可能会使布局变得混乱。你应该尝试一下,也许它适合你。

While there is MS Word support in Apache POI, it is not very good. Loading and then saving any file with other than the most basic formatting will likely garble the layout. You should try it out though, maybe it works for you.

还有很多商业图书馆,但我不知道它们中是否有任何一个更好。

There are a number of commercial libraries as well, but I don't know if any of them are any better.

我最近在处理类似要求时不得不满足的糟糕解决方案是使用 DOCX 格式,打开ZIP容器,读取文档XML,然后用正确的文本替换我的标记。这适用于替换没有段落等的简单文本位。

The crappy "solution" I had to settle for when working on a similar requirement recently was using the DOCX format, opening the ZIP container, reading the document XML, and then replacing my markers with the right texts. This does work for replacing simple bits of text without paragraphs etc.

private static final String WORD_TEMPLATE_PATH = "word/word_template.docx";
private static final String DOCUMENT_XML = "word/document.xml";

/*....*/

final Resource templateFile = new ClassPathResource(WORD_TEMPLATE_PATH);

final ZipInputStream zipIn = new ZipInputStream(templateFile.getInputStream());
final ZipOutputStream zipOut = new ZipOutputStream(output);

ZipEntry inEntry;
while ((inEntry = zipIn.getNextEntry()) != null) {
    final ZipEntry outEntry = new ZipEntry(inEntry.getName());
    zipOut.putNextEntry(outEntry);

    if (inEntry.getName().equals(DOCUMENT_XML)) {
        final String contentIn = IOUtils.toString(zipIn, UTF_8);
        final String outContent = this.processContent(new StringReader(contentIn));
        IOUtils.write(outContent, zipOut, UTF_8);
    } else {
        IOUtils.copy(zipIn, zipOut);
    }

    zipOut.closeEntry();
}

zipIn.close();
zipOut.finish();

我并不为此感到自豪,但它确实有效。

I'm not proud of it, but it works.

这篇关于使用Java:替换MS Word文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆