Springboot + Vue + axios完成excel下载

  • 后端

pom

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.0</version>
</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 =>{
  });
},
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值