java - How to read / write into docx file using commons.io.FileUtils? -


need quick help. trying write java program generate report. have report template in docx file.

what want is, use docx file template , put data in multiple times various records , write new docx file. main thing want maintain formatting , indentation of contents inside docx file. bullets data. , that's problem is.

below piece of code handling 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 pre report goes here...: ");         prereport.append(system.lineseparator());          string docxcontent = "";         (map.entry<string, string> entry : detailsmap.entryset()) {             docxcontent = fileutils.readfiletostring(reporttemplatefile, standardcharsets.utf_8);              // code fetch , data insert 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 can see, using fileutils read , write methods , using utf_8 encoding. that's guess, not sure same. trying append newly generated docx file contents string builder , print same on console, that's secondary. main thing docx should written properly. no luck.

when prints, weird characters , nothing readable. when try open newly generated docx file, doesn't open.

any idea should data in proper format. attaching image file of how reporttemplate.docx looks, using template generate report. using commons-io-2.4.jar

reporttemplate.docx file

please guide if can. lot.

you can use apache poi creating , editing doc docx files or docx4j. otherwise there no simple way edit doc or docx files without these libraries.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -