#excel #spring-boot #vue.js #apache-poi
Вопрос:
вот в чем дело. Я использую 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!=nullamp;amp;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 не определена.
Это сбивало меня с толку несколько дней. Любая помощь будет признательна.
Комментарии:
1. Так же как и ошибка в Springboot, Vue или Excel. Выделите строку с ошибками и обновите свой вопрос.
2. Я предполагаю, что проблема вызвана разделением интерфейса и бэкенда. Я отправляю запрос непосредственно в серверную часть, и файл можно загрузить в обычном режиме
3. Так вы ожидаете, что мы будем работать в этом на основе догадки ???