赞
踩
pom文件
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>easyexcel</artifactId>
- <version>2.2.4</version>
- </dependency>
java代码
public void exportStoreSignList(HttpServletResponse response,@RequestParam(value="createTime",required = false)String createTime,@RequestParam(value="topic",required = false)String topic, @RequestParam(value="nowTime",required = false)String nowTime,@RequestParam(value="name",required = false)String name, @RequestParam(value="tableName",required = false)String tableName) throws Exception { //获取模板 ClassPathResource classPathResource = new ClassPathResource("/excel/excelCharts.xlsx"); //输入流 InputStream inputStream = null; //输出流 ServletOutputStream outputStream = null; //处理填充数据 List<Map<String,Object>> storeList = new ArrayList<>(); storeList = this.excelcharts(createTime,topic); ExcelWriter excelWriter = null; Map<String,Object> map = new HashMap<>(); if(tableName != null && tableName != ""){ map.put("name",name); }else{ map.put("name",""); } if(tableName != null && tableName != ""){ String rkDateFormat = createTime.substring(0, 4) + "年" + createTime.substring(5, 7) + "月" + createTime.substring(8, 10) + "日"; map.put("createTime",rkDateFormat); }else{ map.put("createTime",""); } try { //输入流 inputStream = classPathResource.getInputStream(); String fileName = URLEncoder.encode(tableName, "UTF-8"); response.setCharacterEncoding("utf-8"); //返回的数据类型 response.setContentType("application/vnd.ms-excel;charset=utf-8"); //响应头 response.setHeader("Content-Disposition", "attachment;fileName=" + fileName); outputStream = response.getOutputStream(); excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).build(); WriteSheet writeSheet = EasyExcel.writerSheet().build(); FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); excelWriter.fill(map, writeSheet); excelWriter.fill(new FillWrapper("data", storeList), fillConfig, writeSheet); outputStream.flush(); excelWriter.finish(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("导出错误!"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("异常!"); }finally { // 千万别忘记finish 会帮忙关闭流 if (excelWriter != null) { excelWriter.finish(); } //关闭流 if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
vue
- await exportStoreList(createTime,topic,nowTime,name,tableName).then((res) => {
- console.log("exportStoreList==================>",res)
-
- if (!res) {
- return
- }
- let url = window.URL.createObjectURL(new Blob([res]))
- let link = document.createElement('a')
- link.style.display = 'none'
- link.href = url
- link.setAttribute('download', this.excelChartsName)
-
- document.body.appendChild(link)
- link.click()
- }).catch(err => console.log('下载失败', err))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。