vue excel导出_vue中复杂表头excel导出

vue代码:

export function exportExcel(id) {  return request({    url: '/admin/project/download/' + id,    method: 'get',    responseType: 'blob'  //二进制流  })};exportExcel(row) {  exportExcel(row.projectId).then(res => {    var blob = new Blob([res.data], {type: 'application/vnd.ms-excel;charset=utf-8'})    var url = window.URL.createObjectURL(blob);    var aLink = document.createElement("a");    aLink.style.display = "none";    aLink.download = row.projectName + '报名表.xlsx';    aLink.href = url;    document.body.appendChild(aLink)    aLink.click()  })}

excel模板:

注意list中是{.xxx},其他地方是{xxx}

42a1fa8ffb400143a5cf5b92c953cc06.png

java代码:

@Datapublic class DownloadData {private String serialNumber;private String teacherNum;private String realName;private String sex;private String orgName;private String subjectTeaching;private String phoneNumber;private String idNum;}List list = ...;String templateFileName = FileUtil.getPath() + "excel" + File.separator + "complex.xlsx";response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf-8");// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系String fileName = null;try {fileName = URLEncoder.encode(UUID.randomUUID().toString(), "UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");ServletOutputStream outputStream = null;try {outputStream = response.getOutputStream();Collections.reverse(list);ExcelWriter excelWriter = EasyExcel.write(outputStream, DownloadData.class).withTemplate(templateFileName).build();WriteSheet writeSheet = EasyExcel.writerSheet().build();FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();excelWriter.fill(list, fillConfig, writeSheet);// 替换表头中的{projectName}, 注意这里没有.Map titleMap = new HashMap<>(1);Project project = projectService.getById(projectId);titleMap.put("projectName", project.getProjectName());excelWriter.fill(titleMap, writeSheet);excelWriter.finish();} catch (IOException e) {e.printStackTrace();} finally {if (outputStream !=null) {try {outputStream.close();} catch (IOException e) {e.printStackTrace();}}}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值