1、用Office组件完成。
sub Test()
dim objExcel
dim objExcelSheets,objExcelBook,objExcelSheet
if Isobject(objExcel) then
objExcel.Quit
set objExcel=nothing
end if
strsql="SELECT id, title, author,url FROM article_title"
adors.open strsql,adoconn,1,1
set objExcel=CreateObject("Excel.Application")
'打开Excel模板
objExcel.Workbooks.Open(server.mappath("Report.xls")) '打开Excel
objExcel.DisplayAlerts = false
objExcel.Application.Visible = false
set objExcelBook = objExcel.ActiveWorkBook
set objExcelSheets = objExcelBook.Worksheets
set objExcelSheet = objExcelBook.Sheets(1)
'写数据
On Error Resume Next
i=2
while not adors.EOF
j=1
for each x in adors.Fields
objExcelSheet.cells(i,j)=x.value
j=j+1
next
adors.MoveNext
i=i+1
wend
adors.Close
adoconn.Close
objExcel.ActiveWorkbook.save
filename=Server.Mappath("temp\"&month&"月统计.xls")
objExcel.Save(filename)
call downloadfile(filename)
On Error Resume Next
objExcel.Quit
set objExcel=Nothing
set objExcelBook=nothing
set objExcelSheet=nothing
set adors=nothing
set adoconn=nothing
response.write "报表生成成功"
end sub
2、用Table的方式导出Excel。这种方式服务器上不用装Office,只能输出Table。
sub stat()
strsql="SELECT id, title, author,url FROM article_title"
adors.open strsql,adoconn,1,1
strline=""
'循环相加数据
On Error Resume Next
while not adors.EOF
strline2=""
for each x in adors.Fields
strline2=strline2+""
next
adors.MoveNext
strline1=strline1+""+strline2+""
wend
strline=strline+strline1+"
编号 | 标题 | 作者 | 连接地址 |
"+Trim(CStr(x.value))+" |
|
"
response.Write strline
adors.Close
adoconn.Close
set adors=nothing
set adoconn=nothing
'一定要加上这句话。
Response.ContentType="application/vnd.ms-excel"
end sub