小弟不才在网上找了个用JS写的导出为EXCEL的例子
但老
是报  Automation 服务器不能创建对像
但是页面输出后 查看源代码
把输出的HTML COPY下来放到一个文件里在改下TXT为HTML
就可以了运行
不知道是什么原因
我把全部代码给发出来
大哥们帮我看看是什么原因!
-------------------------------------
aspx文件
--------
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="W.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<div id="top" runat="server"><FONT face="宋体"> </FONT>
</div>
<center><input type="Button" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="notPrint"></center>
</form>
</body>
</HTML>--------------------------------------
cs文件
----------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace W
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlGenericControl top;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string link="server=127.0.0.1;user=sa;password=1;database=hubin";
string sql="select * from hb";
SqlConnection con=new SqlConnection(link);
SqlCommand ocm=new SqlCommand(sql,con);
SqlDataReader rs=null;
con.Open();
string table=@"<table id=data width=500 border=0 align=center cellpadding=2 cellspacing=1 bgcolor=#0066CC>"+
@"<tr><td>ID</td><td>NAME</td><td>ARG</td></tr>";
rs=ocm.ExecuteReader();
while(rs.Read())
{
table+="<tr bgcolor=#FFFFFF><td>"+rs[0].ToString()+"</td><td>"+rs[0].ToString()+"</td><td>"+rs[2].ToString()+"</td></tr>";
}
table+="</table>";
rs.Close();
ocm=null;
con.Close();
top.InnerHtml=table;
string javascript="<script language='javascript'>function AutomateExcel(){"+
         "var oXL = new ActiveXObject('Excel.Application');"+ 
                   "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;"+
         "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>";
Response.Write(javascript);
}
}
}

解决方案 »

  1.   

    你走的用户类型不同,保存成htm估计你用的是类似于c:\xxx.htm这样的路径做file:\\\而aspx走的是http://当然会报错了,这是因为安全机制的问题,很难有权限的一般来说,你可以用设置contentType输出一个table的形式的
      

  2.   

    我发信息你你不在线了。有时间联系我。我写了一个你可以参考的导出为EXCEL的东东。
      

  3.   

    var oXL = new ActiveXObject('Excel.Application');很难有执行权限,而且依赖对方必须有office
      

  4.   

    只要服务器上面安装Excel就可以用COM编程方式在服务器导出,然后下载。
    还有个办法就是使用微软的ReportingService