乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > jquery <em>ajax</em>调用后台导出<em>excel</em

jquery <em>ajax</em>调用后台导出<em>excel</em

作者:乔山办公网日期:

返回目录:excel表格制作


这个一般是后台做的,后台生成excel文件返回前端下载。
主要原因是虽然有这种插件(ExcellentExport.js、exceljs、js-xlsx等等),但是受到jquery版本和浏览器支持的限制,处理起来也比后端处理复杂。而且一旦数据量大起来浏览器是承受不了的zd

7a686964616fe4b893e5b19e361public static void queryexcel(String filename,HttpServletRequest request,HttpServletResponse response) throws Exception {
  HSSFWorkbook workbook = null;
  //读取表头(表字段名)
  String[] titles = (String[]) request.getAttribute("titles");
  //读取数据库数据
  String [] info=(String[]) request.getAttribute("info");
  // 创建工作簿实例
  workbook = new HSSFWorkbook();
  HSSFSheet sheet = workbook.createSheet("sheet1");
  workbook.getSheet(null);

  HSSFFont font = workbook.createFont();
  font.setFontName(HSSFFont.FONT_ARIAL);
  font.setFontHeightInPoints((short)14);
  HSSFCellStyle style = workbook.createCellStyle();
  style.setFont(font);
  style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中
  style.setWrapText(true);
  //加载表头数据
  HSSFRow titleRow = sheet.createRow(0);
  for (int i = 0; i < titles.length; i++) {
  HSSFCell cell = titleRow.createCell((short) i);
  cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  //设置编码
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue(titles[i].toString());
  }

  if (info != null) {
  HSSFRow dataRow = sheet.createRow(1);
  for (int i = 0; i < info.length; i++) {
  HSSFCell cell = dataRow.createCell((short)i);
  sheet.setColumnWidth((short)i, (short)3500);
  cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  //设置编码
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue(info[i]);
  }
  }

  //通过数据流下载工作簿
  try {
  OutputStream out = response.getOutputStream();
  response.setHeader("Content-disposition",
  "attachment;filename="+toUtf8String(filename+".xls"));
  response.setContentType("text ml;charset=utf-8");
  request.setCharacterEncoding("UTF-8");
  workbook.write(out);
  out.flush();
  out.close();
  } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  }

  //中文名
  public static String toUtf8String(String s){
  StringBuffer sb = new StringBuffer();
  for (int i=0;i<s.length();i++){
  char c = s.charAt(i);
  if (c >= 0 && c <= 255){sb.append(c);}
  else{
  byte[] b;
  try { b = Character.toString(c).getBytes("UTF-8");}
  catch (Exception ex) {
  System.out.println(ex);
  b = new byte[0];
  }
  for (int j = 0; j < b.length; j++) {
  int k = b[j];
  if (k < 0) k += 256;
  sb.append("%" + Integer.toHexString(k).toUpperCase());
  }
  }
  }
  return sb.toString();
  }

Excel导出是无法用ajax的,只能使用window.location.href

会的。当你发出请求本质就是建立了和服务器的socket通信。
你把请求发出去,action还没有返回数据,你又请求了另外的页面。这个会将之前的连接关闭,那么action无法返回数据到页面。

如果你想确定发送数据出去,action返回,前台选择保存或者取消。
那么最好在点击了下载文件功能后,将网页设置遮罩层,提示文件正在请求中。这样用户就无法点击其他操作。当action返回数据后,再将网页遮罩层取消即可!

相关阅读

  • jquery <em>ajax</em>调用后台导出<em>excel</em

  • 乔山办公网excel表格制作
  • 这个一般是后台抄做的,后台生成excel文件返回前端下载。主要原因是虽然有这种插件(ExcellentExport.js、袭exceljs、js-xlsx等等),但是受到jquery版本和浏览器支持的限制,处理起来也比后
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网