开发者

Generating docx in java

开发者 https://www.devze.com 2022-12-18 16:32 出处:网络
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 \"

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("Tran开发者_StackOverflow中文版sformation 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 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/


You can use POI or docx4j to ensure you create valid Word documents.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消