excel - springboot使用poi下载excel不起作用
问题描述
事情就是这样。我使用springboot作为后端和vue作为前端来导出excel。springboot的代码如下:
@RequestMapping(value = "/download/cellInfo", method = RequestMethod.GET)
public void downLoadCellFile(@RequestParam String countyCode,
@RequestParam String cellCode,
HttpServletResponse response) {
try {
configService.downLoad(countyCode, cellCode,response);
} catch (Exception e) {
log.error("error:", e);
}
}
public void downLoad(String countyCode, String cellCode, HttpServletResponse response) throws IOException {
String fileName = createFileName(countyCode,cellCode);
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
List<Residential> residentialList = residentialService.listResidents(countyCode,cellCode);
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("用户信息");
Row head = sheet.createRow(0);
head.createCell(0).setCellValue("楼栋");
head.createCell(1).setCellValue("单元");
head.createCell(2).setCellValue("门牌");
head.createCell(3).setCellValue("用户号");
head.createCell(4).setCellValue("用户姓名");
head.createCell(5).setCellValue("联系电话");
head.createCell(6).setCellValue("详细安装地址");
head.createCell(7).setCellValue("表口径");
head.createCell(8).setCellValue("设备地址");
FileOutputStream fos = null;
if(residentialList!=null&&residentialList.size()>0) {
for (Residential residential : residentialList) {
Row body = sheet.createRow(sheet.getLastRowNum()+1);
body.createCell(0).setCellValue(residential.getUnitCode().split("-")[0]);
body.createCell(1).setCellValue(residential.getUnitCode().split("-")[1]);
body.createCell(2).setCellValue(residential.getRoomCode());
}
fos = new FileOutputStream(fileName);
fos.close();
}
response.flushBuffer();
ServletOutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
return ;
}
vue的代码如下:
handleExport() {
for (let i = 0; i < this.exportList.length; i++) {
this.$axios({
method: 'get',
url: address + "/config/download/cellInfo",
params: {countyCode:this.exportList[i].countyCode,
cellCode:this.exportList[i].cellCode},
responseType: 'blob'
})
.then(res => {
console.log(res)
const blob = new Blob([res.data],{type: 'application/vnd.ms-excel'});
let filename ='1.xls';
//创建一个超链接,将文件流赋进去,然后实现这个超链接的单击事件
const elink = document.createElement('a');
elink.download = filename;
elink.style.display = 'none';
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href); // 释放URL 对象
document.body.removeChild(elink);
})
.catch(err => {
console.log('失败', err)
})
}
this.exportDialogVisible = false;
}
现在的问题是请求返回 200 但下载没有开始,并且 vue 的控制台未定义。这让我困惑了几天。任何帮助将不胜感激。
解决方案
推荐阅读
- sql - Postgres 在跳过缺失值的同时有效地检查 JSONB 包含
- sparql - SPARQL:找到具有相同三元组的主题?
- c - 当数字的任何位等于0时如何打印1?
- python - 将不带分隔符的 .txt 文件抓取到 JSON 对象中
- java - Jsoup 选择器没有找到某个类
- grep - 搜索并打印集合中的两个关键词
- node.js - NodeJs Route 在生产中返回 404,但在本地不返回
- scala - 升级了 spark 版本,在 spark 作业中遇到 java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Product;)V
- sql - Pyspark 分组和结构化数据
- onnx - onnxruntime:无法导入名称“get_all_providers”