1

Need some quick help. I am trying to write a java program to generate a report. I have the report template in a docx file.

What I want to do is, use that docx file as template and put data in it multiple times for various records and write that to a new docx file. The main thing is I want to maintain the formatting and indentation of the contents inside the docx file. They are bullets data. And that's where the problem is.

Below is the piece of code handling the above operation,

    public void readWriteDocx(HashMap<String, String> detailsMap) {
    try {
        File reportTemplateFile = new File("ReportTemplate.docx");
        File actualReportFile = new File("ActualReport.docx");

        StringBuilder preReport = new StringBuilder();
        preReport.append("Some details about pre report goes here...: ");
        preReport.append(System.lineSeparator());

        String docxContent = "";
        for (Map.Entry<String, String> entry : detailsMap.entrySet()) {
            docxContent = FileUtils.readFileToString(reportTemplateFile, StandardCharsets.UTF_8);

            // code to fetch and get data to insert into docxContent

            docxContent = docxContent.replace("$filename", keyFilename);
            docxContent = docxContent.replace("$expected", expectedFile);
            docxContent = docxContent.replace("$actual", actualFile);
            docxContent = docxContent.replace("$reportCount", String.valueOf(reportCount));
            docxContent = docxContent.replace("$diffMessage", key);

            FileUtils.writeStringToFile(actualReportFile, docxContent, StandardCharsets.UTF_8, true);

        }
        preReport.append(FileUtils.readFileToString(actualReportFile, StandardCharsets.UTF_8));
        System.out.print(preReport.toString());

    } catch (IOException e) {
        e.printStackTrace();
    }
}

As you can see, I am using FileUtils read and write methods and using UTF_8 encoding. That's just a guess, I am not sure about the same. I am trying to append the newly generated docx file contents to a string builder and print the same on console, but that's secondary. Main thing is that the docx should be written properly. But no luck.

When this prints, its all weird characters and nothing is readable. When I try to open the newly generated docx file, it doesn't even open.

Any idea what should I do to get the data in proper format. I am attaching image file of how my ReportTemplate.docx looks, that I am using as a template to generate this report. I am using commons-io-2.4.jar

ReportTemplate.docx file

Please guide if you can. Thanks a lot.

WebNoob
  • 237
  • 6
  • 16

1 Answers1

0

You can use Apache POI for creating and editing doc docx files or docx4j. Otherwise there is no simple way to edit doc or docx files without these libraries.

D. Krauchanka
  • 264
  • 3
  • 15
  • Thanks for reply. I have never used POI. Will it allow me to use the docx file as template, then dynamically enter and append the data in it for various iterations and then eventually write all of the data to a new docx file? – WebNoob Nov 10 '16 at 21:01
  • Yes, you can do this easily. Here you can find an [example](http://stackoverflow.com/questions/22615594/replacing-variables-in-a-word-document-template-with-java) – D. Krauchanka Nov 11 '16 at 07:00
  • Thanks man, that was helpful. I have used that link and tried some code. Facing issues with that. Created a new question here : http://stackoverflow.com/questions/40547493/java-apache-poi-read-write-from-doc-file-issue See if you can help :-) – WebNoob Nov 11 '16 at 11:55