求一个用asp在win2003中将Access倒出到Excel的例子

猜我是几娃 2009-11-02 06:03:53
本来在自己的XP系统机子上写的程序,一点问题没有,但一转到服务器上(Windows2003系统)就出现些莫名其妙的问题,开始是CreateObject("Excel.Application")不能创建对象,查了半天,把DCOM什么的组件的权限都设置好了,对象能创建了,数据能倒出来,XLS文件也能生成,但倒出文件一直处于正在打开的状态直到Timeout,而且任务管理器中每倒一次就多出一个Excel.exe进程和DW20.exe进程,不知道怎么回收。。。只能手动去杀掉
哪位大哥能给一个能在windows2003下将Access倒出到Excel的asp程序例子? 能用的小弟另开贴送分。。。。
...全文
161 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wcwtitxu 2009-11-03
  • 打赏
  • 举报
回复

Sub SaveToFile(str, path)
With CreateObject("ADODB.Stream")
.Mode = 3
.Type = 2
.Open
.Charset = "UTF-8"
.WriteText str
.SaveToFile path, 2
.Close
End With
End Sub

Function LoadXML(path)
Set LoadXML = CreateObject("MSXML2.DOMDocument")
LoadXML.async = False
LoadXML.load path
End Function

Dim rs, path, savePath, xsltPath
savePath = Server.MapPath("/export/xls/myExcelFile.xls")
xsltPath = Server.MapPath("xsl.xslt")
path = Server.MapPath(Session.SessionId & ".xml")

Set rs = conn.Execute("SELECT * FROM [tab]")
rs.Save path, 1
rs.Close
Set rs = Nothing

SaveToFile LoadXML(path).transformNode(LoadXML(xsltPath)), savePath
CreateObject("Scripting.FileSystemObject").DeleteFile path



xls.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn='http://www.w3.org/2005/02/xpath-functions'
xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<xsl:output method="html" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
<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=utf-8" />
<meta name="ProgId" content="Excel.Sheet" />
<meta name="Generator" content="Microsoft Excel 11" />
<xsl:value-of select="'<!--[if gte mso 9]>'" disable-output-escaping="yes"/><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>工作薄</x:Name>
<x:WorksheetOptions>
<x:DefaultRowHeight>285</x:DefaultRowHeight>
<x:Selected/>
<x:Panes>
<x:Pane>
<x:Number>3</x:Number>
<x:ActiveRow>2</x:ActiveRow>
<x:ActiveCol>1</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:ExcelWorksheets>
<x:WindowHeight>9090</x:WindowHeight>
<x:WindowWidth>11715</x:WindowWidth>
<x:WindowTopX>240</x:WindowTopX>
<x:WindowTopY>90</x:WindowTopY>
<x:ProtectStructure>False</x:ProtectStructure>
<x:ProtectWindows>False</x:ProtectWindows>
</x:ExcelWorkbook>
</xml><xsl:value-of select="'<![endif]-->'" disable-output-escaping="yes"/>
<style type="text/css">td,th{font-size:12px; font-family:Arial,新宋体;}</style>
</head>
<body>
<table>
<tr>
<xsl:for-each select="//s:Schema//s:ElementType/s:AttributeType">
<th><xsl:value-of select="./@name"/></th>
</xsl:for-each>
</tr>
<xsl:for-each select="/xml/rs:data/z:row">
<xsl:variable name="row" select="."/>
<tr>
<xsl:for-each select="//s:Schema//s:ElementType/s:AttributeType">
<xsl:variable name="th" select="." />
<td>
<xsl:for-each select="$row/@*">
<xsl:if test="local-name(.) = $th/@name">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body></html>
</xsl:template>
</xsl:stylesheet>
lzp4881 2009-11-03
  • 打赏
  • 举报
回复
你把
<%
if request.querystring("action")="toExcel" then
Response.ContentType="application/vnd.ms-excel"
Response.AddHeader "content-disposition","attachment;filename=aaa.xls"
end if
%>
改成
<%
'if request.querystring("action")="toExcel" then
Response.ContentType="application/vnd.ms-excel"
Response.AddHeader "content-disposition","attachment;filename=aaa.xls"
'end if
%>
这样就会直接导出,而不会先生成表格了
猜我是几娃 2009-11-03
  • 打赏
  • 举报
回复
lzp4881
你说的这个方法刚才我想到了,先将数据写表格,再倒出,但有个问题就是如果数据量非常大的话,比如几十万条记录,那光是写表格就吃不消了。。。
所以我感觉是不是还是应该要直接通过建立对象直接由数据库将数据写到Excel里。。。
猜我是几娃 2009-11-03
  • 打赏
  • 举报
