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();
    }

模板文件

生成效果

相关推荐
czlczl200209254 分钟前
Spring Boot Filter 机制与 FilterRegistrationBean
java·spring boot·后端
严文文-Chris6 分钟前
RAG关键技术要点详解
java·服务器·前端
❀͜͡傀儡师10 分钟前
基于docker一键部署 x86的cpu_mem_hog 用于生成CPU和内存负载,用于服务器cpu和内存使用不达标的
java·服务器·docker
蜡笔大新79812 分钟前
IO流的认识(2)
java·ide·intellij-idea
廋到被风吹走22 分钟前
【Java】【JVM】OOM 原因、定位与解决方案
java·开发语言·jvm
苹果醋327 分钟前
vue + iview + vue-i18n中英翻译
java·运维·spring boot·mysql·nginx
橙露31 分钟前
VMware Workstation Pro 25H2的linux版本,免费分享,下载:全新命名体系 + 深度适配 Linux 内核,虚拟化效率拉满
java·linux·服务器
帮帮志36 分钟前
启动phcharm报错:Archived non-system classes are disabled because the java.system.
java·开发语言
Alex_81D40 分钟前
Spring Data JPA以及JPQL等特性详细使用教程
java·数据库·后端
spencer_tseng42 分钟前
Eclipse JDT Core for Java Code Formatter
java·ide·eclipse