导出excel 到浏览器下载中

avatar
作者
筋斗云
阅读量:0

前端方法:

/*后台流导出*/   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); 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!