回复
这个是我的倒出程序,哪位大哥能给我分析下问题出在哪。。。在XP系统没有问题,但在2003下页面打不开,能生成Excel文件,但会有一个Excel.exe和一个DW20.exe进程一直存在。

Dim ExcelSql
Dim rs
ExcelSql="select * from aa"
response.Write Str_sql
On error resume next
Set rs=conn.execute(ExcelSql)

if rs.eof then
Response.write "没有记录可导出! " & aspButton("返回上一页",1)
rs.close
set rs=nothing
exit sub
end if

Dim App,Book,Shts,Sht
set App = Server.CreateObject("Excel.Application")

App.DisplayAlerts = false
App.Application.Visible = false
App.WorkBooks.add
set Book = App.ActiveWorkBook
set Shts = Book.Worksheets
set Sht = Book.Sheets(1)

Sht.Range("A1:D1").Value = Array("a","b","c","d")
Dim r '行数
r=2 '从第二行开始写
Dim DeferOctEmp,IsCheck
do while not rs.eof
Sht.Range("A"&r&":D"&r).Value=Array(rs("a"),rs("b"), rs("c"), rs("d"))
r=r+1
rs.movenext
loop
rs.close()
set rs=nothing
'设置自动列宽
Sht.Range("A1:D"&(r-1)).Columns.AutoFit
'保存Excel文件
FileName="demo"
ExcelFile="Excel/"&FileName&".xls"
Book.SaveAs Server.MapPath(ExcelFile)
response.Write "数据已导出 <a href="&ExcelFile&">下载</a>"
if err.Number<>0 then
Response.write err.Description & " " & aspButton("返回上一页",1)
App.Quit
set App = Nothing
exit sub
end if

Book.Save
App.Quit
set App = Nothing
lzp4881 2009-11-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 buker19999 的回复:]
不是将表格的数据倒到Excel,是将Access的数据倒到Excel,要先访问数据库,读出数据后写进Excel 。。。
[/Quote]
你把表格里面的数据改成从数据库读出来的不就行了
<%
if request.querystring("action")="toExcel" then
Response.ContentType="application/vnd.ms-excel"
Response.AddHeader "content-disposition","attachment;filename=aaa.xls"
end if
%>

<html>
<body>

<table name="data" id="data" width="100%" height="25%" border="0" cellpadding="1" cellspacing="1">
<tr>
<td width="50%" align="center" bgcolor="#FFFFCC">数据1 </td>
<td width="50%" align="center" bgcolor="#FFFFCC">数据2 </td>
</tr>
<%
..数据库连接
set rs=conn.execute("select * from table where ...")
do while not rs.eof
%>
<tr>
<td><%=rs(0)%></td>
<td><%=rs(1)%></td>
</tr>
<%
rs.movenext
loop
rs.close
%>
</table>
<input type="button" name="out_excel" onClick="location.href='?action=toExcel'" value="导出到excel">

</body>
</html>
猜我是几娃 2009-11-03
  • 打赏
  • 举报
回复
不是将表格的数据倒到Excel,是将Access的数据倒到Excel,要先访问数据库,读出数据后写进Excel 。。。
AB13938074 2009-11-03
  • 打赏
  • 举报
