给你一段代码自己改一改吧,我来不及改了:)
希望对你有用!<jsp:useBean class="demo_project.Get_Data" id="user" scope="session"></jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>显示数据</title>
<script language="javascript">
var excelObj;
var $Table;
function printTable(talbleId){
  var rows,cols;
  var i,j;
  $Table=document.all(talbleId);
  rows=$Table.rows.length;
  if(rows>0){
   try{
    cols=$Table.rows(0).children.length;
    excelObj = new ActiveXObject("Excel.Application");
    excelObj.Visible = true;
    excelObj.WorkBooks.Add;
    for(i=1;i<=rows;i++)
     for(j=1;j<=cols;j++){
       showInExcel(i,j,$Table.rows(i-1).children(j-1).innerText);
     }
    }catch(e){alert("生成Excel失败!");}
  }
  else{
    alert("no data");}
}
function showInExcel(row,col,value){
  excelObj.Cells(row, col).Value=value;
}
</script>
</head>
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import= "java.lang.*"%>
<%@ page import= "java.util.*"%>
<%
//String type
java.lang.String str;
str=user.getProjectData_T();
//Array type
java.lang.String[][] str1;
java.lang.String temp="";
str1=user.getProjectData_A();
int i;
for(i=0;i<4;i++)
{
  temp=temp+"<td>"+str1[0][i]+"</td>";
}//Vector type
Vector v=new Vector();
java.lang.String temp1="";
v=user.getProjectData_V();
temp1=v.firstElement().toString();
%>
<body  bgcolor="#eaeaea">
<center>
  <table id="myTable" border="1" cellspacing="0" cellpadding="1">
<tr>
   <th nowrap>项目名称</th>
   <th nowrap>项目编号</th>
   <th nowrap>项目经理</th>
   <th nowrap>项目类别</th>
</tr>
<%=str%>
<tr>
<%=temp%>
</tr>
  </table>
  <%=temp1%>
  <table border="0">
  <tr>
  <td>
  <input type="button" value="生成EXCEL表" onclick="printTable('myTable')">
 </td>
 <td>直接从页面的表格里取数据生成Excel表
 </td>
</tr>
<tr>
  <td>
  <input type="button" value="生成EXCEL表" onclick="printTable('myTable')">
 </td>
 <td>直接从数据库中取数据生成Excel表
 </td>
</tr>
  </table>
  </center>
</body>
</html>

