关于将表中的数据导入EXCEL中的问题,以前有人问过,可是没有答案,请各位大侠帮忙,伸援手!

purpleroser 2005-01-28 10:47:20
在用javascript创建excel.application对象时,提示automation服务器不能创建对象
<%@ page AutoEventWireup="false"%>
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<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.Sheets("Sheet1").Name="记录集";
oXL.UserControl = true;

}
//-->
</SCRIPT>
<body>
<a href="#" onclick="AutomateExcel();">导出到excel</a>
<table id="data" width="80%" border="1" cellpadding="0" bordercolor="#000000">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>生日</td>
</tr>
</table>
</body>
</html>
用过了大家以前说得任何方法都不行,不知道哪位大侠还有高招,提示一下,或者从新给个写法也行!
只要实现将表单的内容导入显示到EXCEL中就行,先谢谢了!急呀!
...全文
189 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hqcsx 2005-01-31
  • 打赏
  • 举报
回复
我的我用过的.对的呀.楼主在好好看看.
hbhbhbhbhb1021 2005-01-28
  • 打赏
  • 举报
回复
好象是要把IE的没有签名的控件安全级别调低,调到提示或启用都可以
purpleroser 2005-01-28
  • 打赏
  • 举报
回复
方法不行呀,高手们,再给点高见吧!急呀!!!
rwj 2005-01-28
  • 打赏
  • 举报
回复
看看我写的这个函数,文件名自己改一下,还有这里没有加标题。自己加一下就可以了。

Function ExporToExcel(strOpen)
Response.write strOpen
'*********************************************************
'* 名称:ExporToExcel
'* 功能:导出数据到EXCEL
'* 用法:ExporToExcel(sql查询字符串)
'*********************************************************
Set Rs_Data = Server.CreateObject("Adodb.Recordset")
Dim Irowcount
Dim Icolcount

Rs_Data.Open strOpen,conn,3,1
With Rs_Data
If .RecordCount < 1 Then
Response.write "<script>alert(""没有记录!"")</script>"
Exit Function
End If
'记录总数
Irowcount = .RecordCount
'字段总数
Icolcount = .Fields.Count
End With

Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
xlApp.Application.Visible = False

Set xlBook = Nothing
Set xlSheet = Nothing

xlApp.Workbooks.Add
Set xlBook = xlApp.ActiveWorkBook
Set xlSheet = xlBook.Worksheets("sheet1")
'xlApp.Visible = True

for i = 1 to Irowcount
for j=1 to Icolcount
xlSheet.cells(i,j) = Rs_Data(j-1)
next
Rs_Data.movenext
next
'xlApp.Application.Visible = True
xlBook.SaveAs server.mappath("/Table.Xls")
xlApp.Quit
Set xlApp = Nothing '"交还控制给Excel
Set xlBook = Nothing
Set xlSheet = Nothing

Response.redirct
End Function
梅雪香 2005-01-28
  • 打赏
  • 举报
回复
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>无标题文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<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>
</head>

<body>

<object classid="clsid:0002E510-0000-0000-C000-000000000046" id="Spreadsheet1" width="676" height="388">
<param name="HTMLURL" value>
<param name="HTMLData" value="">
<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>
<td>客户名称A</td>
<td>客户名称B</td>
<td>客户名称C</td>
<td>客户名称D</td>
<tr>
<td>abc</td>
<td>def</td> <td>111</td> <td>222</td> <tr>
<td>ddd</td>
<td>dfdf</td>
<td>ddd</td>
<td>ddd</td>
</table>
</div>
<script language="javascript">
Spreadsheet1.HTMLData = tablers.innerHTML ;
</script>


</body>
</html>
baikaishui_0825 2005-01-28
  • 打赏
  • 举报
回复
http://blog.csdn.net/baikaishui_0825/archive/2004/12/15/217139.aspx
hqcsx 2005-01-28
  • 打赏
  • 举报
回复
<!--#include file="../conn/conn.asp"-->
<%
'导出xml,csv,htm文件DEMO
'用法:ExportDB.asp?t=文件类型&sql=sql语句
'文件类型可以是htm,csv,xml,默认是htm
'http://www.51windows.Net
'tablename = "htm"
Dim shijian,dingdan
shijian = now()
dingdan = year(shijian)&month(shijian)&day(shijian)&hour(shijian)&minute(shijian)&second(shijian)

dim tablename
sql2 = request("sql")
tablename = request("tablename")
if tablename = "" then
tablename = "数据导出结果"&dingdan
else
tablename = tablename&dingdan
end if
sql = replace(sql2,";","")

filetype = lcase(request("t"))
dim dbid,IndexPageName
function HTMLEncode(fString)
if not isnull(fString) then

fString = Server.HTMLEncode(fString)
fString = Replace(fString, CHR(10) & CHR(10), "</P><P> ")
fString = Replace(fString, CHR(10), "<BR> ")
fString = Replace(fString, CHR(9), "  ")

HTMLEncode = fString
end if
end function

