将数据导入Excel的问题

Narcist 2009-08-19 04:34:37
用ASP写段程序,将数据导入到Excel里,
运行时提示错误:ActiveX 部件不能创建对象: 'Server.CreateObject'

错误代码是:Set objSpreadsheet = Server.CreateObject("OWC.Spreadsheet")这句,这是怎么导致的,如何解决呢
...全文
47 点赞 收藏 6
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shenzhenNBA 2009-09-02
出现的问题估计楼上的说了,估计服务器上没有安装对应的控件,

如下是将数据从MDB导出到EXCEL中,服务器需要安装EXCEL和FSO(FileSystemObject)对象(一般都会有的):


<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
dim msg,exlFileName
msg=""
exlFileName="xExcelFileName" '保存的EXCEL文件名,不含后缀,根据需要修改,
err.clear

if exlFileName<>"" then
dim acDBName,objDB,strConn
acDBName="path/DateBaseName.mdb" '根据你的需要修改
strConn="DRIVER={microsoft access driver (*.mdb)};UID=;PWD=;DBQ="&server.MapPath(acDBName)
set objDB=server.CreateObject("adodb.connection")
objDB.open strConn

dim objRS,qSQL
qSQL="select * from xTable" '你的表名,根据需要修改
set objRS=server.CreateObject("adodb.recordset")
objRS.open qSQL,objDB,1,1

if objRS.bof and objRS.eof then
msg="<font color=""red"">No data in ACCESS DB, and not create EXCEL file.</font>"
else
dim objFSO,objExcel,saveExlFileName,strTemp,tempA,i

saveExlFileName=""
saveExlFileName="Excels/"&exlFileName&year(now()) '可以使用程序生成保存的路径或文件夹
if len(month(now()))<2 then
saveExlFileName=saveExlFileName&"0"&month(now())
else
saveExlFileName=saveExlFileName&month(now())
end if
if len(day(now()))<2 then
saveExlFileName=saveExlFileName&"0"&day(now())
else
saveExlFileName=saveExlFileName&day(now())
end if
if len(hour(now()))<2 then
saveExlFileName=saveExlFileName&"0"&hour(now())
else
saveExlFileName=saveExlFileName&hour(now())
end if
if len(minute(now()))<2 then
saveExlFileName=saveExlFileName&"0"&minute(now())
else
saveExlFileName=saveExlFileName&minute(now())
end if
saveExlFileName=saveExlFileName&".xls"

on error resume next
set objFSO=server.CreateObject("scripting.FileSystemObject")
if objFSO.fileExists(server.MapPath(saveExlFileName)) then
objFSO.deleteFile(server.MapPath(saveExlFileName))
end if
set objExcel=objFSO.createTextFile(server.MapPath(saveExlFileName),true,true)

for i=0 to objRS.fields.count -1
tempA=tempA & objRS.fields(i).name & chr(9)
next
objExcel.write tempA & chr(13)

do while not objRS.eof
tempA=""
for i=0 to objRS.fields.count-1
tempA=tempA & objRS.fields(i).value & chr(9)
next
objExcel.write tempA
objExcel.write chr(13)
objRS.movenext
loop

objExcel.close
set objExcel=nothing
set objFSO=nothing
msg="ok"
end if

objRS.close
set objRS=nothing
objDB.close
set objDB=nothing
else
msg="<font color=""red"">File name is empty and not create EXCEL file.</font>"
end if

if err.number<>0 then
response.Write "<br><font color=""red"">Error code:</font> " & err.number
response.Write "<br><font color=""red"">Error description:</font> " & err.description
response.Write "<br><font color=""red"">Error source:</font> " & err.source
end if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>FSO建立并增加数据到EXCEL中</title>
</head>
<style type="text/css">
body,td,div,input,fieldset,textarea{font-family:Verdana; font-size:11px; color:#333333; font-weight:normal;}
a:link,a:visited{font-family:Verdana; font-size:11px; color:#333333; font-weight:normal; padding:0px 3px; line-height:20px; text-decoration:none;}
a:hover,a:active{font-family:Verdana; font-size:11px; color:#FF6600; font-weight:normal;}
span{font-family:Verdana; font-size:11px; color:red; font-weight:bold; padding-left:5px; margin:0px 10px;}
</style>
<body>
<p> </p>
<p>
<%
if msg="ok" then
response.Write "<b>OK! EXCEL FILE CREATED:</b>"
response.Write "<hr size=""1"" color=""#FF6600"">"
response.Write "Click and download <a href=""" & saveExlFileName & """><b>" & saveExlFileName & "</b></a>"
else
response.Write "<b>Tip:</b><br>" & msg
end if
%>
</p>
</body>
</html>


回复
yanniu008 2009-09-02
OWC 印象是要装OFFICE的
回复
toury 2009-09-02
你服务器上没安装OWC吧?
回复
wtg321 2009-09-02
执行的机器里装EXCEL了嘛?
回复
chinank 2009-08-21
dcom组件给权限了么?
回复
poiuy343 2009-08-19
ASP操作EXCEL总结
http://hi.baidu.com/xymc_001/blog/item/295483c2c3a39b1a0ff4771b.html
回复
发动态
发帖子
ASP
创建于2007-09-28

2.8w+

社区成员

ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
申请成为版主
社区公告
暂无公告