Asp将查询结果导出到excel里
作者:wang 日期:2009-10-19
是Office的OWC版本问题。2000,2002XP,2003各不相同,加入Office的版本检查即可,完整代码如下:
.....................................................................
<%
set conn=Server.CreateObject("adodb.connection")
connstr= "driver={SQL Server};server=192.168.100.207;uid=sa;pwd=szweb05;database=shizhu_2009"'//////链接数据库
conn.open connstr
dim rs,sql,filename,fs,myfile,x
Set fs=server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename=Server.MapPath("FT_User.xls")'/////数据表保存的文件名
'--如果原来的EXCEL文件存在的话删除它
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
'--创建EXCEL文件
set myfile=fs.CreateTextFile(filename,true)
'///////从数据库中把你想放到EXCEL中的数据查出来
Set rs=Server.CreateObject("ADODB.Recordset")
sql="select * from News"
rs.open sql,conn,1,1
if not rs.EOF then
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
Response.Write "
导出成功,请选择继续操作 "
response.Write ""
"
%>
.........................................................
<%
Response.Buffer = TRUE
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "content-disposition", "inline; filename = 用户信息.xls"
%>
............................................................
asp导出到excel
--------------------------------------------------------------------------------
在开头加上这一句
Response.ContentType = "application/vnd.ms-excel"
如果导出结果为空白的,那就去掉它
一下是导出Excel代码
--------------------------------------------------------------------------------
<%
set rs=server.createobject("adodb.recordset")
sql="select * from provinceinfo where 1=1"
rs.open sql,objconn,1,1
Set ExcelApp =CreateObject("Excel.Application")
ExcelApp.Application.Visible = True
Set ExcelBook = ExcelApp.Workbooks.Add
ExcelBook.WorkSheets(1).cells(1,1).value ="用户表"
ExcelBook.WorkSheets(1).cells(2,1).value = "用户编号"
ExcelBook.WorkSheets(1).cells(2,2).value = "登陆名"
ExcelBook.WorkSheets(1).cells(2,3).value = "真实姓名"
ExcelBook.WorkSheets(1).cells(2,4).value = "密码"
cnt =3
do while not rs.eof
ExcelBook.WorkSheets(1).cells(cnt,1).value = rs("provinceid")
ExcelBook.WorkSheets(1).cells(cnt,2).value = rs("province")
ExcelBook.WorkSheets(1).cells(cnt,3).value = rs("flag")
ExcelBook.WorkSheets(1).cells(cnt,4).value = rs("id")
rs.movenext
cnt = cint(cnt) + 1
loop
Excelbook.SaveAs "d:\yourfile.xls" '这个是数据导出完毕以后在D盘存成文件
ExcelApp.Application.Quit '导出以后退出Excel
Set ExcelApp = Nothing '注销Excel对象
%>
这是读取Excel表中数据例子
--------------------------------------------------------------------------------
sConn1="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("..")&"\temp\"&sNewFileName & ";Extended Properties=""Excel 8.0;HDR=NO;"""
oxls.Open "Select * FROM [sheet1$]",sConn1,1,3
oxls.movenext
oxls("f11")="1月"
oxls.update
...
oxls.Close
Set oxls=nothing
.....................................................................
<%
set conn=Server.CreateObject("adodb.connection")
connstr= "driver={SQL Server};server=192.168.100.207;uid=sa;pwd=szweb05;database=shizhu_2009"'//////链接数据库
conn.open connstr
dim rs,sql,filename,fs,myfile,x
Set fs=server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename=Server.MapPath("FT_User.xls")'/////数据表保存的文件名
'--如果原来的EXCEL文件存在的话删除它
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
'--创建EXCEL文件
set myfile=fs.CreateTextFile(filename,true)
'///////从数据库中把你想放到EXCEL中的数据查出来
Set rs=Server.CreateObject("ADODB.Recordset")
sql="select * from News"
rs.open sql,conn,1,1
if not rs.EOF then
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
Response.Write "
response.Write "
" response.write ("√下载") & " √关闭" Response.Write " |
%>
.........................................................
<%
Response.Buffer = TRUE
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "content-disposition", "inline; filename = 用户信息.xls"
%>
............................................................
asp导出到excel
--------------------------------------------------------------------------------
在开头加上这一句
Response.ContentType = "application/vnd.ms-excel"
如果导出结果为空白的,那就去掉它
一下是导出Excel代码
--------------------------------------------------------------------------------
<%
set rs=server.createobject("adodb.recordset")
sql="select * from provinceinfo where 1=1"
rs.open sql,objconn,1,1
Set ExcelApp =CreateObject("Excel.Application")
ExcelApp.Application.Visible = True
Set ExcelBook = ExcelApp.Workbooks.Add
ExcelBook.WorkSheets(1).cells(1,1).value ="用户表"
ExcelBook.WorkSheets(1).cells(2,1).value = "用户编号"
ExcelBook.WorkSheets(1).cells(2,2).value = "登陆名"
ExcelBook.WorkSheets(1).cells(2,3).value = "真实姓名"
ExcelBook.WorkSheets(1).cells(2,4).value = "密码"
cnt =3
do while not rs.eof
ExcelBook.WorkSheets(1).cells(cnt,1).value = rs("provinceid")
ExcelBook.WorkSheets(1).cells(cnt,2).value = rs("province")
ExcelBook.WorkSheets(1).cells(cnt,3).value = rs("flag")
ExcelBook.WorkSheets(1).cells(cnt,4).value = rs("id")
rs.movenext
cnt = cint(cnt) + 1
loop
Excelbook.SaveAs "d:\yourfile.xls" '这个是数据导出完毕以后在D盘存成文件
ExcelApp.Application.Quit '导出以后退出Excel
Set ExcelApp = Nothing '注销Excel对象
%>
这是读取Excel表中数据例子
--------------------------------------------------------------------------------
sConn1="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("..")&"\temp\"&sNewFileName & ";Extended Properties=""Excel 8.0;HDR=NO;"""
oxls.Open "Select * FROM [sheet1$]",sConn1,1,3
oxls.movenext
oxls("f11")="1月"
oxls.update
...
oxls.Close
Set oxls=nothing
[本日志由 wang 于 2009-10-20 10:26 AM 编辑]
上一篇: 通过asp把excel数据导入mssql数据库成功下一篇: 把WebForm数据导出到Excel中
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 6864
发表评论