栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java动态导出word文档内容及图片

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java动态导出word文档内容及图片

前言

最近公司在做一个工业柔性产线管控,项目中有一个需求要将工序中流程图及流程图中的子流程及工步相关信息导入到表格中(因为流程图下的子流程图有无及数量未知,所以最好的办法就是使用动态导入);因为以前更多接触的是excel的导入与导出,word还没接触过,刚好家人出现了黄码,就在家居家一天,顺便把相关需求写个demo;希望对有相关需求的朋友有帮助。
不废话,上代码

导入依赖
	  
		
			org.apache.poi
			poi-ooxml
			5.2.2
		
		
			org.apache.poi
			poi
			5.2.2
		
		
		
			com.deepoove
			poi-tl
			1.12.0
		
初始化数据
    public Map initWordData() {

        Map renderData = new HashMap<>();
        // 文本标题
        TextRenderData title = new TextRenderData("工序名称:...");
        renderData.put("title", title);

        // 文本内容
        StringBuilder contentData = new StringBuilder()
                .append("内容与大地112sada")
                .append(System.lineSeparator())
                .append("......");
        TextRenderData content = new TextRenderData(contentData.toString());
        renderData.put("content", content);

        //主流程图 图片可以本地的
        renderData .put("father", "D:\27.jpg");

        Tables.TableBuilder tableBuilder = Tables.of(new String[][]{
                new String[]{"表头1", "表头2"},
                new String[]{"10", "11"},
                new String[]{"sadad", "da"}
        });
        //子流程图 网上随便找的
        Pictures.PictureBuilder childPic = Pictures.ofUrl("https://img12.360buyimg.com/n7/jfs/t1/198601/28/13395/148193/616ab0f7E31ac234b/9d504022a9069a2b.jpg");

        // child信息
        TextRenderData name = new TextRenderData("子流程");
        renderData.put("name1", name);
        renderData.put("table1",tableBuilder.border(BorderStyle.DEFAULT).create());
        renderData.put("pic1", childPic.create());

        return renderData;
    }
动态替换模板并导出
   
    public void exportWord() {
        OutputStream os = null;
        XWPFTemplate template = null;
        try {
           Map renderData = initWordData();

           //编辑模板将模板动态化
            Configure config = Configure.builder().bind("data", new DocumentRenderPolicy()).build();
            Map data = new HashMap<>();
            Documents.DocumentBuilder documentBuilder = Documents.of()
                    .addParagraph(Paragraphs.of("{{title}}").create())
                    .addParagraph(Paragraphs.of("{{content}}").create())
                    .addParagraph(Paragraphs.of("{{@father}}").create());

            for (int i = 1; i < 2; i++) {
                documentBuilder.addParagraph(Paragraphs.of("{{name"+i+"}}").create());
                documentBuilder.addParagraph(Paragraphs.of("{{#table"+i+"}}").create());
                documentBuilder.addParagraph(Paragraphs.of("{{@pic"+i+"}}").create());
            }
            DocumentRenderData document = documentBuilder.create();
            data.put("data", document);

            // 原始模板文件
            String templatePath = "D:\simpleTemplate3.docx";
            InputStream is = new FileInputStream(new File(templatePath));

            // 根据配置编译  并渲染数据
            template = XWPFTemplate.compile(is,config).render(data);

            // 临时输出位置
            String outPath = "D:\test32.docx";
            os = new FileOutputStream(new File(outPath));
            // 写出
            template.write(os);

            // 模板文件
            InputStream is2 = new FileInputStream(new File(outPath));
            // 渲染数据
            template = XWPFTemplate.compile(is2).render(renderData);
            // 最终输出位置
            String outPath2 = "D:\test3.docx";
            os = new FileOutputStream(new File(outPath2));
            template.write(os);
            
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (template != null) {
                    template.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
测试 原始模板

临时模板


说明:{{var}}是文本标签;{{@var}}是图片标签;{{#var}}是表格标签

最终结果


需要说明的是目前 poi-tl 是最新的版本,需要poi的版本也较高需要更新到5.2.2,如果不需要临时模板这个也很好操作,最终输出为一个文件即可更改;最后希望对大家有帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1031625.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号