function Myreplace(str)
if not isnull(str) then
fString = Replace(fString,"""", """""")
Myreplace = str
else
Myreplace = ""
end if
end function

dim def_export_val
def_export_sep = ","
def_export_val = """"
if lcase(left(sql,6))<>"select" then
Response.write "出错了。。。"
Response.end
end if
Set rs = Conn.Execute(sql)
if filetype="xml" then
Response.contenttype="text/xml"
Response.Charset = "gb2312"
Response.AddHeader "Content-Disposition", "attachment;filename="&tablename&".xml"
Response.write "<?xml version=""1.0"" encoding=""gb2312""?>"
Response.write vbnewline&"<root>"
strLine=""
dim thefield(50)
i = 0
For each x in rs.fields
thefield(i)=x.name
i=i+1
Next
While rs.EOF =false
strLine= vbnewline&chr(9)&"<row>"
k=0
For each x in rs.fields
strLine= strLine & vbnewline&chr(9)&chr(9)&"<"&thefield(k)&">"
if instr(x.value,"<") or instr(x.value,">") or len(x.value)>255 then
strLine= strLine &"<![CDATA["& x.value &"]]>"
else
strLine= strLine & x.value
end if
strLine= strLine &"</"&thefield(k)&">"


k=k+1
Next
rs.MoveNext
Response.write strLine &vbnewline& chr(9)&"</row>"
Wend
Response.write vbnewline&"</root>"
elseif filetype="csv" then
'Response.contenttype="application/vnd.ms-excel"
Response.contenttype="text/csv"
Response.AddHeader "Content-Disposition", "attachment;filename="&tablename&".csv"
strLine=""
For each x in rs.fields
strLine= strLine & def_export_val & x.name & def_export_val & def_export_sep
Next
Response.write strLine & vbnewline
While rs.EOF =false
strLine= ""
For each x in rs.fields
strLine= strLine & def_export_val & trim(Myreplace(x.value)) & def_export_val & def_export_sep
Next
rs.MoveNext
Response.write strLine & vbnewline
Wend
else
if filetype="htm" then'弹出下载html的对话框
Response.contenttype="application/ms-download"
Response.AddHeader "Content-Disposition", "attachment;filename="&tablename&".htm"
end if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<!--META NAME="Author" CONTENT="51windows,海娃,haiwa">
<META NAME="Description" CONTENT="Power by 51windows.Net"-->
<title>导出数据。。</title>
<style>
<!--
body { font-family: Verdana; font-size: 11px }
th { font-family: Verdana; font-size: 11px;padding:3px;color:#FFFFFF;background-color:#999999;}
td { font-family: Verdana; font-size: 11px;padding:3px;background-color:#EFEFEF;}
-->
</style>
</head>
<BODY style="overflow:auto;" topmargin=2 bgcolor=buttonface oncontextmenu=self.event.returnValue=false >
<div align="center">
<table border="0" cellpadding="0" cellspacing="1" bgcolor="#000000" width="100%">
<tr>
<%
i=0
For each x in rs.fields
strLine= strLine &chr(9)&chr(9)&"<th align=""center"">"& x.name &"</th>"& vbnewline
Next
Response.write strLine&chr(9)&"</tr>"& vbnewline & vbnewline
While rs.EOF =false
i=i+1
Response.write chr(9)&"<tr>"& vbnewline
strLine= ""
For each x in rs.fields
strLine= strLine &chr(9)&chr(9)&"<td>"& HTMLEncode(x.value) &"</td>"& vbnewline
Next
rs.MoveNext
Response.write strLine
Response.write chr(9)&"</tr>"& vbnewline & vbnewline
Wend
Response.write "</table>"& vbnewline
if filetype<>"htm" and filetype<>"xls" and filetype<>"txt" then
Response.write "<p style='line-height:160%;'>"&i&"条记录 <a href='?tablename="&request("tablename")&"&t=htm&sql="&server.urlencode(sql2)&"'>导出HTML</a>|<a href='?tablename="&request("tablename")&"&t=csv&sql="&server.urlencode(sql2)&"'>导出EXCEL</a>|<a href='?tablename="&request("tablename")&"&t=xml&sql="&server.urlencode(sql2)&"'>导出XML</a>"& vbnewline
end if
'Response.write "<p>Power by <A HREF=""http://www.51windows.Net"" target=""_blank"">51windows.Net</A>"& vbnewline
Response.write "</div>"& vbnewline
Response.write "</BODY>"& vbnewline
Response.write "</HTML>"& vbnewline
%>

<%
end if

rs.close
conn.close
Set rs=nothing
Set conn=nothing
%>
yqh1314 2005-01-28
  • 打赏
  • 举报
回复
来混分来了!
yb2008 2005-01-28
  • 打赏
  • 举报
回复
在IE中把控件启用!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
<TABLE id="outtable" name="outtable">
<TR>
<TD>asdf</TD>
<TD>asdf</TD>
<TD>asdf</TD>
<TD>asdf</TD>
<TD>asdf</TD>
</TR>
<TR>
<TD>asdf</TD>
<TD>asdf</TD>
<TD>asdf</TD>
<TD>asdf</TD>
<TD>asdf</TD>
</TR>
<TR>
<TD>asdf</TD>
<TD>asdf</TD>
<TD>asdf</TD>
<TD>asdf</TD>
<TD>asdf</TD>
</TR>
<TR>
<TD>asdf</TD>
<TD>asdf</TD>
<TD>asdf</TD>
<TD>asdf</TD>
<TD>asdf</TD>
</TR>
</TABLE>
<input type="button" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="notPrint">
<SCRIPT LANGUAGE="javascript">
function AutomateExcel()
{
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var hang = outtable.rows.length;

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

for (i=0;i<hang;i++)
{
for (j=0;j<lie;j++)
{
oSheet.Cells(i+1,j+1).value = outtable.rows(i).cells(j).innerText;
}

}
oXL.Visible = true;
oXL.UserControl = true;
}
</SCRIPT>
</body>
</html>
purpleroser 2005-01-28
  • 打赏
  • 举报
回复
解决了,谢谢!

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