先简单描述下问题情况:jsp 导出excel 时, 只要不用下载工具直接下载(如下图)一切正常,但当使用下载工具时就会出现两次下载并每次下载内容为null,在DownExcel.jsp中输出System.out.println(body);当下载提示刚打开时有内容输出,而点击下载后输出为null。用超连接测试过能正常使用,但当内容比较多时,超连接地址长度有限内容不能完全显示。代码如下:
excelTest.jsp<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=GBK">
</head>
<body> 
<form id="frm1" action="DownExcel.jsp" target="_blank" style="margin:0;padding:0;" method="POST">
<input type="hidden" name="body" value="" />
</form>
<script type="text/javascript">
function downXLS() {
    // 把表格内容用表单 POST 到 DownXLS.jsp
    var tab = document.getElementById("tab1");
    var frm = document.getElementById("frm1");
    frm.body.value = tab.innerHTML;
    frm.submit();
}
</script>
<table id="tab1" border="1" cellspacing="1" cellpadding="1">  
     <tr><td colspan="5" align="center">WEB页面导出为EXCEL文档的方法 </td></tr>  
     <tr>  
         <td>列标题1 </td>  
         <td>列标题2 </td>  
         <td>列标题3 </td>  
         <td>列标题4 </td>  
         <td>列标题5 </td>  
     </tr>  
     <tr>  
         <td>aaa </td>  
         <td>bbb </td>  
         <td>ccc </td>
         <td>ddd </td>
         <td>eee </td>
     </tr>
     <tr>
         <td>AAA </td>
         <td>BBB </td>
         <td>CCC </td>
         <td>DDD </td>
         <td>EEE </td>
     </tr>
     <tr>
         <td>FFF </td>
         <td>GGG </td>
         <td>HHH </td>
         <td>III </td>
         <td>JJJ </td>
     </tr>
</table>
<input type="button" value="down" onclick="javascript: downXLS();" />
</body>
</html>
DownExcel.jsp
<%@ page language="java" pageEncoding="GBK"%>
<%
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment; filename=stat.xls");
request.setCharacterEncoding("GBK");
String body = request.getParameter("body");
//System.out.println(body);
%>
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40"><head>
<meta http-equiv=Content-Type content="text/html; charset=GBK">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 11">
<!--[if gte mso 9]><xml>
 <x:ExcelWorkbook>
  <x:ExcelWorksheets>
   <x:ExcelWorksheet>
    <x:Name>Sheet1</x:Name>
    <x:WorksheetOptions>
     <x:DefaultRowHeight>285</x:DefaultRowHeight>
     <x:Selected/>
     <x:Panes>
      <x:Pane>
       <x:Number>3</x:Number>
       <x:ActiveRow>2</x:ActiveRow>
       <x:ActiveCol>1</x:ActiveCol>
      </x:Pane>
     </x:Panes>
     <x:ProtectContents>False</x:ProtectContents>
     <x:ProtectObjects>False</x:ProtectObjects>
     <x:ProtectScenarios>False</x:ProtectScenarios>
    </x:WorksheetOptions>
   </x:ExcelWorksheet>
  </x:ExcelWorksheets>
  <x:WindowHeight>9090</x:WindowHeight>
  <x:WindowWidth>11715</x:WindowWidth>
  <x:WindowTopX>240</x:WindowTopX>
  <x:WindowTopY>90</x:WindowTopY>
  <x:ProtectStructure>False</x:ProtectStructure>
  <x:ProtectWindows>False</x:ProtectWindows>
 </x:ExcelWorkbook>
</xml><![endif]-->
</head>
<body>
<table border="1" cellspacing="1" cellpadding="1">
<% out.print(body); %>
</table>
</body>
</html>