昨天说了如何将数据导出成 excel 的两种方式,今天完善一下将 java 导出(文件在服务器)并下载到本地
1. 触发导出 ajax 代码
$.ajax({ type: "POST", 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. 处理导出的方法代码片段
List<ClUserinfo> regUsers = clSecurityService.findClUserinfos(clSecurityForm); List<TempUser> userList = new ArrayList<TempUser>(); for (ClUserinfo userinfo : regUsers) { TempUser user = new TempUser(); user.setUserName(userinfo.getUserName()); user.setUserType(userinfo.getUserType()); user.setCompany(userinfo.getCompany()); user.setKeshi(userinfo.getKeshi()); user.setProvince(userinfo.getProvince()); user.setCreatedDate(userinfo.getCreatedDate()); user.setCreateDateStr(userinfo.getCreatedDate().toString()); userList.add(user); } HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("注册用户表"); HSSFRow row = sheet.createRow((int) 0); HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCell cell = row.createCell((short) 0); cell.setCellValue("姓名"); cell.setCellStyle(style); cell = row.createCell((short) 1); cell.setCellValue("专委会"); cell.setCellStyle(style); cell = row.createCell((short) 2); cell.setCellValue("单位"); cell.setCellStyle(style); cell = row.createCell((short) 3); cell.setCellValue("科室"); cell.setCellStyle(style); cell = row.createCell((short) 4); cell.setCellValue("注册日期"); cell.setCellStyle(style); for (int i = 0; i < userList.size(); i++) { row = sheet.createRow((int) i + 1); TempUser user = (TempUser) userList.get(i); row.createCell((short) 0).setCellValue(user.getUserName()); row.createCell((short) 1).setCellValue(user.getUserType()); row.createCell((short) 2).setCellValue(user.getCompany()); row.createCell((short) 3).setCellValue(user.getKeshi()); row.createCell((short) 4).setCellValue(user.getCreateDateStr()); } try { String fileName = "RegistUserList-" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + ".xls"; String savaPath = ServletActionContext.getRequest().getRealPath("/upload/excel"); FileOutputStream fos = new FileOutputStream(savaPath + "\\" + fileName); wb.write(fos); fos.close(); Map<String,String> map = new HashMap<String,String>(); map.put("url", "/upload/excel/" + fileName); JSONObject json = JSONObject.fromObject(map); this.result = json.toString(); } catch (Exception e) { e.printStackTrace(); }
3. result 装返回结果
/** * json return */ private String result; public String getResult() { return result; }
public void setResult(String result) { this.result = result; }
4. XXXXXAction_XXXXXXX 方法以及 result 在 struts2 的配置
<action name="XXXXXAction_exportUserinfoData" class="com.gzewell.ucomweb.web.security.action.XXXXXAction" method="exportUserinfoData"> <result name="success" type="json"> <param name="root">result</param> </result> </action>
说明:
fos:文件输出流,将文件放在服务器的 /upload/excel 目录
result: 将路径放入map 结果以 json 的形式返回
window.open:打开新的页面,即为需要下载的文件在服务器的位置
${ctx}: 为服务器地址ip | 网址的表达式 (127.0.0.1)