转到该页面的时候,同时把table中的值写到excel中就可以了

解决方案 »

  1.   

    <%@ page 
    language="java"
    contentType="application/msexcel;charset=GBK"
    %>
    <% 
    response.setHeader("Pragma","No-cache"); 
    response.setHeader("Cache-Control","no-cache"); 
    response.setDateHeader("Expires", 0); 
    %> 
    <%
    /***************************************************************************
    程序名称:生成Excel功能
    功能描述:评价统计
    作   者:easydozer
    日   期:2004-3-4
    ***************************************************************************/
    //程序处理区开始//程序处理区结束String saveFileName = "评价统计";
    response.setHeader("Content-Disposition","attachment;filename="+saveFileName+".xls");
    %>
    <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=GB2312">
    <meta name=ProgId content=Excel.Sheet>
    <meta name=Generator content="Microsoft Excel 9">
    <link rel=File-List href="./Book1.files/filelist.xml">
    <link rel=Edit-Time-Data href="./Book1.files/editdata.mso">
    <link rel=OLE-Object-Data href="./Book1.files/oledata.mso">
    <!--[if gte mso 9]><xml>
     <o:DocumentProperties>
      <o:Author>xjs</o:Author>
      <o:LastAuthor>xjs</o:LastAuthor>
      <o:Created>2004-02-20T08:55:42Z</o:Created>
      <o:LastSaved>2004-02-20T08:56:14Z</o:LastSaved>
      <o:Company>efound</o:Company>
      <o:Version>9.2812</o:Version>
     </o:DocumentProperties>
     <o:OfficeDocumentSettings>
      <o:DownloadComponents/>
      <o:LocationOfComponents HRef="file:msowc.cab"/>
     </o:OfficeDocumentSettings>
    </xml><![endif]-->
    <style>
    <!--table
    {mso-displayed-decimal-separator:"\.";
    mso-displayed-thousand-separator:"\,";}
    @page
    {margin:1.0in .75in 1.0in .75in;
    mso-header-margin:.5in;
    mso-footer-margin:.5in;}
    tr
    {mso-height-source:auto;
    mso-ruby-visibility:none;}
    col
    {mso-width-source:auto;
    mso-ruby-visibility:none;}
    br
    {mso-data-placement:same-cell;}
    .style0
    {mso-number-format:General;
    text-align:general;
    vertical-align:bottom;
    white-space:nowrap;
    mso-rotate:0;
    mso-background-source:auto;
    mso-pattern:auto;
    color:windowtext;
    font-size:12.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:宋体;
    mso-generic-font-family:auto;
    mso-font-charset:134;
    border:none;
    mso-protection:locked visible;
    mso-style-name:常规;
    mso-style-id:0;}
    td
    {mso-style-parent:style0;
    padding-top:1px;
    padding-right:1px;
    padding-left:1px;
    mso-ignore:padding;
    color:windowtext;
    font-size:10.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:宋体;
    mso-generic-font-family:auto;
    mso-font-charset:134;
    mso-number-format:General;
    text-align:general;
    vertical-align:bottom;
    border:none;
    mso-background-source:auto;
    mso-pattern:auto;
    mso-protection:locked visible;
    white-space:nowrap;
    mso-rotate:0;}
    .Ztitle{
    background:#ECECEC;
    }
    ruby
    {ruby-align:left;}
    rt
    {color:windowtext;
    font-size:9.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:宋体;
    mso-generic-font-family:auto;
    mso-font-charset:134;
    mso-char-type:none;
    display:none;}
    -->
    </style>
    <!--[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:ActiveCol>4</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:ExcelWorksheet>
        <x:Name>Sheet2</x:Name>
        <x:WorksheetOptions>
         <x:DefaultRowHeight>285</x:DefaultRowHeight>
         <x:ProtectContents>False</x:ProtectContents>
         <x:ProtectObjects>False</x:ProtectObjects>
         <x:ProtectScenarios>False</x:ProtectScenarios>
        </x:WorksheetOptions>
       </x:ExcelWorksheet>
       <x:ExcelWorksheet>
        <x:Name>Sheet3</x:Name>
        <x:WorksheetOptions>
         <x:DefaultRowHeight>285</x:DefaultRowHeight>
         <x:ProtectContents>False</x:ProtectContents>
         <x:ProtectObjects>False</x:ProtectObjects>
         <x:ProtectScenarios>False</x:ProtectScenarios>
        </x:WorksheetOptions>
       </x:ExcelWorksheet>
      </x:ExcelWorksheets>
      <x:WindowHeight>6075</x:WindowHeight>
      <x:WindowWidth>11700</x:WindowWidth>
      <x:WindowTopX>120</x:WindowTopX>
      <x:WindowTopY>45</x:WindowTopY>
      <x:ProtectStructure>False</x:ProtectStructure>
      <x:ProtectWindows>False</x:ProtectWindows>
     </x:ExcelWorkbook>
    </xml><![endif]-->
    </head><body link=blue vlink=purple><%
    //现在开始输出表格数据,表格数据尽量简单,少使用格式
    %>
    <%//.......................%>
    <%
    //输出数据结束
    %></body></html>
      

  2.   

    我的页面如下:
    <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
    <%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ page import="com.trademis.web.reportBean.ProductSaleReport,java.math.BigDecimal,java.lang.Double,java.text.DecimalFormat" %>
    <%@ page contentType="application/vnd.ms-excel;charset=GBK" %>
    <%int i=1;%>
    <%
    DecimalFormat dfAmount = new DecimalFormat("#.000");
        DecimalFormat dfMoney = new DecimalFormat("#.00");
    double sumAmountInto =0;
    double sumAmountSale =0;
            double sumAmountAllocateInto =0;
            double sumAmountAllocateSale =0;
            double sumQuantity =0;
    %>
    <html:html>
    <head>
    <title>
    产品销售数量统计
    </title>
    </head>
    <body>
    <table width="100%"  class="tablebg" align="center" cellpadding="3" cellspacing="1">
      <caption>产品销售数量统计</caption>
      <tr class="tabletitle">
        <td>序号</td>
        <td>产品</td>
        <td>累计收入</td>
        <td>销售数量</td>
        <td>调入</td>
        <td>调出</td>
        <td>实际库存</td>
      </tr>
      <logic:iterate name="LogList" id="log" scope="request" type="com.trademis.web.reportBean.ProductSaleReport">
      <%
    double amountInto =Double.parseDouble(((ProductSaleReport)log).getAmountInto());
            double amountSale=Double.parseDouble(((ProductSaleReport)log).getAmountSale());
            double amountAllocateInto=Double.parseDouble(((ProductSaleReport)log).getAmountAllocateInto());
            double amountAllocateSale=Double.parseDouble(((ProductSaleReport)log).getAmountAllocateSale());
            double quantity=Double.parseDouble(((ProductSaleReport)log).getQuantity());%>
      <tr>
        <td class="tableCenter"><%=i++%></td>
        <td class="tableCenter"><bean:writeTrans dictionary="ProductInfo" name="log" property="productCode"/></td>
        <td class="tableLeft"><div align="right"><bean:write name="log" property="amountInto" formatKey="format.money"/></div></td>
        <td class="tableLeft"><div align="right"><bean:write name="log"   property="amountSale" formatKey="format.money"/></div></td>
        <td class="tableLeft"><div align="right"><bean:write name="log"   property="amountAllocateInto" formatKey="format.money"/></div></td>
        <td class="tableLeft"><div align="right"><bean:write name="log"   property="amountAllocateSale" formatKey="format.money"/></div></td>
        <td class="tableLeft"><div align="right"><bean:write name="log"   property="quantity" formatKey="format.money"/></div></td>
        <%
    sumAmountInto +=amountInto;
    sumAmountSale +=amountSale;
            sumAmountAllocateInto +=amountAllocateInto;
            sumAmountAllocateSale +=amountAllocateSale;
            sumQuantity +=quantity;
    %>
      </tr>
      </logic:iterate>
      <tr>
        <td class="tableCenter"></td>
        <td class="tableCenter">合计</td>
        <td class="tableLeft"><%=dfAmount.format(sumAmountInto)%></td>
        <td class="tableLeft"><%=dfAmount.format(sumAmountSale)%></td>
        <td class="tableLeft"><%=dfAmount.format(sumAmountAllocateInto)%></td>
        <td class="tableLeft"><%=dfAmount.format(sumAmountAllocateSale)%></td>
        <td class="tableLeft"><%=dfAmount.format(sumQuantity)%></td>
      </tr>
      <tr bgcolor="#ffffff">
        <td colspan="10"><hr/></td>
      </tr>
    </table>
    </body>
    </html:html>
      

  3.   

    给你个例子,是html格式的,可以直接运行,你一下子就明白了,可以直接吧table中的内容导入到excel:<%@ page contentType="text/html; charset=GBK"%>
    <input type="hidden" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="notPrint"> <title>浏览器表格导出到Excel</title>
    <input type="button" name="out_word1" onclick="javascript:AutomateExcel() " value="导出到excel" class="notPrint"> 
    <table id="data" width="200" border="1">
      <tr>
        <td>我是李春雷</td>
        <td>11</td>
      </tr>
      <tr>
        <td>22</td>
        <td>22</td>
      </tr>
      <tr>
        <td>33</td>
        <td>33</td>
      </tr>
      <tr>
        <td>44 </td>
        <td>44</td>
      </tr>
    </table>
    我是李春雷
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function AutomateExcel() 
    {
    // Start Excel and get Application object.
    var oXL = new ActiveXObject("Excel.Application"); 
    // Get a new workbook.
    var oWB = oXL.Workbooks.Add();
    var oSheet = oWB.ActiveSheet;
    var table = document.all.data; 
    var hang = table.rows.length;var lie = table.rows(0).cells.length; // Add table headers going cell by cell.
    for (i=0;i<hang;i++)
    {
    for (j=0;j<lie;j++)
    {
    oSheet.Cells(i+1,j+1).Value = table.rows(i).cells(j).innerText;
    }}
    oXL.Visible = true;
    oXL.UserControl = true;
    }
    //-->
    </SCRIPT>
      

  4.   

    点击button后没有反应,提示有错误,我的页面是在“iframe”中的,这有关系吗?谢谢。
      

  5.   

    可以的,只要在HTML文件中加入<%@ page contentType="application/msexcel;charset=GBK"%>
    意识是说用EXCEL程序打开!!!!???
      

  6.   

    我想要实现的的确是如“ lcllcl987”老兄的例子,点击bottun后输出excel文件。我用了“lcllcl987”的方法,但是没有成功。
      

  7.   

    把我的代码贴出来,请“lcllcl987”帮忙看看:
    <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
    <%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ page import="com.trademis.web.reportBean.ProductSaleReport,java.math.BigDecimal,java.lang.Double,java.text.DecimalFormat" %>
    <%@ page contentType="text/html; charset=GBK"%>
    <%int i=1;%>
    <%
    DecimalFormat dfAmount = new DecimalFormat("#.000");
        DecimalFormat dfMoney = new DecimalFormat("#.00");
    double sumAmountInto =0;
    double sumAmountSale =0;
            double sumAmountAllocateInto =0;
            double sumAmountAllocateSale =0;
            double sumQuantity =0;
    %>
    <html:html>
    <head>
    <title>
    产品销售数量统计
    </title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <link href="../CSS/trade.CSS" rel="stylesheet" type="text/css"></head>
    <body>
    <input type="button" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="notPrint">
    <table id="data" width="100%"  class="tablebg" align="center" cellpadding="3" cellspacing="1">
      <caption>产品销售数量统计</caption>
      <tr class="tabletitle">
        <td>序号</td>
        <td>产品</td>
        <td>累计收入</td>
        <td>销售数量</td>
        <td>调入</td>
        <td>调出</td>
        <td>实际库存</td>
      </tr>
      <logic:iterate name="LogList" id="log" scope="request" type="com.trademis.web.reportBean.ProductSaleReport">
      <%
    double amountInto =Double.parseDouble(((ProductSaleReport)log).getAmountInto());
            double amountSale=Double.parseDouble(((ProductSaleReport)log).getAmountSale());
            double amountAllocateInto=Double.parseDouble(((ProductSaleReport)log).getAmountAllocateInto());
            double amountAllocateSale=Double.parseDouble(((ProductSaleReport)log).getAmountAllocateSale());
            double quantity=Double.parseDouble(((ProductSaleReport)log).getQuantity());%>
      <tr>
        <td class="tableCenter"><%=i++%></td>
        <td class="tableCenter"><bean:writeTrans dictionary="ProductInfo" name="log" property="productCode"/></td>
        <td class="tableLeft"><div align="right"><bean:write name="log" property="amountInto" formatKey="format.money"/></div></td>
        <td class="tableLeft"><div align="right"><bean:write name="log"   property="amountSale" formatKey="format.money"/></div></td>
        <td class="tableLeft"><div align="right"><bean:write name="log"   property="amountAllocateInto" formatKey="format.money"/></div></td>
        <td class="tableLeft"><div align="right"><bean:write name="log"   property="amountAllocateSale" formatKey="format.money"/></div></td>
        <td class="tableLeft"><div align="right"><bean:write name="log"   property="quantity" formatKey="format.money"/></div></td>
        <%
    sumAmountInto +=amountInto;
    sumAmountSale +=amountSale;
            sumAmountAllocateInto +=amountAllocateInto;
            sumAmountAllocateSale +=amountAllocateSale;
            sumQuantity +=quantity;
    %>
      </tr>
      </logic:iterate>
      <tr>
        <td class="tableCenter"></td>
        <td class="tableCenter">合计</td>
        <td class="tableLeft"><%=dfAmount.format(sumAmountInto)%></td>
        <td class="tableLeft"><%=dfAmount.format(sumAmountSale)%></td>
        <td class="tableLeft"><%=dfAmount.format(sumAmountAllocateInto)%></td>
        <td class="tableLeft"><%=dfAmount.format(sumAmountAllocateSale)%></td>
        <td class="tableLeft"><%=dfAmount.format(sumQuantity)%></td>
      </tr>
      <tr bgcolor="#ffffff">
        <td colspan="10"><hr/></td>
      </tr>
    </table>
    </body>
    </html:html>
    <SCRIPT LANGUAGE="JavaScript">
    function AutomateExcel() 
    {
    // Start Excel and get Application object.
    var oXL = new ActiveXObject("Excel.Application"); 
    // Get a new workbook.
    var oWB = oXL.Workbooks.Add();
    var oSheet = oWB.ActiveSheet;
    var table = document.all.data; 
    var hang = table.rows.length;

    var lie = table.rows(0).cells.length; 

    // Add table headers going cell by cell.
    for (i=0;i<hang;i++)
    {
    for (j=0;j<lie;j++)
    {
    oSheet.Cells(i+1,j+1).Value = table.rows(i).cells(j).innerText;
    }

    }
    oXL.Visible = true;
    oXL.UserControl = true;
    }
    </SCRIPT>
      

  8.   

    去掉<tr bgcolor="#ffffff">
        <td colspan="10"><hr/></td>
      </tr>
    试试!
    上面lcllcl987的例子仅支持colspan=1和rowspan=1的情况。>1的话得再改改。
    一点拙见!
      

  9.   

    我的例子可以成功啊!
    你copy到你的html文件中试验一下,一点问题也没的
      

  10.   

    我验证了,是可以成功,但是浏览器会阻止你的javascript,客户使用了可能不方便。
      

  11.   

    非常感谢大家!
    唯一有点遗憾的就如flyingis说的“浏览器会阻止你的javascript,客户使用了可能不方便。”
    但先这样啦。