首页 > 解决方案 > 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("用户号&quot;);
    head.createCell(4).setCellValue("用户姓名");
    head.createCell(5).setCellValue("联系电话");
    head.createCell(6).setCellValue("详细安装地址");
    head.createCell(7).setCellValue("表口径&quot;);
    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 的控制台未定义。这让我困惑了几天。任何帮助将不胜感激。

标签: excelspring-bootvue.jsapache-poi

解决方案


推荐阅读