最近公司做项目,需要导出word或者PDF文档,PDF实现文档已经轻车熟路了,想研究搞一下word文档。经过调研发现了一个好用的开源项目Poi-tl

http://deepoove.com/poi-tl

poi-tl是一个基于Apache POI的Word模板引擎,也是一个免费开源的Java类库,你可以非常方便的加入到你的项目中,并且拥有着让人喜悦的特性

功能很强大,可以基于模板生成,写了一个demo

public class Demo {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = Demo.class.getResourceAsStream("/template.docx");
        String upgrade = "{\"upgradeStrategy\":0,\"fileDownloadModel\":0,\"downloadUrl\":\"http://downloadurl\",\"userName\":\"admin\",\"password\":\"admin\",\"bucket\":\" test\",\"path\":\"test/test.zip\",\"fileName\":\"test.zip\",\"fileSize\":10000,\"signatureMethod\":0,\"signatureValue\":\"123456asadsasdsa\",\"fileVersion\":1.0,\"upgradeStartTime\":\"2022-10-12 18:00:00\",\"extend\":{\"key1\":\"value2\",\"key2\":\"value2\"},\"timestamp\":\"2022-07-11 16:06:54\"}" ;
        JSONObject jsonObject = JSONObject.parseObject(upgrade);

        HighlightRenderData code = new HighlightRenderData();
        code.setCode(JSONObject.toJSONString(jsonObject, true));
        code.setLanguage("javascript");
        code.setStyle(HighlightStyle.builder().withShowLine(false).withTheme("vs").build());


        List<Goods> goods = new ArrayList<>();
        Goods good = new Goods();
        good.setCount(4);
        good.setName("墙纸");
        good.setDesc("书房卧室");
        good.setDiscount(1500);
        good.setPrice(400);
        good.setTax(new Random().nextInt(10) + 20);
        good.setTotalPrice(1600);
        good.setPicture(Pictures.ofUrl("http://deepoove.com/images/icecream.png")
                .size(100, 100).create());

        goods.add(good);
        goods.add(good);
        goods.add(good);

        List<Params> paramsList = new ArrayList<>();
        Params params1 = new Params();
        params1.setProductKey("productKey");
        params1.setParamType("String");
        params1.setDescText("要发送消息产品的ProductKey");
        paramsList.add(params1);

        Params params2 = new Params();
        params2.setProductKey("upgradeStrategy");
        params2.setParamType("String");
        params2.setDescText("升级策略(0:立即升级 1:定时升级 2:取消升级 3:仅下载 4:仅升级)");
        paramsList.add(params2);

        Params params3 = new Params();
        params3.setProductKey("downloadUrl");
        params3.setParamType("String");
        params3.setDescText(null);
        paramsList.add(params3);

        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        Configure config = Configure.builder().bind("code", new HighlightRenderPolicy()).bind("goods", policy).bind("paramsList",policy)                .useSpringEL()
                .build();
        XWPFTemplate template = XWPFTemplate.compile(inputStream, config).render(
                new HashMap<String, Object>() {{
                    put("title", "Hi, poi-tl Word模板引擎");
                    put("code", code);
                    put("goods", goods);
                    put("paramsList",paramsList);

                }});

        template.writeAndClose(new FileOutputStream("output"+UUID.randomUUID().toString().replace("-","")+".docx"));
    }
}

word 模板:
image.png

文档非常详细


nohup
160 声望9 粉丝