• XWPFTemplate(二)动态生成表格


    记录XWPFTemplate关于表格的遍历。
    
    • 1

    具体代码

    public static void main(String[] args) throws IOException {
            //模板文件地址
            String filePath = "/Users/liu/Downloads/test.docx";
    
            Map<String,Object> map = new HashMap<>();
            Calendar now = Calendar.getInstance();
            //年
            map.put("year", now.get(Calendar.YEAR));
            //月
            map.put("month", (now.get(Calendar.MONTH)) + 1);
            //日
            map.put("day", now.get(Calendar.DAY_OF_MONTH));
    
            List<Map<String,Object>> detailList=new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                Map<String,Object> detailMap = new HashMap<>();
                detailMap.put("key",i);
                detailMap.put("key1","name"+i);
                detailMap.put("key2",i+"kg");
                detailMap.put("key3",i);
                detailMap.put("key4",103.3);
                detailList.add(detailMap);
            }
            //图片
            InputStream png = new FileInputStream("/Users/liu/Pictures/表情/7301626317522_.pic_hd.jpg");
            map.put("png", new PictureRenderData(270, 270, PictureType.PNG, png));
            List<HashMap> arrayList = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                HashMap m = new HashMap();
                m.put("image", Pictures.ofStream(new FileInputStream("/Users/liu/Pictures/表情/7301626317522_.pic_hd.jpg"), PictureType.PNG)
                        .size(400, 400).create());
                arrayList.add(m);
            }
            map.put("images",arrayList);
            //渲染表格
            HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
            ConfigureBuilder configureBuilder = Configure.builder();
            configureBuilder.bind("details", policy);
            Configure config = configureBuilder.build();
            map.put("details", detailList);
            map.put("title","测试生成");//总金额
    
            XWPFTemplate template = XWPFTemplate.compile(filePath, config).render(map);
    
            File file = new File("/Users/liu/Downloads/test1.docx");
            FileOutputStream out = new FileOutputStream(file);
            template.write(out);
            out.close();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    模板文件
    在这里插入图片描述
    生成效果
    在这里插入图片描述

  • 相关阅读:
    MongoDB文档(二)
    Python魔术方法
    数学建模的六个步骤
    大量需求测不过来怎么破?
    【算法挨揍日记】day07——904. 水果成篮、438. 找到字符串中所有字母异位词
    iOS系统暗黑模式
    在Flask中使用Jinjia2
    有人在双11疯狂剁手,有人在双11被直播“治愈”
    SpringBoot集成ElasticSearch(ES)
    java计算机毕业设计ssm+vue个人时间规划系统
  • 原文地址:https://blog.csdn.net/weixin_44150474/article/details/136239426