导出excel 到浏览器下载中

前端方法:

/*后台流导出*/
  exportFile({url, fileName, type, data}) {
    let xhr = new XMLHttpRequest();
    xhr.withCredentials = true;
    xhr.open('POST', url, true);
    xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
    xhr.responseType = "blob";  // 返回类型blob
    //发送空内容请求
    xhr.send(JSON.stringify(data));
    fileName = `${fileName}.${type}`;
    if (type === 'pdf') {
      type = 'application/pdf';
    }else if (type === 'xls') {
      type = 'application/vnd.ms-excel';
    }else if (type === 'doc') {
      type = 'application/msword';
    }
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4 && xhr.status == 200) {
        // 数据在 this.response 保存
        let blob = new Blob([this.response], {
          type: type
        });
        // 创建a链接 href链接地址 download为下载下来后文件的名称
        let aa = document.createElement('a');
        aa.href = URL.createObjectURL(blob);
        aa.innerHTML = 'a链接';
        aa.download = fileName;
        document.body.appendChild(aa);
        aa.click();
        document.body.removeChild(aa);
        URL.revokeObjectURL(blob)
      }
    }
  }

后端代码:

        List<TjColHfglExcel> excel = TjColHfglDao.getTjColHfglByExcel(dto, session);
        //对结果进行字典转换
        if (CollUtil.isNotEmpty(excel)) {
            for (TjColHfglExcel tjColHfglExcel : excel) {
                tjColHfglExcel.setColorFinally(riskLevelDic.get(tjColHfglExcel.getColorFinally()));
                tjColHfglExcel.setHffs(followUpMethodDic.get(tjColHfglExcel.getHffs()));
                tjColHfglExcel.setZxzt(executionStatusDic.get(tjColHfglExcel.getZxzt()));
                tjColHfglExcel.setGlys(bbpPersonDic.get(tjColHfglExcel.getGlys()));
                tjColHfglExcel.setDxbz(sfDic.get(tjColHfglExcel.getDxbz()));
                //处理时间 将时间格式转成yyyy_MM_dd
                if (StrUtil.isNotEmpty(tjColHfglExcel.getHfrq())){
                    tjColHfglExcel.setHfrq(tjColHfglExcel.getHfrq().substring(0,10));
                }
                if (StrUtil.isNotEmpty(tjColHfglExcel.getZxrq())){
                    tjColHfglExcel.setZxrq(tjColHfglExcel.getZxrq().substring(0,10));
                }}
                }

        //设置表头
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.addHeaderAlias("xm", "姓名");
        writer.addHeaderAlias("colorFinally", "风险等级");
        writer.addHeaderAlias("tjbh", "体检编号");
        writer.addHeaderAlias("lxdh", "联系电话");
        writer.addHeaderAlias("hfrq", "回访日期");
        writer.addHeaderAlias("glys", "管理医生");
        writer.addHeaderAlias("hffs", "回访方式");
        writer.addHeaderAlias("departmentinfo", "社区");
        writer.addHeaderAlias("dxbz", "短信");
        writer.addHeaderAlias("zxrq", "执行日期");
        writer.addHeaderAlias("zxr", "执行人");
        writer.addHeaderAlias("zxzt", "执行状态");



        writer.write(excel, true);
        //response为HttpServletResponse对象
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=test.xlsx");
        ServletOutputStream out=response.getOutputStream();

        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值