欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > 导出excel 到浏览器下载中

导出excel 到浏览器下载中

2024/10/25 0:35:28 来源:https://blog.csdn.net/weixin_46399670/article/details/140798969  浏览:    关键词:导出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_ddif (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);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com