- @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)
- {}
- }
- /***********導出start************/
- var excel_flag = 0;
- var win_check;
- var exportExcelBtn = new Ext.Button({
- renderTo:'exportExcelBtn',
- text:"<span class='marL10'>"+'導出'+"</span>",
- height:24,
- iconCls:'findnew',
- width:110,
- bodyStyle:'padding:5px',
- handler: function()
- {
- excel_flag = 0;
- //禁用按鈕
- exportExcelBtn.disable();
- location.href = "exportExcel";
- //每隔一秒向後臺發送請求
- win_check = window.setInterval(check, 1000);
- }
- });
- /**
- * 用於防止重複提交
- */
- function check()
- {
- excel_flag ++;
- if(excel_flag > 30)
- {
- //清空定時器
- window.clearInterval(win_check);
- //啓用按鈕
- exportExcelBtn.enable();
- }
- Ext.Ajax.request(
- {
- url : 'check',
- success : function (response, result)
- {
- if(response.responseText=="true")
- {
- //清空定時器
- window.clearInterval(win_check);
- //啓用按鈕
- exportExcelBtn.enable();
- }
- }
- })
- }
- /***********導出end*****************/