poi 导出excel 异常处理方式--曲线救国法

时间:2023-03-10 07:20:53
poi 导出excel 异常处理方式--曲线救国法

excel 导出不算什么新鲜的话题。目前各种生成excel的开源jar包,poi,jxtl等。但是下载过程中如果出现异常该如何处理呢。

翻了之前的几个项目中的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";
}

  

以上是个人的一点小小愚见,欢迎拍砖!