昨天说了如何将数据导出成 excel 的两种方式,今天完善一下将 java 导出(文件在服务器)并下载到本地

 

1. 触发导出 ajax 代码

  1. $.ajax({
  2. type: "POST",
  3. url: "${ctx}/website/clsecurity/XXXXXAction_exportUserinfoData.do",
               async:
    false,
               dataType:
    "json",
               data: {
                  "province": province,
                  "userType": userType,
                  "startDate": startDate,
                  "endDate": endDate
              },
              success:
    function(data) {
                
    var json = eval( \'(\' + data + \')\' );
                window.open(
    "${ctx}" + json.url);
              }
           });

 

2. 处理导出的方法代码片段

  1.        List<ClUserinfo> regUsers = clSecurityService.findClUserinfos(clSecurityForm);
  2. List<TempUser> userList = new ArrayList<TempUser>();
  3. for (ClUserinfo userinfo : regUsers) {
  4. TempUser user = new TempUser();
  5. user.setUserName(userinfo.getUserName());
  6. user.setUserType(userinfo.getUserType());
  7. user.setCompany(userinfo.getCompany());
  8. user.setKeshi(userinfo.getKeshi());
  9. user.setProvince(userinfo.getProvince());
  10. user.setCreatedDate(userinfo.getCreatedDate());
  11. user.setCreateDateStr(userinfo.getCreatedDate().toString());
  12. userList.add(user);
  13. }
  14. HSSFWorkbook wb = new HSSFWorkbook();
  15. HSSFSheet sheet = wb.createSheet("注册用户表");
  16. HSSFRow row = sheet.createRow((int) 0);
  17. HSSFCellStyle style = wb.createCellStyle();
  18. style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  19. HSSFCell cell = row.createCell((short) 0);
  20. cell.setCellValue("姓名");
  21. cell.setCellStyle(style);
  22. cell = row.createCell((short) 1);
  23. cell.setCellValue("专委会");
  24. cell.setCellStyle(style);
  25. cell = row.createCell((short) 2);
  26. cell.setCellValue("单位");
  27. cell.setCellStyle(style);
  28. cell = row.createCell((short) 3);
  29. cell.setCellValue("科室");
  30. cell.setCellStyle(style);
  31. cell = row.createCell((short) 4);
  32. cell.setCellValue("注册日期");
  33. cell.setCellStyle(style);
  34. for (int i = 0; i < userList.size(); i++) {
  35. row = sheet.createRow((int) i + 1);
  36. TempUser user = (TempUser) userList.get(i);
  37. row.createCell((short) 0).setCellValue(user.getUserName());
  38. row.createCell((short) 1).setCellValue(user.getUserType());
  39. row.createCell((short) 2).setCellValue(user.getCompany());
  40. row.createCell((short) 3).setCellValue(user.getKeshi());
  41. row.createCell((short) 4).setCellValue(user.getCreateDateStr());
  42. }
  43. try {
  44. String fileName = "RegistUserList-" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + ".xls";
  45. String savaPath = ServletActionContext.getRequest().getRealPath("/upload/excel");
  46. FileOutputStream fos = new FileOutputStream(savaPath + "\\" + fileName);
  47. wb.write(fos);
  48. fos.close();
  49. Map<String,String> map = new HashMap<String,String>();
  50. map.put("url", "/upload/excel/" + fileName);
  51. JSONObject json = JSONObject.fromObject(map);
  52. this.result = json.toString();
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }

 

3. result 装返回结果

  1.   /**
  2. * json return
  3. */
  4. private String result;
  5. public String getResult() {
  6. return result;
  7. }
    public void setResult(String result) {
  8. this.result = result;
  9. }

 

4. XXXXXAction_XXXXXXX 方法以及 result 在 struts2 的配置

  1.     <action name="XXXXXAction_exportUserinfoData" class="com.gzewell.ucomweb.web.security.action.XXXXXAction" method="exportUserinfoData">
  2.        <result name="success" type="json">
  3.          <param name="root">result</param>
  4.        </result>
  5.     </action>

 

 

说明:

  fos:文件输出流,将文件放在服务器的 /upload/excel 目录

  result: 将路径放入map 结果以 json 的形式返回

   window.open:打开新的页面,即为需要下载的文件在服务器的位置

   ${ctx}: 为服务器地址ip | 网址的表达式 (127.0.0.1) 

  

 

 

版权声明:本文为linnuo原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/linnuo/p/6739562.html