回复
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="conn.asp"-->
<%
DIM infoid
infoid=Trim(Request("infoid"))
on error resume next'如果有错误继续执行下面的代码
Server.ScriptTimeOut=360000'防止超时
set conn = server.createobject("ADODB.Connection")
conn.open StrConn
set rs=server.createobject("adodb.recordset")
'sql="select * from PE_Commonmodel where ItemID in ("&infoid&")"'根据此SQL语句导出至Excel
sql="select PE_CommonModel.title as 标题,PE_U_Article.Author as 作者,PE_U_Article.Content as 内容 from PE_CommonModel,PE_U_Article WHERE PE_CommonModel.ItemID = PE_U_Article.ID and PE_CommonModel.Status=99 and PE_CommonModel.ItemID in ("&infoid&")"'根据此SQL语句导出至Excel
rs.Open sql,conn,3,3
for Createtablei=0 to rs.Fields.Count-1
Createtable=Createtable&rs.fields(Createtablei).name&" text ,"
next
Createtablesql="Create table Sheet1("&left(Createtable,len(Createtable)-1)&")"
ExcelFile="Excel/Excel.xls"
set fso=Server.CreateObject ("Scripting.FileSystemObject")
fpath=Server.MapPath(ExcelFile)
if fso.FileExists(fpath) then
whichfile=Server.MapPath(ExcelFile)
Set fs = CreateObject("Scripting.FileSystemObject")
Set thisfile = fs.GetFile(whichfile)
thisfile.delete true
dim excelfile,tbname
end if
Dim Driver,DBPath
Set conn = Server.CreateObject("ADODB.Connection")
Driver = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;"
DBPath = "Data Source="&Server.MapPath(ExcelFile)
conn.Open Driver & DBPath
conn.Execute(Createtablesql)
for ii=0 to rs.recordcount-1
for i=0 to rs.Fields.Count-1
Inserttablename=Inserttablename&rs.fields(i).name&","
Inserttable=Inserttable&"'"&Rs(i)&"',"
Next
Insertintosql="Insert into Sheet1("&left(Inserttablename,len(Inserttablename)-1)&")values("&left(Inserttable,len(Inserttable)-1)&")"
'显示错误信息开始
if err.number<>0 then
response.write "<br><br><br><br><br><br><br>"
response.write " <div align='center'><font color='red'>"&err.description&"</font><a href='javascript:history.go(-1)'>退回上一步!</a></div>"
response.write "<br><br><br><br><br><br>"
response.end
end if
'显示错误信息结束
conn.Execute(Insertintosql)
'显示错误信息开始
if err.number<>0 then
response.write "<br><br><br><br><br><br><br>"
response.write " <div align='center'><font color='red'>"&err.description&"</font><a href='javascript:history.go(-1)'>退回上一步!</a></div>"
response.write "<br><br><br><br><br><br>"
response.end
end if
'显示错误信息结束
Insertintosql =""
Inserttable=""
Inserttablename=""
rs.MoveNext
Next
'显示错误信息开始
if err.number<>0 then
response.write "<br><br><br><br><br><br><br>"
response.write " <div align='center'><font color='red'>"&err.description&"</font><a href='javascript:history.go(-1)'>退回上一步!</a></div>"
response.write "<br><br><br><br><br><br>"
response.end
end if
'显示错误信息结束
Response.Redirect (ExcelFile)
%>
猜我是几娃 2009-11-03
  • 打赏
  • 举报
回复
TO fandelei1982
我把这两点都加进了我本来的程序中,但没有任何的改善,一打开这个文件,这个页面还是一直处于正在打开的状态,任务管理器中还是会有EXCEL.exe和DW20.exe 这两个进程一直存在。
猜我是几娃 2009-11-03
  • 打赏
  • 举报
回复
TO wcwtitxu
我按照你给的代码做了下,只对操作数据库做了修改,其他的地方都没有改动,但没有生成xls文件,倒是在目录下生成一个898481770.xml的文件,里面的内容是我要倒的数据。。。我对xml还不了解,不知道应该对你的代码要做哪些修改或者应该注意哪些地方。
猜我是几娃 2009-11-03
  • 打赏
  • 举报
回复
非常感谢各位的帮忙。。。。。。。
TO lzp4881
我按照你的方法做了下,数据是能倒出,但xls里的格式太乱了,都没有表格的样子,汉字都成了乱码,不知道从哪里着手去进行修正。。。
suxiuhai 2009-11-03
  • 打赏
  • 举报
回复
没试过用程序实现,只是直接导出,嘿嘿~
friendly_ 2009-11-03
  • 打赏
  • 举报
回复
这说明在2003下,操作完后,没能正常关闭excel进程.
先把App.Application.Visible = false 改成App.Application.Visible = true,让excel显示出来,看看操作完后是不是正常关闭,若不能正常关闭,建立时加上App.UserControl = false,关闭时加上Book.close set Book=Nothing,试试.
lzp4881 2009-11-02
  • 打赏
  • 举报
回复
<%
if request.querystring("action")="toExcel" then
Response.ContentType="application/vnd.ms-excel"
Response.AddHeader "content-disposition","attachment;filename=aaa.xls"
end if
%>

<html>
<body>
<table name="data" id="data" width="100%" height="25%" border="0" cellpadding="1" cellspacing="1">
<tr>
<td width="50%" align="center" bgcolor="#FFFFCC">数据1</td>
<td width="50%" align="center" bgcolor="#FFFFCC">数据2</td>
</tr>
</table>
<input type="button" name="out_excel" onClick="location.href='?action=toExcel'" value="导出到excel">

</body>
</html>

28,390

社区成员

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

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