如何在asp中把數(shù)據(jù)庫(kù)內(nèi)容導(dǎo)出到excel?
就這么簡(jiǎn)單的代碼,我居然弄了一個(gè)下午和一個(gè)晚上,還好,總算解決了,現(xiàn)把過(guò)程總結(jié)如下:
1.完整代碼:
<%
set rs=server.createobject("adodb.recordset")
sql="select * from deptinfo"
rs.open sql,conn,1,1
Set ExcelApp=server.CreateObject("Excel.Application") '就這句害人?。?!
ExcelApp.Application.Visible = True
Set ExcelBook = ExcelApp.Workbooks.Add
ExcelBook.WorkSheets(1).cells(1,1).value ="部門(mén)表"
ExcelBook.WorkSheets(1).cells(2,1).value = "部門(mén)編號(hào)"
ExcelBook.WorkSheets(1).cells(2,2).value = "部門(mén)名稱(chēng)"
ExcelBook.WorkSheets(1).cells(2,3).value = "負(fù)責(zé)人"
ExcelBook.WorkSheets(1).cells(2,4).value = "書(shū)記"
ExcelBook.WorkSheets(1).cells(2,5).value = "其他負(fù)責(zé)人"
ExcelBook.WorkSheets(1).cells(2,6).value = "是否教學(xué)部門(mén)"
cnt =3
do while not rs.eof
ExcelBook.WorkSheets(1).cells(cnt,1).value = rs("deptid")
ExcelBook.WorkSheets(1).cells(cnt,2).value = rs("deptname")
ExcelBook.WorkSheets(1).cells(cnt,3).value = rs("governor")
ExcelBook.WorkSheets(1).cells(cnt,4).value = rs("g1")
ExcelBook.WorkSheets(1).cells(cnt,5).value = rs("g2")
ExcelBook.WorkSheets(1).cells(cnt,6).value = rs("istechdept")
rs.movenext
cnt = cint(cnt) + 1
loop
rs.close
set rs=nothing
Excelbook.SaveAs "d:\yourfile.xls" '這個(gè)是數(shù)據(jù)導(dǎo)出完畢以后在D盤(pán)存成文件
ExcelApp.Application.Quit '導(dǎo)出以后退出Excel
Set ExcelApp = Nothing '注銷(xiāo)Excel對(duì)象
response.Write "導(dǎo)出成功!"
%>
2.調(diào)試時(shí),Set ExcelApp=server.CreateObject("Excel.Application")
這一句總是出現(xiàn)以下錯(cuò)誤:
運(yùn)行,產(chǎn)生錯(cuò)誤
錯(cuò)誤類(lèi)型:
Server 對(duì)象, ASP 0178 (0x80070005)
檢查權(quán)限時(shí),對(duì) Server.CreateObject 的調(diào)用失敗。拒絕對(duì)此對(duì)象的訪問(wèn)。
3.解決方案:
管理工具-->組件服務(wù)-->計(jì)算機(jī)-->我的電腦-->DCOM配置
(1)選擇Microsoft Excel 應(yīng)用程序 -->屬性
標(biāo)識(shí):選擇交互式用戶
安全:啟動(dòng)和激活權(quán)限-->自定義,加入InterNet來(lái)賓帳戶,勾上本地啟動(dòng)和本地激活
Internet來(lái)賓賬戶的添加方法如下:
(2)選擇Microsoft Office Excel 2007 工作簿 -->屬性
常規(guī):身份驗(yàn)證級(jí)別 -->無(wú)
聯(lián)系客服