springmvc 导出excel

  1. // 导出excel方法 
  2. @RequestMapping("exportExcel"
  3. public void exportExcel(HttpServletRequest request, HttpServletResponse response) 
  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. @RequestMapping("check"
  47. public void check(HttpServletRequest request, HttpServletResponse response) 
  48.      try 
  49.      { 
  50.          if ("open".equals(request.getSession().getAttribute("state"))) 
  51.          { 
  52.              request.getSession().setAttribute("state", null); 
  53.              response.getWriter().write("true"); 
  54.              response.getWriter().flush(); 
  55.          } 
  56.          else 
  57.          { 
  58.              response.getWriter().write("false"); 
  59.              response.getWriter().flush(); 
  60.          } 
  61.      } 
  62.      catch (IOException e) 
  63.      {} 
   // 导出excel方法
    @RequestMapping("exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response)
    {
        HttpSession session = request.getSession();
        session.setAttribute("state", null);
        // 生成提示信息,
        response.setContentType("application/vnd.ms-excel");
        String codedFileName = null;
        OutputStream fOut = null;
        try
        {
            // 进行转码,使其支持中文文件名
            codedFileName = java.net.URLEncoder.encode("中文", "UTF-8");
            response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
            // response.addHeader("Content-Disposition", "attachment;   filename=" + codedFileName + ".xls");
            // 产生工作簿对象
            HSSFWorkbook workbook = new HSSFWorkbook();
            //产生工作表对象
            HSSFSheet sheet = workbook.createSheet();
            for (int i = 0; i <= 30000; i++)
            {
                HSSFRow row = sheet.createRow((int)i);//创建一行
                HSSFCell cell = row.createCell((int)0);//创建一列
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue("测试成功" + i);
            }
            fOut = response.getOutputStream();
            workbook.write(fOut);
        }
        catch (UnsupportedEncodingException e1)
        {}
        catch (Exception e)
        {}
        finally
        {
            try
            {
                fOut.flush();
                fOut.close();
            }
            catch (IOException e)
            {}
            session.setAttribute("state", "open");
        }
        System.out.println("文件生成...");
    }
    @RequestMapping("check")
    public void check(HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            if ("open".equals(request.getSession().getAttribute("state")))
            {
                request.getSession().setAttribute("state", null);
                response.getWriter().write("true");
                response.getWriter().flush();
            }
            else
            {
                response.getWriter().write("false");
                response.getWriter().flush();
            }
        }
        catch (IOException e)
        {}
    }


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*****************/ 

猜你喜欢

转载自blog.csdn.net/superiorpengFight/article/details/43303361