Creating docx in java

I have a docx template that I save as .xml and then parse the contents. Then I create a new updated document. After creating a Word document, I cannot open it. It says that "the document is damaged." I click OK. Then he says: “Click OK if you want to receive the document.” I click OK. Then I get an updated document. It happens every time. I created the same program as a stand-alone Java application. A document created through a stand-alone Java application opens without any errors. Can anyone give me an idea about this? I also used the same code for the server side.

Here is the code that I use to create the document.

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();

}
+3
4

POI Word (.doc, .docx, ). POI : - - , , API ( XML) -

http://poi.apache.org/

+1

POI docx4j, Word.

+1

JVM?. , , , , Eclipse UTF-8, , JVM ISO-8559.

, -Dfile.encoding=UTF-8.

0

I used both Apache POI and docx4j extensively, and said docx4j is more reliable as it offers more support out of the box not only for the document itself, but also for tables and images. Much of what docx4j does is automated where areas of the Apache POI you need to do a lot of manual coding to support docx. Unfortunately, not much has been done for POI to support docx. I would suggest using docx4j as they have built-in support for opening and saving a new .docx file out of the box.

0
source

Source: https://habr.com/ru/post/1730520/


All Articles