当前位置: 首页 > news >正文

java使用easypoi模版导出word详细步骤

文章目录

  • 第一步、引入pom依赖
  • 第二步、新建导出工具类WordUtil
  • 第三步、创建模版word
  • 4.编写接口代码
  • 5.导出结果示例

第一步、引入pom依赖

<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-spring-boot-starter</artifactId><version>4.4.0</version>
</dependency>

第二步、新建导出工具类WordUtil


import cn.afterturn.easypoi.word.WordExportUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Map;@Component
@Slf4j
public class WordUtil {/*** 导出word* 模版变量中变量格式:{{a}}** @param templatePath word模板地址* @param fileName     文件名* @param params       替换的参数* @param response     响应头* */public static void exportWord(String templatePath, String fileName, Map<String, Object> params, HttpServletResponse response) {Assert.notNull(templatePath, "模板路径不能为空");Assert.notNull(fileName, "导出文件名不能为空");Assert.isTrue(fileName.endsWith(".docx"), "word导出请使用docx格式");try {XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);//设置响应体内容类型response.setContentType("application/octet-stream");//添加响应头response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));//暴露新添加的响应头response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");//将word文档流输出到输出流中doc.write(response.getOutputStream());//关闭流doc.close();} catch (Exception e) {log.error("exportWord方法出现问题", e);e.printStackTrace();}}}

第三步、创建模版word

在静态资源目录下resources/static/templates新建exportWord.docx,编写以下模版内容:
在这里插入图片描述

4.编写接口代码

    @GetMapping("/exportWord")public void exportWord(HttpServletResponse response) throws FileNotFoundException {//存放数据,也就是填充在word里面的值Map<String, Object> params = new HashMap<>();params.put("ceshi","测试使用easypoi模版导出word");params.put("name","张三");params.put("text","知之为知之不知为不知");//模板路径// String templatePath = "E:\\demo\\word.docx";// 或模板在静态资源的相对路径File rootFile = new File((ResourceUtils.getURL("classpath:").getPath()));File templateFile = new File(rootFile, "/static/templates/exportWord.docx");//jar包获取不到文件路径//URLDecoder.decode() 解决获取中文名称文件路径乱码String templatePath = URLDecoder.decode(templateFile.getPath());//生成文件名String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" + System.currentTimeMillis() + ".docx";// 导出woldtry {// 导出Word文档为文件WordUtil.exportWord(templatePath, fileName, params,response);// 将导出的Word文件转换为流} catch (Exception e) {System.out.println("导出Word文档时出现异常:" + e.getMessage());}}

5.导出结果示例

通过浏览器访问接口:http://localhost:8080//exportWord,导出word内容如下
在这里插入图片描述

http://www.lryc.cn/news/397714.html

相关文章:

  • Android 内部保持数据的方式
  • uniapp 表格,动态表头表格封装渲染
  • beyond Compare连接 openWrt 和 VsCode
  • 量化机器人能否识别市场机会?
  • 香橙派AIpro开发板评测:部署yolov5模型实现图像和视频中物体的识别
  • MongoDB教程(二):mongoDB引用shell
  • A133 Android10 root修改
  • 实验场:在几分钟内使用 Bedrock Anthropic Models 和 Elasticsearch 进行 RAG 实验
  • 代理详解之静态代理、动态代理、SpringAOP实现
  • Laravel - laravel-websockets 开发详解
  • vue3 学习笔记04 -- axios的使用及封装
  • 键盘快捷键设置录入
  • 刷题Day49|647. 回文子串、516.最长回文子序列
  • 关于transformers库验证时不进入compute_metrics方法的一些坑
  • 苹果提出RLAIF:轻量级语言模型编写代码
  • [leetcode] shortest-subarray-with-sum-at-least-k 和至少为 K 的最短子数组
  • 专业140+总分420+天津大学815信号与系统考研经验天大电子信息与通信工程,真题,大纲,参考书。
  • 前端如何取消接口调用
  • k8s 容器环境下的镜像如何转换为docker 使用
  • FreeRTOS 队列
  • 如何识别图片文字转化为文本?5个软件帮助你快速提取图片文字
  • Flink SQL kafka连接器
  • glm-4 联网搜索 api 测试
  • Java毕业设计 基于SSM vue图书管理系统小程序 微信小程序
  • bert训练的一些技巧(rand() < self.skipgram_prb)
  • pandas修改时间索引报错处理
  • Nginx Bla~Bla~
  • java awt和swing介绍
  • 奇怪的错误记录
  • 来啦,经典传说大变身牛郎织女后代逗趣日常