XWPFTemplate(二)动态生成表格

复制代码
记录XWPFTemplate关于表格的遍历。

具体代码

java 复制代码
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();
    }

模板文件

生成效果

相关推荐
程序员-周李斌30 分钟前
Java NIO [非阻塞 + 多路复用解]
java·开发语言·开源软件·nio
程序猿小蒜32 分钟前
基于Spring Boot的宠物领养系统的设计与实现
java·前端·spring boot·后端·spring·宠物
合作小小程序员小小店33 分钟前
web网页开发,在线%食堂管理%系统,基于Idea,html,css,jQuery,java,ssm,mysql。
java·前端·mysql·html·intellij-idea·jquery
奋斗的小高1 小时前
Docker 安装与使用
java
毕设源码-钟学长1 小时前
【开题答辩全过程】以 浮生馆汉服租赁管理系统为例,包含答辩的问题和答案
android·java·tomcat
90后小陈老师1 小时前
用户管理系统 07 项目前端初始化 | 新手实战 | 期末实训 | Java+SpringBoot+Vue
java·前端·spring boot
Coder-coco1 小时前
点餐|智能点餐系统|基于java+ Springboot的动端的点餐系统小程序(源码+数据库+文档)
java·vue.js·spring boot·小程序·论文·毕设·电子点餐系统
Halo_tjn1 小时前
Set集合专项实验
java·开发语言·前端·python
嘟嘟w2 小时前
Spring 核心注解深度分析
java·后端·spring
9***Y482 小时前
Java开发工具IntelliJ IDEA技巧
java·开发语言·intellij-idea