excel|列印 在進行ASP網站開發時,有時需在用戶端調用MSSQL資料庫的資料進行列印,若調用資料量小,可以通過在用戶端運用FileSystemObject組建檔案對象的方法實現列印,這裡不再贅述。若需調用大量資料,可在用戶端指令碼中執行個體化RDS.DataSpace(Remote Data Service)對象,並採用遠程提供者通過ASP網站訪問MSSQL資料庫(設定成只能通過RDS Default Handler或自訂商業對象才能訪問資料庫,可保證資料庫的安全),再在用戶端執行個體化EXCEL.APPLICATION對象,把資料集中的資料寫入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" 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伺服器IP地址
strcn="provider=ms remote;remote server=http://192.168.0.1;handler=msdfmap.handler;data source=pubsdatabase;" '192.168.0.1 為WEB伺服器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 ="職務表"
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>
也可以執行個體化RDS DataControl,只需把以上部分代碼進行修改:
set rds = CreateObject("RDS.DataSpace")
Set df = rds.CreateObject("RDSServer.DataFactory","http://192.168.0.1") '192.168.0.1 為WEB伺服器IP地址
strcn="provider=ms remote;remote server=http://192.168.0.1;handler=msdfmap.handler;data source=pubsdatabase;" '192.168.0.1 為WEB伺服器IP地址
strsql= "getalljobs"
Set rs = df.Query(strCn, strSQL)
修改為:
set DC = createobject("RDS.DataControl")
dc.ExecuteOptions =1 '設定成同步執行,可以簡化下步代碼
dc.FetchOptions = 1
With dc
.Server = "http://192.168.0.1"
.Handler = "MSDFMAP.Handler"
.Connect = "Data Source=pubsdatabase;"
.Sql = "getalljobs"
.Refresh
End With
set rs= dc.Recordset
修改檔案MSDFMAP.INI(若在WIN98,C:\windows\msdfmap.ini;若在WIN2000,D:\winnt\msdfmap.ini;若在WIN2000 SERVER,D:\winnts\msdfmap.ini)。
[sql getalljobs]
Sql="SELECT * FROM jobs"
[connect pubsDatabase]
Access=Readonly
Connect="provider=sqloledb;data source=sql server;initial catalog=pubs;UID=userid;PWD=password"
開啟註冊表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ Services\W3SVC\Parameters\ADCLaunch 若無 RDSServer.Datafactory,請添加。本例使用RDS Default Handler訪問資料庫,若不通過RDS Handler訪問資料庫,修改註冊表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DataFactory\ HandlerInfo 將HandlerRequired=1 設定成HandlerRequired =0。請注意,若不通過RDS Handler或自訂商業對象訪問資料庫,將對資料庫帶來安全隱患,所以作者極力推薦採用只能通過RDS Handler或自訂商業對象才能訪問資料庫的方式。
下面用VB編寫一個自訂商業對象,代碼如下:
'編寫ActiveX DLL,名稱:rsget.dll,包含類rsreturn,方法returnrs
Public Function ReturnRs(strDB As Variant, strSQL As Variant) As ADODB.Recordset
'Returns an ADODB recordset.
On Error GoTo ehGetRecordset
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Select Case strDB
Case "ydjjspdatabase"
strDB = "ydjjsp"
Case "pubsdatabase"
strDB = "pubs"
End Select
If strSQL = "getallbuy" Then
strSQL = "select * from buyuser"
GoTo nextstep
End If
If Left(strSQL, InStr(strSQL, "(") - 1) = "getpubsbyid" Then
If InStr(strSQL, ",") <= 0 Then
Dim str As String
str = Mid(strSQL, InStr(strSQL, "(") + 2, InStr(strSQL, ")") - InStr(strSQL, "(") - 3)
strSQL = "select * from jobs where job_id='" & str & "'"
E