poi导出数据到word,带图片且图片数量不确定(能确定数量范围,这里是3-20张)
注:有更好解决方法,参考:https://blog.csdn.net/m0_49605579/article/details/122583318
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.1</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.1</version>
</dependency>
<dependency><groupId>org.jxls</groupId><artifactId>jxls</artifactId><version>2.6.0</version><exclusions><exclusion><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId></exclusion></exclusions>
</dependency>
<dependency><groupId>org.jxls</groupId><artifactId>jxls-poi</artifactId><version>1.2.0</version>
</dependency>
<dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.core</artifactId><version>2.0.2</version>
</dependency>
<dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.document</artifactId><version>2.0.2</version>
</dependency>
<dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.template</artifactId><version>2.0.2</version>
</dependency>
<dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.document.docx</artifactId><version>2.0.2</version>
</dependency>
<dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId><version>2.0.2</version>
</dependency>
<dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.23</version>
</dependency>
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.5</version>
</dependency>
compile "fr.opensagres.xdocreport:fr.opensagres.xdocreport.core:2.0.2"compile "fr.opensagres.xdocreport:fr.opensagres.xdocreport.document:2.0.2"compile "fr.opensagres.xdocreport:fr.opensagres.xdocreport.document.docx:2.0.2"compile "fr.opensagres.xdocreport:fr.opensagres.xdocreport.template:2.0.2"compile "fr.opensagres.xdocreport:fr.opensagres.xdocreport.template.freemarker:2.0.2"compile "org.freemarker:freemarker:2.3.23"compile "commons-io:commons-io:2.5"compile "org.apache.poi:poi:3.14"compile "org.apache.poi:poi-ooxml:3.14"
2.创建导出模板
纯文字使用${}占位,图片使用建立初始化图片并添加书签的方式占位,
书签名就是datamap的键名,多张图片添加多个书签
3.编写代码(这里使用浏览器下载方式导出)
(1)导出工具方法
/*** 导出工具方法* // ** // * @param response 相应头* // * @param map 集合外数据* // * @param list 遍历数据* // * @param fileName 文件名称(带后缀)* // * @return 是否导出成功* // * @throws IOException* // * @throws XDocReportException*/public static void exportFile (HttpServletResponse response,Map<String, File> fileDataMap,String fileName,Map<String, Object> map,List<Object> list) throws IOException, XDocReportException {// 获取Word模板,模板存放路径在项目的resources目录下// 这里的Kit为当前工具方法的类名,如果不是静态方法可以直接用thisInputStreamins= Kit.class.getResourceAsStream(fileName);// 注册xdocreport实例并加载FreeMarker模板引擎IXDocReportreport=XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);// 创建xdocreport上下文对象IContext context = report.createContext();//添加文本数据if (map != null) {for (String s : map.keySet()) {context.put(s, map.get(s));}}//添加循环表格数据(需要自行添加,模板对应加上关键字)if (list != null) {context.put("resultList", list);// 创建字段元数据FieldsMetadata fm = report.createFieldsMetadata();// Word模板中的表格数据对应的集合类型fm.load("resultList", Object.class, true);}FieldsMetadata metadata = report.createFieldsMetadata();// 替换word模板中的动态图片if (fileMap != null) {for (String s : fileMap.keySet()) {IImageProvider zp = new FileImageProvider((fileMap.get(s)), true);zp.setSize(130F, 130F);metadata.addFieldAsImage(s);report.setFieldsMetadata(metadata);context.put(s, zp);}}// 浏览器端下载response.setCharacterEncoding("utf-8");response.setContentType("application/msword");response.setHeader("Content-Disposition","attachment;filename=".concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));report.process(context, response.getOutputStream());}
try{Map<String, File> fileMap = null;Map<String, Object> exportMap = new hashMap();exportMap.put("???","data");int num = 1;//由于poitl和导入功能用的poi版本冲突,无法用,这里使用IXDocReport对象做导出//可以循环添加,键名可以拼接为img1,img2,img3。。。,模板书签名对应//需要导出的文件集合,对接实际业务serviceList<File> list = new arrayList();if(list != null) {fileMap = new hashMap();for (File f : list) {fileMap.put("img" + num , f);num ++;}}Kit.exportFile(response, fileMap, "模板名称.docx", exportMap, null);} catch (Exception e) {e.printStackTrace();}
(3)因为我们业务文件接口返回的是byte数组,所以这里提供一个byte数组转File对象的方法(网上c的)
/*** byte数组转file对象工具方法(中方法)** @param inputStream* @param name* @param ext* @param tmpDirFile* @return* @throws IOException*/public static File createTmpFile(InputStream inputStream, String name, String ext, File tmpDirFile) throws IOException {File resultFile = File.createTempFile(name, '.' + ext, tmpDirFile);resultFile.deleteOnExit();FileUtils.copyToFile(inputStream, resultFile);return resultFile;}/*** byte数组转file对象工具方法** @param bytes 文件byte数组* @param fileType 输出文件类型* @return file对象* @throws IOException io异常*/public static File bytesToFile(byte[] bytes, String fileType) throws IOException {return createTmpFile(new ByteArrayInputStream(bytes),UUID.randomUUID().toString(),fileType,Files.createTempDirectory("tempFile").toFile());}
注:方法思路由该贴而来
https://blog.csdn.net/plxddyxnmd/article/details/109129838
poi导出数据到word,带图片且图片数量不确定(能确定数量范围,这里是3-20张)相关推荐
- java freemarker导出word 带图片,文件导出后图片无法使用office正常打开
问题记录:java freemark导出word 带图片,文件导出后图片无法使用office正常打开,解决之! 现象:wps打开正常,office如下 修改点: 图片${image1}标签前后保证无空 ...
- Java导出数据到Word模板中
Java导出数据到Word模板. 前言 网上的方案 需求介绍 模板 简介 使用体验 poi-tl Freemarker 操作步骤 总结 前言 相信很多人都会遇到Java导出的业务,Java导出主要有导 ...
- iText导出pdf、word、图片
一.前言 在企业的信息系统中,报表处理一直占比较重要的作用,本文将介绍一种生成PDF报表的Java组件--iText.通过在服务器端使用Jsp或JavaBean生成PDF报表,客户端采用超级连接显示 ...
- 使用POI导出数据到excel代码
使用POI导出数据到excel代码 POM文件 <!-- 导出excel --> <dependency><groupId>org.apache.poi</g ...
- Java使用poi导出数据到excel(包括xls和xlsx两种格式)并通过浏览器下载
情景:将数据导出到excel是java开发常用的功能,数据量不大的时候,xls和xlsx两种格式的文件都行,但是数据量太大的时候就有区别了,xls格式的文件一个sheet页最多只能存六万多条数据,而x ...
- poi使用模板导出word带图片
1.下面是我做的加载模板导出带图片的word的导出方式,使用之前请映入freemarker的jar包,可以从百度下载即可. 2.首先准备到你要导出的word模板,在要填充的模板中填入el表达式,如下图 ...
- poi 拆分带图片的word_java poi设置生成的word的图片为上下型环绕以及其位置的实现...
问题描述 在使用poi-tl word模版工具时,发现生成的文档中,图片格式为嵌入型,有的图片甚至被表格遮挡一半.而自己想要的图片格式为上下型环绕,并且图片需要居中. 问题分析 poi-tl渲染图片, ...
- vue 导出word带图片
下载依赖 包 npm install docxtemplater npm install pizzip npm install jszip npm install jszip-utils npm in ...
- java 导出word 带格式_java 导出数据为word文档(保持模板格式)
导出数据到具体的word文档里面,word有一定的格式,需要保持不变 这里使用freemarker来实现: ①:设计好word文档格式,需要用数据填充的地方用便于识别的长字符串替换 如 aaaaa ...
最新文章
- java p7 数字签名,p7结构的数字信封 | 学步园
- UIBarButtonSystemItem 各种款式
- 小学生python入门-极度舒适的全套 Python 入门教程,小学生看了也能学会
- Springboot搭建web项目
- 筛选汇总 power_批量汇总多Excel表格 | 标题都从第n行起怎么办?
- mediawiki mysql_MediaWiki
- Spark天堂之门解密
- Redis 6.0 的客户端缓存是怎么肥事?一文带你了解!
- 嵌入式系统实时仿真解决方案SkyEye
- c语言c99标准_从年薪10万到年薪30万,C语言程序员必读的5本书
- hadoop——Map/Reduce中combiner的使用
- 如何抢到腾讯云校园1元优惠资格(新版腾讯云)-更新
- ZeroMQ XPub/XSub模式
- 透过散射介质的成像matlab,基于解卷积透过散射介质大视场成像恢复方法
- c 语言查询条件并列怎么用,关于C# if语句中并列条件的执行
- appweb的开发步骤简介
- [2021江西省赛高校组]wp
- mechanize 是 pthon 的并且可编程的 浏览器插件,
- 【基础概念】什么是benchmark model?
- 楞严咒全文正确注音版_楞严咒全文注音