我们需要一个Java库来替换MS Word文件中的字符串。有人能提供建议吗?
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();
我不以此为豪,但它起作用了。
试试这个链接: http://www.dancrintea.ro/doc-to-pdf/
除了可以替换 MS Word 文件中的字符串外,还可以: - 通过简化的 API(如 getCell(x,y) 和 setCell(x,y,string))读写 Excel 文件 - 隐藏 Excel 工作表(例如辅助计算) - 替换 DOC、ODT 和 SXW 文件中的图像 - 转换为:
doc --> pdf、html、txt、rtf xls --> pdf、html、csv ppt --> pdf、swf
<tag>hello </tag><tag>world!</tag>
。只要你在输入整个标记单词时不改变光标位置,看起来似乎可以正常工作。 - Michael