java批量数据导出多个excel,为什么老是只生成一个excel文件

public void exportexcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 为模板中变量赋值
// 传递模板地址和要操作的页签
ExcelExp excel = new XssExcelExp("../../excel/volunteer.xlsx", 0);
VolunteerFormMap volunteerFormMap = findHasHMap(VolunteerFormMap.class);
volunteerFormMap.put("mapper_id","VolunteerMapper.findvolunteer");
List> lists = baseService.findByAll(volunteerFormMap);
for (Map map2 : lists) {
Map map1 = new HashMap();
map1.put("name", (String) map2.get("name"));
map1.put("gender", (String) map2.get("gender"));
map1.put("education", (String) map2.get("education"));
map1.put("politicalAff", (String) map2.get("political_aff"));
map1.put("workUnit", (String) map2.get("work_unit"));
map1.put("unitAddress", (String) map2.get("unit_address"));
map1.put("address", (String) map2.get("address"));
map1.put("phoneNumber", (String) map2.get("phoneNumber"));
map1.put("idNumber", (String) map2.get("id_number"));
map1.put("hobby", (String) map2.get("hobby"));
map1.put("contactInfo", (String) map2.get("contact_info"));
map1.put("introducer", (String) map2.get("introducer"));
map1.put("partinTime", (String) map2.get("partin_time"));
excel.replaceExcelData(map1);

// 导出,此处只封装了浏览器下载方式
// 调用downloadExcel,返回输出流给客户端
String fileName = (String) map2.get("name") + ".xlsx";
excel.downloadExcel(response, fileName);
}
}

public void downloadExcel(HttpServletResponse response, String filaName) throws IOException {
String encodeFileName = URLEncoder.encode(filaName, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + encodeFileName);
//response.setHeader("Accept-Ranges", "bytes");
ServletOutputStream out = response.getOutputStream();
xssWb.write(out);
out.flush();
out.close();
}

1个回答

java servlet一个请求,只能有一个返回输出流,所以一次只能下载一个文件。
如果确实需要下载多个Excel文件,可以先在本地生成多个Excel文件,然后使用zip压缩,再通过一个返回输出流将zip压缩包返回给客户端。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问