springboot+jxls|springboot+jxls + vue.js导出excel并在浏览器中下载

服务端 maven:

net.sf.jxls jxls-core 1.0.6 compile

/** * @param mQuamaintence 数据对象 * @return 文件名 */ @RequestMapping("export") public JCMessage excel(@RequestBody MQuamaintence mQuamaintence) {/*获取规则*/ DrivingFaceStick drivingFaceStick = new DrivingFaceStick(); drivingFaceStick.setForgid(mQuamaintence.getForgid()); drivingFaceStick.setTunnename(mQuamaintence.getTunnelid()); drivingFaceStick = drivingFaceStickService.getDrivingFaceStickUnique(drivingFaceStick); Map beans = new HashMap(); /*基本信息*/ beans.put("tence",mQuamaintence); /*规则*/ beans.put("driving", drivingFaceStick); //加载excel模板文件 File file = null; try { file = ResourceUtils.getFile("classpath:excel/model.xlsx"); } catch (FileNotFoundException e) {} //配置下载路径 createDir(new File(tempPath)); //根据模板生成新的excel File excelFile = createNewFile(beans, file, tempPath); JCMessage jcMessage = new JCMessage(); jcMessage.setData(excelFile.getName()); return jcMessage; }

【springboot+jxls|springboot+jxls + vue.js导出excel并在浏览器中下载】下载文件接口
/** * 下载文件 * @param response * @param fileName 文件名 */ @RequestMapping("downloadfile") public void downloadfile(HttpServletResponse response,String fileName){File excelFile = new File(tempPath+fileName); // downloadFile(response, excelFile); // // //删除服务器生成文件 deleteFile(excelFile); }

文件下载涉及接口
/** * 根据excel模板生成新的excel * * @param beans map对象 * @param file 文件 * @param path 文件路径 * @return 下载文件 */ private File createNewFile(Map beans, File file, String path) { XLSTransformer transformer = new XLSTransformer(); //可以写工具类来生成命名规则 String name = getUUid.getUUid() + ".xlsx"; File newFile = new File(path + name); try (InputStream in = new BufferedInputStream(new FileInputStream(file)); OutputStream out = new FileOutputStream(newFile)) { Workbook workbook = transformer.transformXLS(in, beans); workbook.write(out); out.flush(); return newFile; } catch (Exception e) { System.out.println(e.getMessage()); } return newFile; }/** * 将服务器新生成的excel从浏览器下载 * * @param response * @param excelFile excel文件 */ private void downloadFile(HttpServletResponse response, File excelFile) { /* 设置文件ContentType类型,这样设置,会自动判断下载文件类型 */ response.setContentType("multipart/form-data"); /* 设置文件头:最后一个参数是设置下载文件名 */ response.setHeader("Content-Disposition", "attachment; filename=" + excelFile.getName()); try ( InputStream ins = new FileInputStream(excelFile); OutputStream os = response.getOutputStream() ) { byte[] b = new byte[2048]; int len; while ((len = ins.read(b)) > 0) { os.write(b, 0, len); } } catch (IOException ioe) { ioe.printStackTrace(); } }/** * 浏览器下载完成之后删除服务器生成的文件 * 也可以设置定时任务去删除服务器文件 * * @param excelFile excel文件 */ private void deleteFile(File excelFile) {excelFile.delete(); }//如果目录不存在创建目录 存在则不创建 private void createDir(File file) { if (!file.exists()) { file.mkdirs(); } }

前端 视同vue中axios调用接口
/*数据导出*/ exportData: function(row) { row.isqualified = row.isqualified == "合格" ? true : false; var ajdata = https://www.it610.com/article/JSON.parse(JSON.stringify(row)); axios.post('preobservation/exportExcel/export', ajdata) .then(function(response) { var data = https://www.it610.com/article/response.data; window.location.href ='preobservation/exportExcel/downloadfile?fileName=' + data.data; }) .catch(function(error) { console.log(error); }); },

    推荐阅读