解决方案 »

  1.   

    我套用了,总显示:生成Excel失败!
      

  2.   

    用鼠标选择,右键单击导出到execel
      

  3.   

    <%@ page language="java" contentType="text/html; charset=GB2312" %>
    <%@ page import="java.text.*,java.io.*,java.util.*" %>
    <jsp:useBean id="SQLBean" class="pcb.myQuery" scope="page" /><%
      String tableid = request.getParameter("tableid");
      int rcount = 0;
      String sql = "SELECT COUNT(*) FROM table_map WHERE table_id='" + tableid + "'";
      SQLBean.execQuery(sql);
      if (SQLBean.mySet.next()) rcount = SQLBean.mySet.getInt(1);
      sql = "SELECT fname FROM table_map WHERE table_id='" + tableid + "' ORDER BY id";
      SQLBean.execQuery(sql);
    %>
    <html><head>
    <meta name="GENERATOR" content="Microsoft FrontPage 5.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title></title>
    </head><body>
    <style type="text/css"> 
    {
      .showTD {color: #003366; } 
      .hiddenTD {display:none; } 
      .onFocue  {color: #CC66FF;}
      .offFocue {color:#003366;}
    }
    #floater {
        LEFT: 445px; POSITION: absolute; TOP: 15px; VISIBILITY: visible; WIDTH: 125px; Z-INDEX: 10}
    </STYLE><object classid="clsid:0002E510-0000-0000-C000-000000000046" id="Spreadsheet1" width="777" height="500">
      <param name="HTMLURL" value>
      <param name="HTMLData" value="&lt;html xmlns:x=&quot;urn:schemas-microsoft-com:office:excel&quot;
    xmlns=&quot;http://www.w3.org/TR/REC-html40&quot;&gt;&lt;head&gt;
    &lt;style type=&quot;text/css&quot;&gt;
    &lt;!--tr
    {mso-height-source:auto;}
    td
    {white-space:nowrap;}
    .wc07FF4F4
    {white-space:nowrap;
    font-family:&Euml;&Icirc;&Igrave;&aring;;
    mso-number-format:General;
    font-size:auto;
    font-weight:auto;
    font-style:auto;
    text-decoration:auto;
    mso-background-source:auto;
    mso-pattern:auto;
    mso-color-source:auto;
    text-align:general;
    vertical-align:bottom;
    border-top:none;
    border-left:none;
    border-right:none;
    border-bottom:none;
    mso-protection:locked;}
    --&gt;
    &lt;/style&gt;
    &lt;/head&gt;&lt;body&gt;
    &lt;!--[if gte mso 9]&gt;&lt;xml&gt;
     &lt;x:ExcelWorkbook&gt;
      &lt;x:ExcelWorksheets&gt;
       &lt;x:ExcelWorksheet&gt;
        &lt;x:OWCVersion&gt;9.0.0.3821&lt;/x:OWCVersion&gt;
        &lt;x:Label Style='border-top:solid .5pt silver;border-left:solid .5pt silver;
         border-right:solid .5pt silver;border-bottom:solid .5pt silver'&gt;
         &lt;x:Caption&gt;Microsoft Office Spreadsheet&lt;/x:Caption&gt;
        &lt;/x:Label&gt;
        &lt;x:Name&gt;Sheet1&lt;/x:Name&gt;
        &lt;x:WorksheetOptions&gt;
         &lt;x:Selected/&gt;
         &lt;x:Height&gt;13229&lt;/x:Height&gt;
         &lt;x:Width&gt;20558&lt;/x:Width&gt;
         &lt;x:TopRowVisible&gt;5&lt;/x:TopRowVisible&gt;
         &lt;x:LeftColumnVisible&gt;0&lt;/x:LeftColumnVisible&gt;
         &lt;x:ProtectContents&gt;False&lt;/x:ProtectContents&gt;
         &lt;x:DefaultRowHeight&gt;210&lt;/x:DefaultRowHeight&gt;
         &lt;x:StandardWidth&gt;2389&lt;/x:StandardWidth&gt;
        &lt;/x:WorksheetOptions&gt;
       &lt;/x:ExcelWorksheet&gt;
      &lt;/x:ExcelWorksheets&gt;
      &lt;x:MaxHeight&gt;80%&lt;/x:MaxHeight&gt;
      &lt;x:MaxWidth&gt;80%&lt;/x:MaxWidth&gt;
     &lt;/x:ExcelWorkbook&gt;
    &lt;/xml&gt;&lt;![endif]--&gt;&lt;table class=wc07FF4F4 x:str&gt;
     &lt;col class=wc07FF4F4 width=&quot;56&quot;&gt;
     &lt;tr height=&quot;14&quot;&gt;
      &lt;td class=wc07FF4F4&gt;&lt;/td&gt;
     &lt;/tr&gt;
    &lt;/table&gt;&lt;/body&gt;&lt;/html&gt;
    ">
      <param name="DataType" value="HTMLDATA">
      <param name="AutoFit" value="0">
      <param name="DisplayColHeaders" value="-1">
      <param name="DisplayGridlines" value="-1">
      <param name="DisplayHorizontalScrollBar" value="-1">
      <param name="DisplayRowHeaders" value="-1">
      <param name="DisplayTitleBar" value="-1">
      <param name="DisplayToolbar" value="-1">
      <param name="DisplayVerticalScrollBar" value="-1">
      <param name="EnableAutoCalculate" value="-1">
      <param name="EnableEvents" value="-1">
      <param name="MoveAfterReturn" value="-1">
      <param name="MoveAfterReturnDirection" value="0">
      <param name="RightToLeft" value="0">
      <param name="ViewableRange" value="1:65536">
    </object>
    <div class="hiddenTD" id="tablers">
    <table width=95% align=center border=1 cellspacing=0 cellpadding=0>
    <tr><b>
    <%
      while (SQLBean.mySet.next()){
      out.println("<td>&nbsp;" + SQLBean.mySet.getString(1) + "</td>");
      }
    %>
    </b>
    </tr><%
      sql = "SELECT * FROM " + tableid + " ORDER BY id";
      SQLBean.execQuery(sql);
      while (SQLBean.mySet.next()){
      out.println("<tr>");
      for(int i=0;i<rcount;i++){
      if(SQLBean.mySet.getString(i+2)!=null){
      out.println("<td>&nbsp;" + String.valueOf(SQLBean.mySet.getString(i+2)) + "</td>");
      }
        }
      out.println("</tr>");
      }
      SQLBean.ShutOff();
    %></table>
    </div>
    <script language="javascript">
    Spreadsheet1.HTMLData = tablers.innerHTML;
    </script>
    </body>
    </html>
      

  4.   

    blackcourser兄,谢谢你给的代码!
    不过运行你代码中的javascript时总是显示“生成Excel失败!”。
    经我测试,发现执行到“excelObj = new ActiveXObject("Excel.Application");”这条语句时发生异常导致显示“生成Excel失败!”。
    希望blackcourser兄能检查一下,并能给出解决方法,小弟在这儿先谢谢你了!
      

  5.   

    xzhoujun兄,也感谢你给出的代码!
    运行你的代码,可以在浏览器中显示Excel界面,界面中的内容正是网页中的表格数据。但是怎么把浏览器中显示的Excel格式的表格数据转存到真正的Excel文件中去呢?在右键菜单中没有相关的命令,在浏览器中显示的Excel界面上有一个“导出到Excel”工具按钮,但按了也没用。用浏览器菜单中“文件/保存”命令也不行。
    希望xzhoujun兄予以指点,小弟在这儿也先谢谢你了!
      

  6.   

    To woodbridge(): 运行EXCEL、WORD等你得配置相应文件,搜索一下过去的帖子吧
      

  7.   

    woodbridge() 搞定这个问题了吗?俺还是不知道怎么做