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.  }  
   // 导出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*****************/  

 欢迎访问我的技术群425783133

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值