ASP中把數(shù)據(jù)導(dǎo)出為Excel的三種方法
方法一:用excel組件
這種方法利用Excel組件直接導(dǎo)出excel文件,要求服務(wù)器端安裝有微軟office(Excel)程序,否則無(wú)法運(yùn)行。
完整示例如下:
Set conn=server.CreateObject("adodb.connection")
connstr="provider=microsoft.jet.oledb.4.0;data source="& Server.MapPath("/db.mdb")
conn.open connstr
set rs=server.createobject("adodb.recordset")
sql="select * from xiaozu"
rs.open sql,conn,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(1,2).value = "學(xué)生名單"
ExcelBook.WorkSheets(1).cells(1,3).value = "所屬學(xué)院"
ExcelBook.WorkSheets(1).cells(1,4).value = "實(shí)習(xí)景區(qū)"
cnt = 2
do while not rs.eof
ExcelBook.WorkSheets(1).cells(cnt,1).value = rs("XZName")
ExcelBook.WorkSheets(1).cells(cnt,2).value = rs("XZStudents")
ExcelBook.WorkSheets(1).cells(cnt,3).value = rs("XZCollage")
ExcelBook.WorkSheets(1).cells(cnt,4).value = rs("XZJD")
rs.movenext
cnt = cint(cnt) + 1
loop
Excelbook.SaveAs "d:\yourfile.xls" '這個(gè)是數(shù)據(jù)導(dǎo)出完畢以后在D盤存成文件
ExcelApp.Application.Quit '導(dǎo)出以后退出Excel
Set ExcelApp = Nothing '注銷Excel對(duì)象
rs.close
set rs = nothing
conn.close
set conn = nothing
方法二:使用文件組件
這種方法導(dǎo)出的是文本文件,只不過后綴名改成了xls。
完整示例如下:
Set conn=server.CreateObject("adodb.connection")
connstr="provider=microsoft.jet.oledb.4.0;data source="& Server.MapPath("/db.mdb")
conn.open connstr
dim s,sql,filename,fs,myfile,x
Set fs = server.CreateObject("scripting.filesystemobject")
'--假設(shè)你想讓生成的EXCEL文件做如下的存放
filename = Server.MapPath("order.xls")
'--如果原來的EXCEL文件存在的話刪除它
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
'--創(chuàng)建EXCEL文件
set myfile = fs.CreateTextFile(filename,true)
StartTime = Request("StartTime")
EndTime = Request("EndTime")
StartEndTime = "AddTime between #"& StartTime &" 00:00:00# and #"& EndTime &" 23:59:59#"
strSql = "select * from xiaozu "
Set rstData =conn.execute(strSql)
if not rstData.EOF and not rstData.BOF then
dim strLine,responsestr
strLine=""
For each x in rstData.fields
strLine = strLine & x.name & chr(9)
Next
'--將表的列名先寫入EXCEL
myfile.writeline strLine
Do while Not rstData.EOF
strLine=""
for each x in rstData.Fields
strLine = strLine & x.value & chr(9)
next
myfile.writeline strLine
rstData.MoveNext
loop
end if
Response.Write "生成EXCEL文件成功,點(diǎn)擊<a href='order.xls' target='_blank'>下載!"
rstData.Close
set rstData = nothing
Conn.Close
Set Conn = nothing
方法三:
該方法不使用任何組件。而是將所有導(dǎo)出的數(shù)據(jù)在網(wǎng)頁(yè)中以Table進(jìn)行顯示,然后增加如下兩行代碼,即可實(shí)現(xiàn)打開網(wǎng)頁(yè)后直接下載保存為Excel:
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment;filename=TestExcel.xls"
完整示例如下:
<%
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment;filename=TestExcel.xls"
Set conn=server.CreateObject("adodb.connection")
connstr="provider=microsoft.jet.oledb.4.0;data source="& Server.MapPath("/db.mdb")
conn.open connstr
set rs=server.createobject("adodb.recordset")
sql="select * from xiaozu"
rs.open sql,conn,1,1
%>
<table border="1">
<tr>
<td>小組名稱</td>
<td>學(xué)生名單</td>
<td>所屬學(xué)院</td>
<td>實(shí)習(xí)景區(qū)</td>
</tr>
<%
while not rs.eof and not rs.bof
%>
<tr>
<td><%=rs("XZName")%></td>
<td><%=rs("XZStudents")%></td>
<td><%=rs("XZCollage")%></td>
<td><%=rs("XZJD")%></td>
</tr>
<% rs.movenext
wend
%>
</table>
<%
rs.close
set rs = nothing
conn.close
set conn = nothing
%>
————————————————
版權(quán)聲明:本文為CSDN博主「qhdzj87」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qhdzj87/article/details/122541475

浙公網(wǎng)安備 33010602011771號(hào)