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压缩包返回给客户端。

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?