spring MVC 导出excel

  1. // 导出excel方法  
  2.  @RequestMapping("exportExcel")  
  3.  public void exportExcel(HttpServletRequest request, HttpServletResponse response)  
  4.  {  
  5.      HttpSession session = request.getSession();  
  6.      session.setAttribute("state"null);  
  7.      // 生成提示信息,  
  8.      response.setContentType("application/vnd.ms-excel");  
  9.      String codedFileName = null;  
  10.      OutputStream fOut = null;  
  11.      try  
  12.      {  
  13.          // 进行转码,使其支持中文文件名  
  14.          codedFileName = java.net.URLEncoder.encode("中文""UTF-8");  
  15.          response.setHeader("content-disposition""attachment;filename=" + codedFileName + ".xls");  
  16.          // response.addHeader("Content-Disposition", "attachment;   filename=" + codedFileName + ".xls");  
  17.          // 产生工作簿对象  
  18.          HSSFWorkbook workbook = new HSSFWorkbook();  
  19.          //产生工作表对象  
  20.          HSSFSheet sheet = workbook.createSheet();  
  21.          for (int i = 0; i <= 30000; i++)  
  22.          {  
  23.              HSSFRow row = sheet.createRow((int)i);//创建一行  
  24.              HSSFCell cell = row.createCell((int)0);//创建一列  
  25.              cell.setCellType(HSSFCell.CELL_TYPE_STRING);  
  26.              cell.setCellValue("测试成功" + i);  
  27.          }  
  28.          fOut = response.getOutputStream();  
  29.          workbook.write(fOut);  
  30.      }  
  31.      catch (UnsupportedEncodingException e1)  
  32.      {}  
  33.      catch (Exception e)  
  34.      {}  
  35.      finally  
  36.      {  
  37.          try  
  38.          {  
  39.              fOut.flush();  
  40.              fOut.close();  
  41.          }  
  42.          catch (IOException e)  
  43.          {}  
  44.          session.setAttribute("state""open");  
  45.      }  
  46.      System.out.println("文件生成...");  
  47.  }  
  48.  @RequestMapping("check")  
  49.  public void check(HttpServletRequest request, HttpServletResponse response)  
  50.  {  
  51.      try  
  52.      {  
  53.          if ("open".equals(request.getSession().getAttribute("state")))  
  54.          {  
  55.              request.getSession().setAttribute("state"null);  
  56.              response.getWriter().write("true");  
  57.              response.getWriter().flush();  
  58.          }  
  59.          else  
  60.          {  
  61.              response.getWriter().write("false");  
  62.              response.getWriter().flush();  
  63.          }  
  64.      }  
  65.      catch (IOException e)  
  66.      {}  
  67.  }  


 

Js代码 复制代码  收藏代码
  1. /***********导出start************/  
  2.     var excel_flag = 0;  
  3.     var win_check;  
  4.     var exportExcelBtn = new Ext.Button({  
  5.         renderTo:'exportExcelBtn',  
  6.         text:"<span class='marL10'>"+'导出'+"</span>",  
  7.         height:24,  
  8.         iconCls:'findnew',  
  9.         width:110,  
  10.         bodyStyle:'padding:5px',  
  11.         handler: function()  
  12.         {  
  13.             excel_flag = 0;  
  14.             //禁用按钮  
  15.             exportExcelBtn.disable();  
  16.             location.href = "exportExcel";  
  17.             //每隔一秒向后台发送请求  
  18.             win_check = window.setInterval(check, 1000);    
  19.         }  
  20.     });  
  21.       
  22.     /** 
  23.      * 用于防止重复提交 
  24.      */  
  25.     function check()  
  26.     {  
  27.         excel_flag ++;  
  28.         if(excel_flag > 30)  
  29.         {  
  30.             //清空定时器  
  31.             window.clearInterval(win_check);  
  32.             //启用按钮  
  33.             exportExcelBtn.enable();  
  34.         }  
  35.         Ext.Ajax.request(  
  36.             {  
  37.                 url : 'check',  
  38.                 success : function (response, result)  
  39.                 {  
  40.                     if(response.responseText=="true")  
  41.                     {  
  42.                         //清空定时器  
  43.                         window.clearInterval(win_check);  
  44.                         //启用按钮  
  45.                         exportExcelBtn.enable();  
  46.                     }  
  47.                 }  
  48.             })  
  49.     }  
  50.     /***********导出end*****************/  

 

你可能感兴趣的:(spring MVC 导出excel)