java poi 导出excel异常如何抛到页面上
使用poi做的excel导出。现在遇到的问题是,如果发生异常,如何能够捕获异常后,传给页面,有一个弹出框,提示导出失败。如果没有异常就正常导出。
jsp中:
java 中处理
public String policyExportInfo(){
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session = request.getSession();
WrUser user = (WrUser) session.getAttribute("user");
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 = "";
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 ();
String rtnPage = this.messageParsingPage(returnValueDecompress);
if(rtnPage.contains(";")){
String strTemp[] = rtnPage.split(";");
if(strTemp.length>0){
policyList = this.messageParsingPolicy(returnValueDecompress);
}
} else {
response.reset();
response.setContentType("text/html charset=UTF-8");
response.getWriter().write("
response.getWriter().write("alert('导出失败,请重试');");
response.getWriter().write("");
// gcspException = "导出失败,请重试";
// logger.error(gcspException);
// return "gcspException";
}
if(policyList!= null){
HSSFWorkbook workbook;
workbook = exportExcelStyle(policyList);
ByteArrayOutputStream output = new ByteArrayOutputStream();
workbook.write(output);
output.flush();
byte[] ba = output.toByteArray();
inputStream = new ByteArrayInputStream(ba,0,ba.length);
output.close();
}else{
}
} catch (Exception e) {
response.reset();
response.setContentType("text/html charset=GBK");
try {
response.getWriter().write("
response.getWriter().write("alert('导出失败,请重试');");
response.getWriter().write("");
} catch (IOException e1) {
e1.printStackTrace();
}
logger.error(gcspException,e);
return "";
//gcspException = "导出失败,请重试";
// return "gcspException";
}
return "exportexcel";
}