weixin_40581872 2017-10-11 07:48 采纳率: 0%
浏览 3962

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条回答

  • lockemn 2017-10-11 09:52
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