VUE+SpringBoot+EasyPoi实现浏览器点击下载word模板数据生成

阅读: 评论:0

VUE+SpringBoot+EasyPoi实现浏览器点击下载word模板数据生成

VUE+SpringBoot+EasyPoi实现浏览器点击下载word模板数据生成

目录

需求

模板

后台SpringBoot

WordUtil

EntityUtils

Controller

Service

ServiceImpl

前台VUE

效果

参考官网


需求

现在有个需求,页面有个人员列表,需要点击旁边的个人简介下载他的word数据

模板

 首先我们先建立个word文件,格式为docx

要绑定的数据就是我们的实体类的字段名,{{}}格式绑定

将模板扔进项目资源

后台SpringBoot

引入依赖

        <!-- 增加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

i.xyd_zgqx_back.utils;import cn.afterturn.easypoi.word.WordExportUtil;
import cn.afterturn.ity.MyXWPFDocument;
i.xyd_ity.TExpertTraining;
i.xyd_sult.Result;
i.xyd_sult.ResultGenerator;
plate.Configuration;
plate.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.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) {Null(templatePath, "模板路径不能为空");Null(temDir, "临时文件路径不能为空");if (!dsWith("/")) {temDir = temDir + File.separator;}File dir = new File(temDir);if (!ists()) {dir.mkdirs();}// 临时文件名String fileName = "temp.docx";try {XWPFDocument doc = new MyXWPFDocument(templatePath);portWord07(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''"+ de(fileName,"UTF-8"));OutputStream out = OutputStream();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

i.xyd_zgqx_back.utils;import flect.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 : Class().getDeclaredFields()) {try {boolean flag = field.isAccessible();field.setAccessible(true);Object o = (object);map.Name(), 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 {portExpertWord(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) (SessionKey.SUPPLIER_ID.key());if (orgId == null) {
//            FailResult("未获取到当前登录供应商信息,请重新登录");}// 专家idInteger id = Integer("id");if (id == null) {
//            FailResult("专家ID不能为空");}// 读取模板ClassPathResource classPathResource = new ClassPathResource("word/expertWord.docx");InputStream templatePath = null;try {templatePath = InputStream();} catch (IOException e) {e.printStackTrace();}// 查询数据库,将数据传给导出TExpertTraining tExpertTraining = tExpertTrainingMapper.selectById(id);if (tExpertTraining == null) {
//            FailResult("未查询到此专家ID的数据");}// 实体转MapMap<String, Object> map = ityToMap(tExpertTraining);// 导出portWord(map, templatePath, "D:\word", request, response);}

前台VUE

页面就好说了

请求的时候设置好responseTypeblob

下载的时候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 = ateObjectURL(blob); // 创建一个临时的url指向blob对象let a = ateElement("a");a.href = url;// 文件名a.download = name + '个人简介';a.click();// 释放这个临时的对象vokeObjectURL(url);}).catch(() => {})},

效果

 点击下载文件出来了

打开文件数据也都接入到word模板里

参考官网

/

本文发布于:2024-02-02 09:35:22,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170683772042921.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23