1. 程式人生 > >Java 實現後臺生成doc文件

Java 實現後臺生成doc文件

最近公司需要後臺報告自動生成,就查了一些實現方式。

最初想生成PDF報告,Freemark + Itext + flying saucer 可以實現,但是生成的PDF文件,後續不易修改。

就改為生成word文件,找到了java兩種實現方式 1.freemark  2. poi 

Freemark .


mvn 配置:

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.20</version>
</dependency>

HTML模板

<h1 id="title_text">基因檢測報告</h1>
<br>
<br>
         <table_title > 患者資訊</table_title>
       <table  class="infotable" width="600">
                 <strong id="a1">姓名:&nbsp;&nbsp;&nbsp;</strong><a class="a2" 
>${Patientname}</a><br> <strong id="a1">年齡:&nbsp;&nbsp;&nbsp;</strong><a class="a2">${PatientAge}</a><br> <strong id="a1">性別:&nbsp;&nbsp;&nbsp;</strong><a class="a2">${PatientSex}</a><br>
</table> <br> <br> <table_title > 化療藥物</table_title> <table class="gridtable" width="600" border="1"> <tr> <th align="left">疾病</th> <th align="left" >藥物</th> <th align="left">基因</th> <th align="left">rs</th> <th align="left" >證據等級</th> <th align="left">基因型</th> <th align="left ">註釋</th> </tr> <#if ChemotherapyList?exists> <#list ChemotherapyList as item> <tr> <td>${item.mDisease}</td> <td>${item.mDrug}</td> <td>${item.mGene}</td> <td>${item.mRS}</td> <td>${item.mEvidenceLevel}</td> <td>${item.mGenotype}</td> <td>${item.mAnnotation}</td> </tr> </#list> </#if> </table>

資料與模板繫結

 Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("C:/Users/ykx/codes/demo/src/main/resources/templates")); //模板資料夾路徑
Template template = cfg.getTemplate("testRS.ftl");  //模板檔案
cfg.setDefaultEncoding("UTF-8");
Map root =  new HashMap(); 
root.put("patientName","老王");
root.put("patientAge","21");
root.put("patientSex","男");
List<ChemotherapyData> userList = new ArrayList<ChemotherapyData>();
// userList.add(new ChemotherapyData());
File file = new File("C:/Users/ykx/codes/demo/src/main/resources/chemotherapeutics.txt");
ArrayList<ChemotherapyData> arrayList = new ArrayList<>();
root.put("patientList",ReaderLocalFiles.txt2String1(arrayList, file));
Writer out1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:/DATA/test.docx"),"UTF-8")); //生成word路徑template.process(root, out1);
word文件


POI

後來又碰到poi包,相比freemark簡單些,不用編輯html模板,直接用word檔案為為文件(docx)

mvn配置

<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.0.0</version>
</dependency>

模板


Code:

  ArrayList<Object> ch_info = new ArrayList<>();
File file = new File("C:\\Users\\ykx\\codes\\demo\\src\\main\\resources\\chemotherapeutics.txt");
Map<String, Object> datas = new HashMap<String, Object>(){{
        put("name", "老王");
put("age", "80");
put("sex","男");
put("table", new TableRenderData(new ArrayList<RenderData>(){{
            add(new TextRenderData("FFD39B", "疾病"));
add(new TextRenderData("FFD39B", "藥物"));
add(new TextRenderData("FFD39B", "基因"));
add(new TextRenderData("FFD39B", "rs"));
add(new TextRenderData("FFD39B", "證據等級"));
add(new TextRenderData("FFD39B", "基因型"));
add(new TextRenderData("FFD39B", "臨床指導"));
}},ReaderLocalFiles.readChemotherapyData1(ch_info, file), "no datas", 8600));
}};
//讀取模板,進行渲染
XWPFTemplate doc = XWPFTemplate
            .create("D:/DATA/test121.docx");
RenderAPI.render(doc, datas);
//輸出渲染後的檔案
FileOutputStream out = new FileOutputStream("D:/DATA/output.docx");
doc.write(out);
out.flush();
out.close();
}
word檔案


GitHub:https://github.com/YukaiXin/docrender