在進行ASP網(wǎng)站開發(fā)時,有時需在客戶端調用MSSQL數(shù)據(jù)庫的數(shù)據(jù)進行打印,若調用數(shù)據(jù)量小,可以通過在客戶端運用FileSystemObject生成文件對象的方法實現(xiàn)打印,這里不再贅述。若需調用大量數(shù)據(jù),可在客戶端腳本中實例化RDS.DataSpace(Remote Data Service)對象,并采用遠程提供程序通過ASP網(wǎng)站訪問MSSQL數(shù)據(jù)庫(設置成只能通過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><center>
<table bgcolor="#ffe4b5" bordercolor="#0000ff">
<tr>
<td 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()">
</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>
<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><center>
<table bgcolor="#ffe4b5" bordercolor="#0000ff">
<tr>
<td 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()">
</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>