当前位置: 代码迷 >> Java Web开发 >> java poi 导出excel错误怎么抛到页面上
  详细解决方案

java poi 导出excel错误怎么抛到页面上

热度:489   发布时间:2016-04-16 22:16:45.0
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<GcspPolicy> ();
        
        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("<script>");
         response.getWriter().write("alert('导出失败,请重试');");
         response.getWriter().write("</script>");
//             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("<script>");
response.getWriter().write("alert('导出失败,请重试');");
         response.getWriter().write("</script>");
} catch (IOException e1) {
e1.printStackTrace();
}
          logger.error(gcspException,e);
         return "";
// gcspException = "导出失败,请重试";
           
//            return "gcspException";
}
     return "exportexcel";
    }
  相关解决方案