Bootstrap

Word模板填充-基于Poi-tl动态填充多张图片(表格样式)

1.生成效果

在这里插入图片描述

word模板

在这里插入图片描述

核心代码

           //填充数据
           Map<String, Object> dateMap = new HashMap<>();
            //新增表格
            TableRenderData tableRenderData = Tables.ofA4Width().cellMargin(0.19f, 0.19f, 0.19f, 0.19f).center().create();
            //第一行图片
            {
                //单元格
                CellRenderData cell1 = new CellRenderData();
                CellRenderData cell2 = new CellRenderData();
                {
                    String image = "图片1.jpg";
                    byte[] imageByte = null;
                    PictureRenderData pic1 = new PictureRenderData(260, 200, getType(image), imageByte);
                    ParagraphRenderData graph1 = new ParagraphRenderData();
                    graph1.addPicture(pic1);
                    cell1.addParagraph(graph1);
                }
                {
                    String image = "图片2.jpg";
                    byte[] imageByte = null;
                    PictureRenderData pic1 = new PictureRenderData(260, 200, getType(image), imageByte);
                    ParagraphRenderData graph1 = new ParagraphRenderData();
                    graph1.addPicture(pic2);
                    cell2.addParagraph(graph1);
                }
                tableRenderData.addRow(Rows.create(cell1, cell2));
            }
            //第二行图片名称
            {
                //单元格
                String cell1 = "图片1";
                String cell2 = "图片2";
                tableRenderData.addRow(Rows.of(cell1, cell2).center().textFontSize(9).rowAtleastHeight(0.6).create());
            }
            dateMap.put("imageTables", tableRenderData);
           //去生成....
   
    /**
     * 获取图片类型
     *
     * @param path
     * @return
     */
    PictureType getType(String path) {
        //判断图片的格式
        PictureType format;
        if (path.endsWith(".emf")) {
            format = PictureType.EMF;
        } else if (path.endsWith(".wmf")) {
            format = PictureType.WMF;
        } else if (path.endsWith(".pict")) {
            format = PictureType.PICT;
        } else if (path.endsWith(".jpeg") || path.endsWith(".jpg")) {
            format = PictureType.JPEG;
        } else if (path.endsWith(".png")) {
            format = PictureType.PNG;
        } else if (path.endsWith(".dib")) {
            format = PictureType.DIB;
        } else if (path.endsWith(".gif")) {
            format = PictureType.GIF;
        } else if (path.endsWith(".tiff")) {
            format = PictureType.TIFF;
        } else if (path.endsWith(".eps")) {
            format = PictureType.EPS;
        } else if (path.endsWith(".bmp")) {
            format = PictureType.BMP;
        } else if (path.endsWith(".wpg")) {
            format = PictureType.WPG;
        } else {
            throw new BizException("不支持的图片类型Unsupported picture: " + path + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
        }
        return format;
    }
;