spring MVC 導出excel


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

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*****************/  
發佈了4 篇原創文章 · 獲贊 16 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章