Excel导出函数

<%
Sub  ExportToExcel   

Response.ContentType  =  "application/vnd.ms-Excel" 
Response.AddHeader  "Content-Disposition",  "attachment;Filename=Results.xls" 
Response.Write  "<body>" 
Response.Write  "<table  border=1>" 

Call  WriteTableData 

Response.Write  "</table>" 
Response.Write  "</body>" 
Response.Write  "</html>"   


End  Sub 
%>

Excel导入数据库


<%
dim FileName
FileName="Excel.xls"  '取得文件名,来自项目经理的指定,路径固定在某个虚拟路径中
Dim conn, rs
set conn=CreateObject("tion")
conn.Open "Driver={Microsoft Excel Driver (*.xls)};" & _
"DriverId=790;" & _
"Dbq=" & Server.mappath(""&FileName&"") & ";" & _
"DefaultDir=G:\"

set rs=createobject("dset")
rs.Open "Select * From [Sheet1$]",conn, 2, 2
f then
response.write "Excel表中无纪录"
else
set connDB = Server.CreateObject("ADODB.Connection")
DBPath = Server.MapPath("Excel.mdb")
'RESPONSE.WRITE DBpath
connDB.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath
Set RsDB = Server.CreateObject("ADODB.Recordset")
SQLDB="Select * from Excel"
RsDB.open SQLDB,connDB,1,3
do while not  rs.eof '利用循环读出数据
RsDB.addnew
RsDB("filename")=rs(0)
RsDB("id1")=rs(1)
RsDB("id2")=rs(2)
RsDB("id3")=rs(3)
RsDB("id4")=rs(4)
Rs.update
venext
rs.movenext
loop
'direct FileName
end if
vefirst
f then
response.write "数据库中无记录"
else
do while not RsDB.EOF
response.write RsDB("filename")&" "
response.write RsDB("id1")&" "
response.write RsDB("id2")&" "
response.write RsDB("id3")&" "
response.write RsDB("id4")&" "
response.write "<br>"
venext
loop
end if
rs.close
set rs=nothing
set conn=nothing
%>
最近为了方便前台对网站留言信息的记录和保存,于是就用ASP程序编写了一个把access数据导成为excel格式的数据。开始的时候也不知道怎么编写程序,在网上了很多都没有完全解决,于是对一些程序进行修改,最后还是完成了,现在拿来和大家分享一下!
'连接数据库
    <%
dim rs,sql,filename,fs,myfile,x
Set fs = server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename = Server.MapPath("download.xls")
'Response.Write filename
'--如果原来的EXCEL文件存在的话删除它
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
'--创建EXCEL文件
set myfile = fs.CreateTextFile(filename,true)
Set rs = Server.CreateObject("ADODB.Recordset")
'--从数据库中把你想放到EXCEL中的数据查出来
sql = "select user as 姓名,tel as 电话,time1 as 留言时间,adr as E_mail,qq as Q_Q from message where hit=0 order by id DESC "
rs.Open sql,conn,1,1
if rs.EOF and rs.BOF then
writeline函数else
dim strLine,responsestr
strLine=""
For each x in rs.fields
strLine = strLine & x.name & chr(9)
Next
'--将表的列名先写入EXCEL
myfile.writeline strLine
Do while Not rs.EOF
strLine=""
for each x in rs.Fields
strLine = strLine & x.value & chr(9)
next
'--将表的数据写入EXCEL
myfile.writeline strLine
rs.MoveNext
loop
end if
rs.Close
set rs = nothing
%>
<a href="download.xls">导出数据</a>
以上程序可以生成一个 download.xls 文件!导出的时候只要地址正确就可以正常下载!
希望对大家有帮助!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。