在java中生成docx
我有一个 docx 模板,我将其另存为 .xml,然后解析内容。 然后我生成一个新的更新的 Word 文档。 word文档生成后打不开。它说“文档已损坏”。我按确定。然后它会显示“如果您想检索该文档,请按确定”。我按确定。然后我得到更新的文档。这种情况每次都会发生。我创建了与独立java应用程序相同的程序。通过独立 Java 应用程序生成的文档打开时不会出现任何错误。有人能给我提供对此的见解吗?我也在服务器端使用了相同的代码。
这是我用来生成文档的代码。
try {
// Prepare the DOM document for writing
Source source = new DOMSource(doc);
// Prepare the output file
FileOutputStream file = new FileOutputStream(filename);
Result result = new StreamResult(file);
// Write the DOM document to the file
Transformer xformer = TransformerFactory.newInstance()
.newTransformer();
xformer.transform(source, result);
file.close();
} catch (TransformerConfigurationException e) {
System.out.println("Transformation Configuration Excepiton in WriteXMLFile");
} catch (TransformerException e) {
System.out.println("Transformation Excepiton in WriteXMLFile");
} catch (Exception e) {
System.out.println("Transformation Excepiton in WriteXMLFile");
e.printStackTrace();
}
I have a docx template that I am saving as .xml and then parsing the content.
Then I am generating a new updated word document. After the word document is generated I am unable to open it. It says " document corrupt ". I press ok. Then it says " Press OK if Do you want to retrieve the document ". I press ok. Then I get the updated document. This happens everytime. I have created the same program as stand alone java application. The document generated through the stand alone Java application opens without any errors. Could anyone provide me an insight into this ? I have used the same code for the server side also.
Here is the code that I use to generate the docuemnt.
try {
// Prepare the DOM document for writing
Source source = new DOMSource(doc);
// Prepare the output file
FileOutputStream file = new FileOutputStream(filename);
Result result = new StreamResult(file);
// Write the DOM document to the file
Transformer xformer = TransformerFactory.newInstance()
.newTransformer();
xformer.transform(source, result);
file.close();
} catch (TransformerConfigurationException e) {
System.out.println("Transformation Configuration Excepiton in WriteXMLFile");
} catch (TransformerException e) {
System.out.println("Transformation Excepiton in WriteXMLFile");
} catch (Exception e) {
System.out.println("Transformation Excepiton in WriteXMLFile");
e.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用 POI 库生成 Word 文档(.doc,不是 .docx,但它也应该可以工作)。
通过 POI,您可以:
- 打开你的word文档
- 使用干净的 API 编辑您想要的任何内容(不会在 XML 中混乱)
- 写入结果
http://poi.apache.org/
I use POI library to generate Word documents (.doc, not .docx but it should work too).
With POI you can :
- open your word document
- edit whatever you want with a clean API (not mess up in XML)
- write the result
http://poi.apache.org/
您可以使用 POI 或 docx4j 确保创建有效的 Word 文档。
You can use POI or docx4j to ensure you create valid Word documents.
你检查过JVM的编码吗?我遇到了这个问题,最后我发现在 Eclipse 中我有 UTF-8,但在独立中我没有指定编码,所以 JVM 采用 ISO-8559。
请使用参数
-Dfile.encoding=UTF-8
进行检查。Had you check the encoding of JVM?. I had have that problem, and finally I discovered that in Eclipse I had UTF-8, but in standalone I didn't specify encoding, so JVM take ISO-8559.
Please, check it with parameter
-Dfile.encoding=UTF-8
.我广泛使用了 Apache POI 和 docx4j,并且说过 docx4j 更强大,因为它不仅为文档本身而且为表格和图像提供了更多开箱即用的支持。 docx4j 所做的很多事情都是自动化的,而在 Apache 的 POI 领域,您必须进行大量手动编码才能支持 docx。不幸的是,POI 并没有为 docx 支持做太多工作。我建议使用 docx4j,因为它们原生支持打开和保存开箱即用的新 .docx 文件。
I have used both Apache POI and docx4j extensively, and having said that docx4j is more robust as it offers more support out of the box for not only the document itself but for tables and images. A lot of what docx4j does is automated, where areas Apache's POI you have to do a lot of manual coding for docx support. Unfortunately not much has been done for POI for docx support. I would suggest using docx4j as they have native support for opening and saving a new .docx file out of the box.