您的位置:首页 > 健康 > 养生 > java使用easypoi模版导出word详细步骤

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

2024/10/6 2:22:28 来源:https://blog.csdn.net/whs15193553607/article/details/140267209  浏览:    关键词: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内容如下
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com