Java 实现后台生成doc文档

11 篇文章 0 订阅
9 篇文章 1 订阅


最近公司需要后台报告自动生成,就查了一些实现方式。

最初想生成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


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值