我在asp中用excel 組件制作web報表,Set ExAp=Server.Create...
給你個例子看看先
在進行ASP網(wǎng)站開發(fā)時,有時需在客戶端調(diào)用MSSQL數(shù)據(jù)庫的數(shù)據(jù)進行打印,若調(diào)用數(shù)據(jù)量小,可以通過在客戶端運用FileSystemObject生成文件對象的方法實現(xiàn)打印,這里不再贅述。若需調(diào)用大量數(shù)據(jù),可在客戶端腳本中實例化RDS.DataSpace(Remote Data Service)對象,并采用遠(yuǎn)程提供程序通過ASP網(wǎng)站訪問MSSQL數(shù)據(jù)庫(設(shè)置成只能通過RDS Default Handler或自定義商業(yè)對象才能訪問數(shù)據(jù)庫,可保證數(shù)據(jù)庫的安全),再在客戶端實例化EXCEL.APPLICATION對象,把數(shù)據(jù)集中的數(shù)據(jù)寫入EXCEL中,再進行保存或打印。代碼如下:
<html>
<head>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<title>客戶端電子表格打印</title>
</head>
<body bgColor=skyblue topMargin=5 leftMargin="20" oncontextmenu="return false" rightMargin=0 bottomMargin="0">
<div align="center"><center>
<table border="1" bgcolor="#ffe4b5" style="HEIGHT: 1px; TOP: 0px" bordercolor="#0000ff">
<tr>
<td align="middle" bgcolor="#ffffff" bordercolor="#000080">
<font color="#000080" size="3">
客戶端電子表格打印
</font>
</td>
</tr>
</table>
</div>
<form name="myform">
<DIV align=left>
<input type="button" value="Excel Report" name="report" language="vbscript" onclick="fun_excel()" style="HEIGHT: 32px; WIDTH: 90px">
</div>
</form>
</body>
</html>
<script language="vbscript">
sub fun_excel()
Dim rds,rs,df
dim strCn,strSQL,StrRs
Dim xlApp, xlBook, xlSheet1
set rds = CreateObject("RDS.DataSpace")
Set df = rds.CreateObject("RDSServer.DataFactory","http://192.168.0.1") '192.168.0.1 為WEB服務(wù)器IP地址
strcn="provider=ms remote;remote server=http://192.168.0.1;handler=msdfmap.handler;data source=pubsdatabase;" '192.168.0.1 為WEB服務(wù)器IP地址
strsql= "getalljobs"
Set rs = df.Query(strCn, strSQL)
Set xlApp = CreateObject("EXCEL.APPLICATION") '注意不是:Server.CreateObject("EXCEL.APPLICATION")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet1 = xlBook.Worksheets(1)
xlSheet1.cells(1,1).value ="職務(wù)表"
xlSheet1.range("A1:D1").merge
xlSheet1.cells(2,1).value = "job_id"
xlSheet1.cells(2,2).value = "job_desc"
xlSheet1.cells(2,3).value = "max_lvl"
xlSheet1.cells(2,4).value = "min_lvl"
cnt =3
do while not rs.eof
xlSheet1.cells(cnt,1).value = rs("job_id")
xlSheet1.cells(cnt,2).value = rs("job_desc")
xlSheet1.cells(cnt,3).value = rs("max_lvl")
xlSheet1.cells(cnt,4).value = rs("min_lvl")
rs.movenext
cnt = cint(cnt) + 1
loop
xlSheet1.Application.Visible = True
end sub
</script>
如果錯誤是檢查權(quán)限時Server.CreateObject 的調(diào)用失敗。對此對象的訪問被拒,有可能是沒權(quán)限.那就
因為IIS沒有權(quán)限調(diào)用Excel的緣故。
運行dcomcnfg.exe,配置Excel的“安全性”->默認(rèn)訪問權(quán)限,默認(rèn)啟動權(quán)限,默認(rèn)配置權(quán)限為“自定義”,并在可訪問用戶中添加上IIS的賬號就OK啦
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。