Springboot + Vue + axios完成excel下载

  • 后端

pom

<dependency>
    <groupId>org.apache.poigroupId>
    <artifactId>poi-ooxmlartifactId>
    <version>4.1.0version>
dependency>

Contorller

@ResponseBody
@PostMapping(value = "/download")
public ResponseVo downloadNewBug(@RequestBody BugStatistical bugStatistical, HttpServletResponse httpServletResponse){
     

        return bugStatisticalService.exportExcel(bugStatistical, httpServletResponse);
    }

ServiceImpl

@Override
public ResponseVo exportExcel(BugStatistical bugStatistical, HttpServletResponse response) {
     

    List<BugStatistical> bugList = statisticalMapper.selectAllBug(bugStatistical);

    // 创建工作簿
    HSSFWorkbook workbook = new HSSFWorkbook();
    // 创建表
    HSSFSheet sheet = workbook.createSheet("提现信息");
    // 创建行
    HSSFRow row = sheet.createRow(0);
    // 创建单元格样式
    HSSFCellStyle cellStyle = workbook.createCellStyle();
    // 表头
    String[] head = {
     "时间", "缺陷编号", "缺陷等级", "提出人", "开发负责人", "产品线", "类型"};
    HSSFCell cell;
    // 设置表头
    for (int i = 0; i < head.length; i++) {
     
        cell = row.createCell(i);
        cell.setCellValue(head[i]);
        cell.setCellStyle(cellStyle);
        // 设置单元格宽度
        sheet.setColumnWidth(i, 4000);
    }
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    // 设置表格内容
    for (int i = 0; i < bugList.size(); i++){
     
        row = sheet.createRow( i + 1);
        BugStatistical bugInfo = bugList.get(i);
        // 这里是内容设置,替换则自己的数据即可
        String[] bugTitle = new String[7];
        bugTitle[0] = df.format(bugInfo.getCreateDate());
        bugTitle[1] = bugInfo.getBugId();
        bugTitle[2] = bugInfo.getBugLevel().toString();
        bugTitle[3] = bugInfo.getTesterName();
        bugTitle[4] = bugInfo.getDevName();
        bugTitle[5] = bugInfo.getProductLine();
        bugTitle[6] = bugInfo.getBugType();
        for (int j = 0; j < bugTitle.length; j++){
     
            row.createCell(j).setCellValue(bugTitle[j]);
        }
    }

    // 设置文件名
    String title ="bug信息";
    try {
     
        String  fileName = new String(title.getBytes("GB2312"), "ISO_8859_1");
        fileName = URLEncoder.encode(fileName,"utf-8");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        OutputStream os = response.getOutputStream();
        response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx");//默认Excel名称
        workbook.write(os);
        os.flush();
        os.close();
    } catch (UnsupportedEncodingException e) {
     
        e.printStackTrace();
    } catch (IOException e) {
     
        e.printStackTrace();
    }

    return null;
}
  • 前端
exportExcel(){
     
this.$axios({
     
  method:"post",
  url: "/xxx/download",
  responseType: 'blob',
  data:yourParams,
}).then(
  res=>{
     
    let data = res.data;
    let url = window.URL.createObjectURL(new Blob([data]));
    let link = document.createElement('a');
    link.style.display = 'none';
    link.href = url;
    link.setAttribute('download', 'bug信息.xlsx');
    document.body.appendChild(link);
    link.click()
  },err =>{
     
  });
},

你可能感兴趣的:(应该是个假前端)