springboot整合POI导出word(文字加图片)
- 1、引入Poi依赖
- 2、设置word模板
- 3、(工具类)Utils
- 4、Controller
- 5、文件放置
- 6、运行结果
1、引入Poi依赖
<dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.8.2</version>
</dependency>
2、设置word模板
注意"{{}}"为中文模式下符号,图片占位{{@pic}},文字为{{text}}。
3、(工具类)Utils
import com.deepoove.poi.XWPFTemplate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URLEncoder;
import java.util.Map;public class ExportWordUtils {public static void exportWord(String templatePath, String temDir, String fileName, Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {if (!temDir.endsWith("/")) {temDir = temDir + File.separator;}File dir = new File(temDir);if (!dir.exists()) {dir.mkdirs();}try {XWPFTemplate doc = XWPFTemplate.compile(templatePath).render(params);String tmpPath = temDir + fileName;//新文件路径FileOutputStream fos = new FileOutputStream(tmpPath);doc.write(fos);fos.flush();fos.close();doc.close();} catch (Exception e) {e.printStackTrace();} finally {//delFileWord(temDir,fileName);//这一步看具体需求,要不要删}}public static void delFileWord(String filePath, String fileName){//删除文件File file =new File(filePath+fileName);File file1 =new File(filePath);file.delete();file1.delete();}
}
4、Controller
import com.deepoove.poi.data.PictureRenderData;
import com.qh.makeword.utils.ExportWordUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;@Controller
public class WordController {@RequestMapping("/export")public void export(HttpServletRequest request, HttpServletResponse response) throws IOException {Map<String,Object> params = new HashMap<>();//key值与模板中的占位符一致params.put("id","2020212345");params.put("name","李四");params.put("Image", new PictureRenderData(100, 140, new ClassPathResource("static/1.jpg").getFile().getPath()));ClassPathResource oldDoc = new ClassPathResource("word/export.docx");//一行代码ExportWordUtils.exportWord(oldDoc.getFile().getPath(),"newWord/","new.docx",params,request,response);}
}
5、文件放置
export.docx为模板,1.jpg为插入模板的图片
6、运行结果
参考:
https://blog.csdn.net/weixin_43694038/article/details/106123534?spm=1001.2014.3001.5506
https://blog.csdn.net/qq_41938882/article/details/89445678?utm_source=app&app_version=5.3.0&code=app_1562916241&uLinkId=usr1mkqgl919blen
springboot整合POI导出word(文字加图片)相关推荐
- springboot整合poi读取数据库数据和图片动态导出excel
springboot整合poi读取数据库数据和图片动态导出excel 第一次操作 话不多说就直接上代码 实现代码 需要的依赖 <dependency><groupId>org. ...
- Springboot整合Poi导出excel(简单版)
一. 问题引入 博客专栏: Springboot整合Poi导出excel(简单版) Springboot整合Poi导出excel(注解版) 总所周知Springboot是一个功能强大的微服务框架,集成 ...
- Springboot整合Poi导出excel(注解版)
简介 博客专栏: Springboot整合Poi导出excel(简单版) Springboot整合Poi导出excel(注解版) 上文提到通过poi简单导出Excel后,很多读者反应需要解决导出自适应 ...
- poi 导出word带单个图片
1.依赖 2.word模板 3.模板存放位置 4.代码 1.vo import io.swagger.annotations.ApiModelProperty; /*** @author qxc* @ ...
- C#通过模板导出Word(文字,表格,图片)
C#导出Word,Excel的方法有很多,这次因为公司的业务需求,需要导出内容丰富(文字,表格,图片)的报告,以前的方法不好使,所以寻找新的导出方法,在网上找到了通过模板文件导出Word的方法,记录一 ...
- SpringBoot整合Freemarker导出word文档表格
freemarker模板里面的template.process()方法里传入的第一个参数Object类型,如果是一个实体类对象在模板上怎么进行渲染,将实体类的值取出 freemarker会调用Obje ...
- poi导出word表格、图片、多段等处理
全文介绍 poi-tl(poi template language)是Word模板引擎,使用Word模板和数据创建很棒的Word文档 . 常用标签介绍(官网): 1文本:{{var}} 2. 图片:{ ...
- SpringBoot Poi导出word,浏览器下载
文章目录 SpringBoot Poi导出word,浏览器下载 1.引依赖: 2.写代码(生成本地word): 3.返回给浏览器下载 1.如何返回给浏览器让它下载 4.解决方案 5.为什么没使用eas ...
- Java使用Poi填充Word表格模板(图片和文字)
Java使用Poi填充Word表格模板(图片和文字) **** 由于个人需求需要对表格模板进行操作,所以本文章只对表格进行替换数据操作,没有段落,没有循环遍历,没有延伸!!!!!(后续补充!!!) * ...
最新文章
- CentOS6安装nodejs
- pandas根据数据类型筛选数据
- rstudio 修改代码间距_Windows电脑使用Rstudio会有多少错误呢
- boost::all_clustering_coefficients用法的测试程序
- 通道Channel-使用NIO 写入数据
- C语言求十个数中最大值
- elasticsearch根据某个字段来查询,以及通过时间筛选
- Asp.net三层结构原理与用意学习入门教程(五)
- mysql浮点数据怎么_MySQL数据浮点类型的实际应用操作
- 如何使用Service的Context弹出Dialog对话框,即全局性对话框
- 什么是重绘repaint?什么是回流reflow?
- 你还发现了CSDN那些变化
- 机器学习_深度学习毕设题目汇总——图像分类
- Axure 灯箱效果
- springboot Basic Auth 暴露API 访问认证
- python symbols函数_Python应用 | 求解微积分!
- 抖音直播用什么手机效果最好 抖音直播手机哪款好2023
- kettle效率提升
- 【GAM全文翻译及代码实现】Global Attention Mechanism: Retain Information to Enhance Channel-Spatial Interactions
- 硬件学习之路——STM32之TM1638
热门文章
- Qt--模拟按下按键(键盘)
- 随心所欲b超工作站图像处理_第七讲随心所欲版医学影像工作站软件每周一题...
- 蔡颖-《APS走向实践》书解读之三:供应、计划排程、供应链优化
- 企业邮箱怎么开通?手机微信怎么绑定公司邮箱?
- 阿拉伯数字转换成大写的数字
- IPPICV: Download failed: 6;“Couldn‘t resolve host name“
- 《安富莱嵌入式周报》第213期:2021.05.17--2021.05.23
- [python + pillow] 自制壁纸不完全教程
- Android计步模块(类似微信运动)
- 2011 ATMEL AVR 校园设计大赛获奖名单