poi 导出excel 异常处理方式--曲线救国法
2014-06-23 13:13
1001 查看
excel 导出不算什么新鲜的话题。目前各种生成excel的开源jar包,poi,jxtl等。但是下载过程中如果出现异常该如何处理呢。
翻了之前的几个项目中的excel导出,有的异常就直接抛了出去,有的打印了异常。Action 中异常已经在最顶层了,抛出显然不是明智之举。但是捕获异常之后不做任何处理,用户体验也不好。有人一定会问,那还不好办,定义一个异常标识,捕获异常之后传给页面,提示用户;没有异常就正常导出就好了吗!问题就在这里。我第一次的js的写法
js定向到这个地址:然后执行一系列的excel组装操作,最后得到一个inputstream,然后我是用struts2的type=stream,直接将这个流输出。这是一个无返回值的操作,我根本得不到返回值。
那第二个方法,使用ajax。
这样子可已接受到返回值了吧!但问题是,如果导出成功,excel的inputstream也同样呗当做流返回回来了。查了一些资料使用ajax导出是不行的。
然后我想,如果是没有返回值的ajax,是不是行呢。
我在java 里面把异常信息print出来。
结果是否定的,不行。
折腾了2天之后,决定尝试最后一种方法,不行就放弃。
在java中定义一个标识,标志导出成功或者失败,如果导出成功就将这个excel存在服务器上(其实随便存在哪里都行了)。返回给页面的是这个标识。如果是导出成功的,执行下载操作,下载之后这个文件删除;
有人会问:那下载中的异常怎么办。在同一台服务器上,除非服务器挂了,否则下载失败是小概率事件,我觉得这个是可以忽略的。但是生成excel不同。这期间包含系统的逻辑处理,同时还会有一些跨服务器的访问,你这了没问题,难免别人不会有问题。
具体代码片段如下:
jsp中:
java 中:
以上是个人的一点小小愚见,欢迎拍砖!
翻了之前的几个项目中的excel导出,有的异常就直接抛了出去,有的打印了异常。Action 中异常已经在最顶层了,抛出显然不是明智之举。但是捕获异常之后不做任何处理,用户体验也不好。有人一定会问,那还不好办,定义一个异常标识,捕获异常之后传给页面,提示用户;没有异常就正常导出就好了吗!问题就在这里。我第一次的js的写法
window.location.href="policyExportInfo.action";
js定向到这个地址:然后执行一系列的excel组装操作,最后得到一个inputstream,然后我是用struts2的type=stream,直接将这个流输出。这是一个无返回值的操作,我根本得不到返回值。
那第二个方法,使用ajax。
$.ajax({ url: "policyExportInfo.action", success: function(returnData){ if(returnData==0){ alert("导出失败,请重试"); } });
这样子可已接受到返回值了吧!但问题是,如果导出成功,excel的inputstream也同样呗当做流返回回来了。查了一些资料使用ajax导出是不行的。
然后我想,如果是没有返回值的ajax,是不是行呢。
我在java 里面把异常信息print出来。
response.reset(); response.setContentType("text/html charset=GBK"); try { response.getWriter().write("<script>"); response.getWriter().write("alert('导出失败,请重试');"); response.getWriter().write("</script>"); } catch (IOException e1) { e1.printStackTrace(); }
结果是否定的,不行。
折腾了2天之后,决定尝试最后一种方法,不行就放弃。
在java中定义一个标识,标志导出成功或者失败,如果导出成功就将这个excel存在服务器上(其实随便存在哪里都行了)。返回给页面的是这个标识。如果是导出成功的,执行下载操作,下载之后这个文件删除;
有人会问:那下载中的异常怎么办。在同一台服务器上,除非服务器挂了,否则下载失败是小概率事件,我觉得这个是可以忽略的。但是生成excel不同。这期间包含系统的逻辑处理,同时还会有一些跨服务器的访问,你这了没问题,难免别人不会有问题。
具体代码片段如下:
jsp中:
$.ajax({
url: "policyExportInfo.action",
success: function(returnData){
if(returnData==0){
alert("导出失败,请重试");
}else{
window.location.href="policyExportInfo.action";}
}
});
java 中:
public String policyExportInfo(){ HttpServletResponse response = ServletActionContext.getResponse(); HttpSession session = request.getSession(); WrUser user = (WrUser) session.getAttribute("user"); String userCode = user.getUserCode(); String dirPath=""; String excelPath = ""; PubTools pTool = new PubTools(); try { dirPath= pTool.getUrlBykey("sysconfig.filePath"); } catch (IOException e2) { e2.printStackTrace(); } excelPath = dirPath + "//" + userCode + new DateTime(new Date(), new DateTime().YEAR_TO_DAY);//导出excel存放的路径 logger.info(excelPath); File uploadFilePath = new File(excelPath); // 如果该目录不存在,则创建之 if (uploadFilePath.exists() == false) { uploadFilePath.mkdirs(); } String as = excelPath + "//" + "承保查询"+new DateTime(new Date(), new DateTime().YEAR_TO_DAY)+".xls"; session.setAttribute("exportFile", as);//下载文件 File myFilePath = new File(as); if (!myFilePath.exists()) { GcspPolicy policy = (GcspPolicy) session.getAttribute("policy");//获取查询对象 if(user!=null&&policy!=null){ policy.setThisComCode(user.getComCode()); } custType = user.getCustType(); String reqXML = this.getXML(custType,"POLICYEXPORT",policy); GcspServerClient client = new GcspServerClient(); // String xmlData1="E:\\policyReturn.txt"; // String returnValue = this.readFile(xmlData1); String returnValue = ""; String returnValueDecompress = ""; response.setContentType("text/html; charset=UTF-8"); ServletOutputStream out = null; try { out = response.getOutputStream(); } catch (IOException e1) { e1.printStackTrace(); } try { //总线保存采用GZIP压缩,并用base64转码;接收到总线返回报文后,需先使用base64解码,并解压缩 returnValue = client.requestXML(reqXML);//获取报文 byte[] b = new sun.misc.BASE64Decoder().decodeBuffer(returnValue);//使用BASE64解码 byte[] b1= client.decompress(b);//解压缩 StringUtil s= new StringUtil(); returnValueDecompress = s.byteToChar(b1, "GBK"); policyList = new ArrayList<GcspPolicy> (); String rtnPage = this.messageParsingPage(returnValueDecompress); if(rtnPage.contains(";")){ String strTemp[] = rtnPage.split(";"); if(strTemp.length>0){ policyList = this.messageParsingPolicy(returnValueDecompress); } } else { out.print("0"); return "none"; } if(policyList!= null){ HSSFWorkbook workbook; workbook = exportExcelStyle(policyList); FileOutputStream output = new FileOutputStream(as); // 输出流 //ByteArrayOutputStream output = new ByteArrayOutputStream(); workbook.write(output); output.flush(); //byte[] ba = output.toByteArray(); //inputStream = new ByteArrayInputStream(ba,0,ba.length); output.close(); }else{ out.print("0"); return "none"; } } catch (Exception e) { try { out.print("0"); return "none"; } catch (IOException e1) { e1.printStackTrace(); } } try { out.print("1"); return "none"; } catch (IOException e) { e.printStackTrace(); } }else{ //下载 String exportFile = (String) session.getAttribute("exportFile"); try { Downfile downfile = new Downfile(); downfile.downfileByPath(response, request, exportFile); downfile.deleteDirectory(excelPath);// 删除excel 文件夹 downfile.deleteFile(exportFile);// 删除生成的压缩文件 } catch (IOException e) { e.printStackTrace(); } } return "none"; }
以上是个人的一点小小愚见,欢迎拍砖!
相关文章推荐
- 从GridView中直接导出数据到Excel文件 处理导出乱码 类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常
- 用poi处理excel文件异常:java.io.IOException: Invalid header signature; read 2338042707334752323
- poi导出Excel 不会产生垃圾文件的导出方式
- Java Web的Excel导出处理(基于POI)
- 170313、poi:采用自定义注解的方式导入、导出excel(这种方式比较好扩展)
- org.apache.poi.hssf.usermodel.HSSFWorkbook 进行处理导出Excel操作
- 从GridView中直接导出数据到Excel文件 处理导出乱码 类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常
- GridView导出成Excel字符"0"丢失/数字丢失的处理方式 收藏
- POI 导出Excel 时间格式和小数点数据的处理
- Struts2使用Displaytag导出Excel,抛出nullException异常的处理
- 用POI导出excel时,较长的数字不想被自动变为科学计数法的解决方式(转)
- 用POI导出excel时,较长的数字不想被自动变为科学计数法的解决方式
- 利用POI框架的SAX方式处理大数据2007版Excel(xlsx)
- 用POI导出excel时,较长的数字不想被自动变为科学计数法的解决方式
- poi 方式导出excel
- java操作excel异常处理-poi处理excel异常
- 从GridView中直接导出数据到Excel文件 处理导出乱码 类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常
- springMVC中使用POI方式导出excel至客户端、服务器实例
- Java导出数据为EXCEL的两种方式JXL和POI
- SpringMVC+POI 实现Excel按模板方式简单导出功能