通过POI将word中通过占位符替换成图片的几种方式

349 阅读2分钟

通过POI将word中占位符替换成图片

方式一

用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

优势:功能强大,社区完善。

final static String template = "D:\\data\\poi\\ApachePoi模版.docx";
    final static String generate = "D:\\data\\poi\\ApachePoi生成效果.docx";
    final static String image = "D:\\data\\poi\\image.jpg";

    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream(template);
        XWPFDocument doc = new XWPFDocument(fis);
        for (XWPFParagraph p : doc.getParagraphs()) {
            List<XWPFRun> runs = p.getRuns();
            for (XWPFRun r : runs) {
                String text = r.getText(0);
                if (text != null && text.contains("{{image}}")) {
                    r.setText("", 0);
                    FileInputStream is = new FileInputStream(image);
                    r.addBreak();
                    r.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, image, Units.toEMU(200), Units.toEMU(200));
                    is.close();
                }
            }
        }
        FileOutputStream fos = new FileOutputStream(generate);
        doc.write(fos);
        fos.close();
    }

在这里插入图片描述

方式二

Poi-tl(poi template language)是Word模板引擎,使用模板和数据创建很棒的Word文档。

优势:简洁,易于上手

   final static String template = "D:\\data\\poi\\PoiTl模版.docx";
    final static String generate = "D:\\data\\poi\\PoiTl生成效果.docx";
    final static String image = "D:\\data\\poi\\image.jpg";

    public static void main(String[] args) throws Exception {
        XWPFTemplate xwpfTemplate = XWPFTemplate.compile(new File(template)).render(
                new HashMap<String, Object>() {{
                    put("image", Pictures.ofLocal(image).size(200, 200).create());
                }});
        xwpfTemplate.writeAndClose(new FileOutputStream(generate));
    }

在这里插入图片描述

方式三

依据“所见即所得”的思想,对模版一比一复刻生成。

优势:100%保留模版格式,模版与生成效果无差距

static String templatePath = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\检测报告.docx";
    static String savePath = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\生成文件.docx";
    static String CMA = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\image\\CMA.png";
    static String ZYZ = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\image\\ZYZ.png";
    static String EWM = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\image\\EWM.png";

    public static void main(String[] args) {
        Map<String, Object> datas = new HashMap<>();
        datas.put("BGBH", "JZ202401000001");//报告编号
        datas.put("JDH", "JD202401001");//监 督 号
        datas.put("GCMC", "台湾省某某商厦1#、2#楼商业幕墙");//工程名称
        datas.put("WTDW", "台湾省某某置业有限公司");//委托单位
        datas.put("YPMC", "硅酮耐候密封胶");//样品名称
        datas.put("JYLB", "见证送检");//检验类别
        datas.put("GSMC", "台湾省某某科学实验研究所有限公司");//公司名称
        datas.put("CMA", FileUtils.localToByte(CMA));
        datas.put("EWM", FileUtils.localToByte(EWM));
        datas.put("ZYZ", FileUtils.localToByte(ZYZ));
        PoiWordUtils.fillData(templatePath, savePath, datas, false);
    }

jiezuo img22222222222.png