要实现页面中的table导出成Excel格式,如果用jxl的API,未免太麻烦了些,这里我介绍一下我们项目中使用的简单的导出。     首先,介绍一下原理,Excel可以打开html文件这个大家应该都知道吧?(这个你都不知道?呵呵  )同样道理,一个只有table元素的html文件也可以在Excel中打开的,因此,我们可以利用页面中的Form的提交,把页面中的table内容提交给一个jsp或servlet,然后由jsp或servlet把提交上来的table内容转变成文件流的形式,以文件下载形式传递给客户端,由此完成导出Excel功能。     以下是查询jsp页面部分代码: <script> function saveResult() {  var idx=document.all.userid.selectedIndex;  if (eval(document.resultFrame.resTbl)==null)  {   alert("请先查询后再保存!");   return false;  }  var file="日告警查询_"+document.all.datetime.value   +"_"+document.all.userid.options[idx].text;  document.all.name.value=file+".xls";    document.all.txt.value="<style>body,table {font-family: \"宋体\";font-size: 9pt}</style>"+document.resultFrame.resTbl.innerHTML;  warnForm.action="/downloadResult.do";  warnForm.target="saveFrm";  warnForm.submit();  document.all.name.value=file; } </script> <html:form action="/warnDayQuery.do" target="resultFrame" >   <input type="hidden" name="name" value=""/>   <input type="hidden" name="txt" value=""/> </html:form> ... <!--       仅仅提供保存功能的iframe //--> <iframe name="saveFrm" src="" frameborder="0" scrolling="no" width="0" height="0"> </iframe>   ... <!--      查询结果 //--> <iframe name="resultFrame" src="" frameborder="0" scrolling="yes" width="100%" height="500"> </iframe>
      下面是查询结果的jsp部分代码: //关键就是这个id号,可以代表要导出的table <span id="resTbl">  <logic:notPresent name="dayResult" scope="session">          查询失败!   </logic:notPresent> <table width="100%" height="10%" border="1" align="center" cellpadding="1" cellspacing="0"  bgcolor="#EEECF2" bordercolor="#A3B2CC">  <logic:present name="dayResult" scope="session">   ...       下面是作为中转的下载jsp服务代码: <%@ page         language="java"         contentType="text/html; charset=GBK"         import="java.io.*,java.net.URL,java.util.*" %> <% String file =null; StringBuffer sb=null; for (Enumeration e = request.getParameterNames(); e.hasMoreElements(); ) {       String name = (String) e.nextElement();       //System.out.println(" >>Name:  "+  name);       //System.out.println(" >>>Parameter:   "+  request.getParameter(name));       if(name.equals("name"))        file=request.getParameter(name);       if(name.equals("txt"))        sb=new StringBuffer(request.getParameter(name));   }
try {  file = new String(file.getBytes("ISO-8859-1"), "gb2312"); } catch (UnsupportedEncodingException e) {  out.println("文件名解析失败!");  return; } 
String extName = file.substring(file.lastIndexOf(".")); String contentType; if (extName == null || extName.equals("")) contentType = ""; else if (extName.equalsIgnoreCase(".chm")) contentType = "application/msword"; else if (extName.equalsIgnoreCase(".doc")) contentType = "application/msword"; else if (extName.equalsIgnoreCase(".xls")) contentType = "application/vnd.ms-excel"; else if (extName.equalsIgnoreCase(".ppt")) contentType = "application/vnd.ms-powerpoint"; else if (extName.equalsIgnoreCase(".txt")) contentType = "text/plain"; else contentType = "application/octet-stream"; 
// "inline" or "attachment" (default) /*String mode = request.getParameter("mode"); recommendedName =  request.getParameter("recommendedName"); if (mode == null || mode.length() == 0) mode = "attachment"; */   String recommendedName =  new String(file.getBytes(),"iso_8859_1");   response.setContentType(contentType);   response.setHeader("Content-Disposition", "attachment; filename=" + recommendedName + "\"");      response.resetBuffer();   ServletOutputStream sos = response.getOutputStream();   /*   String s = null;   while ((s=br.readLine())!=null) {       sos.print(s);   }   */   sos.write(sb.toString().getBytes("ISO-8859-1"));   sos.flush();   sos.close(); %>
  你也可以做成servlet形式,怎么样,这样的Excel导出是不是很简单阿?  
  
 
  |