poi-tl(poi template language)是Word模板引擎,使用模板和数据创建很棒的Word文档。
新增一个word,填入下面的内容
word文字 {{name}} {{author}} {{link}} {{anchor}} 图片 {{@img}} 表格 {{#table1}} {{#table2}} {{#table3}} 列表 {{*list}} 区块对(隐藏) {{?sections1}} Test {{/sections1}} 区块对(对象) {{?person}} Hi {{name}}! {{/person}} 区块对(列表循环文字) {{?songs}} {{name}} {{/songs}} 区块对(列表循环图片) {{?coll1}} {{@collImg}} {{/coll1}} 区块对(列表循环表格) {{?coll2}} {{#collTable}} {{/coll2}} 区块对(列表循环2) {{?coll3}} {{#collTable1}} {{/coll3}} 拼接word {{+nested}}
javaimport cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.*;
import com.deepoove.poi.data.style.BorderStyle;
import com.deepoove.poi.policy.ListRenderPolicy;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class testExport {
    public static void main(String[] args) throws IOException {
        Configure config = Configure.builder().bind("coll1", new ListRenderPolicy()).build();
        XWPFTemplate template = XWPFTemplate.compile("C:/Users/li823/Desktop/template.docx",config).render(
                new HashMap<String, Object>(){{
                    put("name", "Sayi");
                    put("author", Texts.of("Sayi").color("000000").create());
                    put("link", Texts.of("website").link("http://deepoove.com").create());
                    put("anchor", Texts.of("anchortxt").anchor("appendix1").create());
                    put("img", Pictures.ofUrl("https://img-blog.csdnimg.cn/2021051521244130.jpg").size(300, 300).altMeta("获取图片失败").create());
                    put("table1", Tables.of(new String[][] {
                            new String[] { "序列号","检查项目", "故障数量" },
                            new String[] { "1","各部位", "100" },
                            new String[] { "2","车轮", "66" }
                    }).border(BorderStyle.DEFAULT).create());
                    RowRenderData row0 = Rows.of("姓名", "学历").textColor("FFFFFF")
                            .bgColor("4472C4").center().create();
                    RowRenderData row1 = Rows.create("李四", "博士");
                    put("table2", Tables.create(row0, row1));
                    // 合并第1行所有单元格的表格
                    RowRenderData row2 = Rows.of("列0", "列1", "列2").center().bgColor("4472C4").create();
                    RowRenderData row3 = Rows.create("没有数据", null, null);
                    MergeCellRule rule = MergeCellRule.builder().map(MergeCellRule.Grid.of(1, 0), MergeCellRule.Grid.of(1, 2)).build();
                    put("table3", Tables.of(row2, row3).mergeRule(rule).create());
                    put("list", Numberings.ofDecimal("Plug-in grammar",
                            "Supports word text, pictures, table...",
                            "Not just templates").create());
                    put("sections1",false);
                    Map<String,String> personMap=MapUtil.of("name","张三");
                    put("person",personMap);
                    List<Map<String,String>> songs=ListUtil.of(
                            MapUtil.of("name","断点"),
                            MapUtil.of("name","后来"));
                    put("songs",songs);
                    List<Map<String, Object>> coll1 = ListUtil.of(
                            MapUtil.of("collImg", Pictures.ofUrl("https://img-blog.csdnimg.cn/2021051521244130.jpg").size(300, 300).altMeta("获取图片失败").create())
                            , MapUtil.of("collImg", Pictures.ofUrl("https://img-blog.csdnimg.cn/2021051521244130.jpg").size(300, 300).altMeta("获取图片失败").create())
                    );
                    put("coll1", coll1);
                    List<Map<String, Object>> coll2 = ListUtil.of(
                            MapUtil.of("collTable", Tables.of(new String[][] {
                                    new String[] { "序列号","检查项目", "故障数量" },
                                    new String[] { "1","各部位", "100" },
                                    new String[] { "2","车轮", "66" }
                            }).border(BorderStyle.DEFAULT).create()),
                            MapUtil.of("collTable", Tables.of(new String[][] {
                                    new String[] { "序列号","检查项目", "故障数量" },
                                    new String[] { "3","各部位", "77" },
                                    new String[] { "4","车轮", "88" }
                            }).border(BorderStyle.DEFAULT).create())
                    );
                    put("coll2", coll2);
                    List<Map<String, Object>> coll3 = ListUtil.of(
                            MapUtil.of("collTable1", Tables.of(new String[][] {
                                    new String[] { " "}
                            }).border(BorderStyle.DEFAULT).create()),
                            MapUtil.of("collTable1", Tables.of(new String[][] {
                                    new String[] { "序列号","检查项目", "故障数量" },
                                    new String[] { "3","各部位", "77" },
                                    new String[] { "4","车轮", "88" }
                            }).border(BorderStyle.DEFAULT).create())
                    );
                    put("coll3", coll3);
                   // put("nested", Includes.ofLocal("C:/Users/li823/Desktop/output1.docx").create());
                    put("quoteImg", Pictures.ofUrl("https://seopic.699pic.com/photo/50059/8720.jpg_wh1200.jpg").create());
                }});
        template.writeAndClose(new FileOutputStream("C:/Users/li823/Desktop/output.docx"));
    }
}

java    public void generateFileByPoitl2(Task task, String exportId) {
        try {
            List<AlgoFault> allAlgoFaults = iAlgoFaultService.lambdaQuery()
                    .eq(AlgoFault::getTaskSn, task.getId())
                    .eq(AlgoFault::getFaultStatus, 1)
                    .eq(AlgoFault::getCheckMode, 0)
                    .list();
            LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
            Configure config = Configure.builder()
                    .bind("checks", policy).bind("errs", policy).build();
            List<Table> tableColl = new ArrayList<>();
            QueryWrapper<AlgoFault> queryWrapper = new QueryWrapper();
            queryWrapper.select("part_name as partName", "COUNT(*) AS faultNum")
                    .eq("task_sn", task.getId())
                    .eq("check_mode",0)
                    .groupBy("part_name");
            List<Map<String, Object>> checks = iAlgoFaultService.listMaps(queryWrapper);
            Map<String, byte[]> imageCache = new ConcurrentHashMap<>();
            if (CollUtil.isNotEmpty(allAlgoFaults)) {
                ExecutorService executor = Executors.newFixedThreadPool(10);
                CompletionService<Void> completionService = new ExecutorCompletionService<>(executor);
                //检查项目列表Stream流
                for (AlgoFault abn : allAlgoFaults) {
                    Callable<Void> callTask = () -> {
                        if (!imageCache.containsKey(abn.getFaultPosImg())) {
                            imageCache.put(abn.getFaultPosImg(), ImageUtil.getUrlImageBytes(abn.getFaultPosImg(), 500, 400));
                        }
                        if (!imageCache.containsKey(abn.getFaultRelImg())) {
                            imageCache.put(abn.getFaultRelImg(), ImageUtil.getUrlImageBytes(abn.getFaultRelImg(), 280, 230));
                        }
                        if (!imageCache.containsKey(abn.getFaultImg())) {
                            imageCache.put(abn.getFaultImg(), ImageUtil.getUrlImageBytes(abn.getFaultImg(), 280, 230));
                        }
                        return null;
                    };
                    completionService.submit(callTask);
                }
                // 等待所有任务完成
                for (int i = 0; i < allAlgoFaults.size(); i++) {
                    try {
                        completionService.take().get();
                    } catch (Exception e) {
                        log.error(e.getMessage());
                    }
                }
                executor.shutdown();
                Map<String, List<AlgoFault>> groupMap = allAlgoFaults.stream().collect(Collectors.groupingBy(AlgoFault::getFaultPosName));
                groupMap.forEach((k, v) -> {
                    //构建外层循环
                    Table table = new Table();
                    table.setBigImg(Pictures.ofBytes(imageCache.get(v.get(0).getFaultPosImg())).center().size(500, 400).altMeta("获取图片失败,图片不存在!").create());
                    table.setBigImgName(v.get(0).getFaultPosName());
                    List<Child> childColl = new ArrayList<>();
                    //内层循环
                    for (AlgoFault f : v) {
                        Child c = new Child();
                        com.ditt.tiisCz.product.entity.common.poitl.Err err = new com.ditt.tiisCz.product.entity.common.poitl.Err();
                        err.setId(f.getFaultSn());
                        err.setCheckTime(DateUtil.format(f.getCheckTime(), "yyyy年MM月dd日 hh时mm分ss秒"));
                        err.setVehicleNo(f.getVehicleNo());
                        err.setTrainNo(f.getTrainNo());
                        err.setPartName(f.getPartName());
                        err.setPointName(f.getPointName());
                        err.setFaultTypeText(f.getFaultTypeText());
                        c.setErrs(ListUtil.of(err));
                        c.setRelImg(Pictures.ofBytes(imageCache.get(f.getFaultRelImg())).center().size(280, 230).altMeta("获取图片失败,图片不存在!").create());
                        c.setSmallImg(Pictures.ofBytes(imageCache.get(f.getFaultImg())).center().size(280, 230).altMeta("获取图片失败,图片不存在!").create());
                        childColl.add(c);
                    }
                    table.setChildColl(childColl);
                    tableColl.add(table);
                });
            }
            XWPFTemplate template = XWPFTemplate.compile(this.getClass().getResourceAsStream("/word/template2.docx"), config).render(
                    new HashMap<String, Object>() {{
                        put("startTime", DateUtil.format(task.getStartTime(), "yyy-MM-dd HH:mm:ss"));
                        put("endTime", DateUtil.format(task.getEndTime(), "yyy-MM-dd HH:mm:ss"));
                        put("trainNo", task.getTrainNo());
                        put("taskId", task.getId());
                        if (CollUtil.isNotEmpty(checks)) {
                            put("checks", checks);
                        }
                        if (CollUtil.isNotEmpty(tableColl)) {
                            put("tableColl", tableColl);
                        }
                    }});
            File tempFile = File.createTempFile(task.getId()+"_", ".docx");
            try (OutputStream ops = new FileOutputStream(tempFile)) {
                template.writeAndClose(ops);
                singleExportMap.put(exportId, tempFile.getAbsolutePath());
            } catch (IOException e) {
                e.printStackTrace();
                throw new SystemException(500, "导出文件出错," + e.getMessage());
            } finally {
                tempFile.deleteOnExit();
            }
            tableColl.clear();
            template = null;
            if (MapUtil.isNotEmpty(imageCache)) {
                imageCache.clear();
            }
        } catch (Exception e) {
            singleExportMap.put(exportId, "错误:" + e.getMessage());
        }
    }
ImageUtil.getUrlImageBytes
java    public static byte[] getUrlImageBytes(String url, int width, int height) {
        try {
            // 直接从网络读取图片并调整大小
            BufferedImage image = Thumbnails.of(new URL(url)).size(width, height).asBufferedImage();
            try(ByteArrayOutputStream baos = new ByteArrayOutputStream()){
                ImageIO.write(image, "jpg", baos);
                return baos.toByteArray();
            }
        } catch (Exception e) {
            log.error("获取图片失败: " + e.getMessage());
        }
        return new byte[0];
    }
本文作者:Weee
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!