本文主要是介绍VUE+SpringBoot+EasyPoi实现浏览器点击下载word模板数据生成,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
需求
模板
后台SpringBoot
pom.xml
WordUtil
EntityUtils
Controller
Service
ServiceImpl
前台VUE
效果
参考官网
需求
现在有个需求,页面有个人员列表,需要点击旁边的个人简介下载他的word数据
模板
首先我们先建立个word文件,格式为docx
要绑定的数据就是我们的实体类的字段名,{{}}格式绑定
将模板扔进项目资源
后台SpringBoot
pom.xml
引入依赖
<!-- 增加poi依赖--><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-spring-boot-starter</artifactId><version>4.3.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>4.4.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>4.4.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>4.4.0</version></dependency><dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.12.0</version></dependency>
WordUtil
package com.cei.xyd_zgqx_back.utils;import cn.afterturn.easypoi.word.WordExportUtil;
import cn.afterturn.easypoi.word.entity.MyXWPFDocument;
import com.cei.xyd_zgqx_back.entity.TExpertTraining;
import com.cei.xyd_zgqx_back.entity.vo.result.Result;
import com.cei.xyd_zgqx_back.entity.vo.result.ResultGenerator;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;@Component
public class WordUtil {/*** 导出word* <p>第一步生成替换后的word文件,只支持docx</p>* <p>第二步下载生成的文件</p>* <p>第三步删除生成的临时文件</p>* 模版变量中变量格式:{{name}}** @param map 实体类转Map* @param templatePath word模板地址* @param temDir 生成临时文件存放地址* @param request HttpServletRequest* @param response HttpServletResponse*/public static void exportWord(Map<String, Object> map, InputStream templatePath, String temDir, HttpServletRequest request, HttpServletResponse response) {Assert.notNull(templatePath, "模板路径不能为空");Assert.notNull(temDir, "临时文件路径不能为空");if (!temDir.endsWith("/")) {temDir = temDir + File.separator;}File dir = new File(temDir);if (!dir.exists()) {dir.mkdirs();}// 临时文件名String fileName = "temp.docx";try {XWPFDocument doc = new MyXWPFDocument(templatePath);WordExportUtil.exportWord07(doc, map);String tmpPath = temDir + fileName;FileOutputStream fos = new FileOutputStream(tmpPath);doc.write(fos);// 设置强制下载不打开response.setContentType("application/force-download");// 设置文件名response.setHeader("Content-Disposition", "attachment;filename*= UTF-8''"+ URLEncoder.encode(fileName,"UTF-8"));OutputStream out = response.getOutputStream();doc.write(out);fos.close();out.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.delete();}
}
EntityUtils
package com.cei.xyd_zgqx_back.utils;import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;public class EntityUtils {/**** @description: 实体类转Map* @return*/public static Map<String, Object> entityToMap(Object object) {Map<String, Object> map = new HashMap<>();for (Field field : object.getClass().getDeclaredFields()) {try {boolean flag = field.isAccessible();field.setAccessible(true);Object o = field.get(object);map.put(field.getName(), o);field.setAccessible(flag);} catch (Exception e) {e.printStackTrace();}}return map;}
}
Controller
/*** */@PostMapping(value = "/exportExpertWord")public void exportExpertWord(@RequestBody String json, HttpServletRequest request, HttpServletResponse response) throws PendingException {iSupplierService.exportExpertWord(json, request, response);}
Service
void exportExpertWord(String json, HttpServletRequest request, HttpServletResponse response);
ServiceImpl
这里面上面有些个人业务,酌情修改删除
步骤就是 >>>> 读取模板 >>>> 查询数据库数据 >>>> 模型结果转Map >>>> 传入Util导出
@Overridepublic void exportExpertWord(String json, HttpServletRequest request, HttpServletResponse response) {JSONObject jsonObject = JSONObject.parseObject(json);// 当前登录供应商数据Integer orgId = (Integer) sign.get(SessionKey.SUPPLIER_ID.key());if (orgId == null) {
// return ResultGenerator.genFailResult("未获取到当前登录供应商信息,请重新登录");}// 专家idInteger id = jsonObject.getInteger("id");if (id == null) {
// return ResultGenerator.genFailResult("专家ID不能为空");}// 读取模板ClassPathResource classPathResource = new ClassPathResource("word/expertWord.docx");InputStream templatePath = null;try {templatePath = classPathResource.getInputStream();} catch (IOException e) {e.printStackTrace();}// 查询数据库,将数据传给导出TExpertTraining tExpertTraining = tExpertTrainingMapper.selectById(id);if (tExpertTraining == null) {
// return ResultGenerator.genFailResult("未查询到此专家ID的数据");}// 实体转MapMap<String, Object> map = EntityUtils.entityToMap(tExpertTraining);// 导出wordWordUtil.exportWord(map, templatePath, "D:\\word", request, response);}
前台VUE
页面就好说了
请求的时候设置好responseType为blob
下载的时候new Blob那注意后面type改成你要下载的文件格式类型,我那块是举例word
// 下载专家个人简介worddownloadProfile(id, name) {const params = {id: id}this.$http.post('/supplier/exportExpertWord', params, {responseType: 'blob'},).then((response) => {// 为blob设置文件类型let blob = new Blob([response], {type: 'application/msword'});let url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象let a = document.createElement("a");a.href = url;// 文件名a.download = name + '个人简介';a.click();// 释放这个临时的对象urlwindow.URL.revokeObjectURL(url);}).catch(() => {})},
效果
点击下载文件出来了
打开文件数据也都接入到word模板里
参考官网
http://easypoi.mydoc.io/
这篇关于VUE+SpringBoot+EasyPoi实现浏览器点击下载word模板数据生成的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!