ASP网站远程客户实现EXCEL打印功能
程序员文章站
2022-06-10 18:18:54
在进行asp网站开发时,有时需在客户端调用(windows平台上强大的平台)数据库的数据进行打印,若调用数据量小,可以通过在客户端运用filesystemobject生成文件对象的方法实现打印,这里...
在进行asp网站开发时,有时需在客户端调用(windows平台上强大的平台)数据库的数据进行打印,若调用数据量小,可以通过在客户端运用filesystemobject生成文件对象的方法实现打印,这里不再赘述。若需调用大量数据,可在客户端脚本中实例化rds.dataspace(remote data service)对象,并采用远程提供程序通过asp网站访问mssql(windows平台上强大的数据库平台)数据库(设置成只能通过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">
<p 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>
</p>
<form name="myform">
<div align=left>
<input type="button" value="excel report" name="report" language="vbscript" onclick="fun_excel()" style="height: 32px; width: 90px">
</p>
</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","https://192.168.0.1") 192.168.0.1 为web服务器ip地址
strcn="provider=ms remote;remote server=https://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","https://192.168.0.1") 192.168.0.1 为web服务器ip地址
strcn="provider=ms remote;remote server=https://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 = "https://192.168.0.1"
.handler = "msdfmap.handler"
.connect = "data source=pubsdatabase;"
.sql = "getalljobs"
.refresh
end with
set rs= dc.recordset
修改文件msdfmap.ini(若在win98,c:windowsmsdfmap.ini;若在win2000,d:winntmsdfmap.ini;若在win2000 server,d:winntsmsdfmap.ini)。
[sql getalljobs]
sql="select * from jobs"
[connect pubsdatabase]
access(小型网站之最爱)=readonly
connect="provider=sqloledb;data source=sql server(windows平台上强大的数据库平台);initial catalog=pubs;uid=userid;pwd=password"
打开注册表hkey_local_machinesystemcurrentcontrolset servicesw3svcparametersadclaunch 若无 rdsserver.datafactory,请添加。本例使用rds default handler访问数据库,若不通过rds handler访问数据库,修改注册表hkey_local_machinesoftwaremicrosoftdatafactory handlerinfo 将handlerrequired=1 设置成handlerrequired =0。请注意,若不通过rds handler或自定义商业对象访问数据库,将对数据库带来安全隐患,所以作者极力推荐采用只能通过rds handler或自定义商业对象才能访问数据库的方式。
<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">
<p 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>
</p>
<form name="myform">
<div align=left>
<input type="button" value="excel report" name="report" language="vbscript" onclick="fun_excel()" style="height: 32px; width: 90px">
</p>
</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","https://192.168.0.1") 192.168.0.1 为web服务器ip地址
strcn="provider=ms remote;remote server=https://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","https://192.168.0.1") 192.168.0.1 为web服务器ip地址
strcn="provider=ms remote;remote server=https://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 = "https://192.168.0.1"
.handler = "msdfmap.handler"
.connect = "data source=pubsdatabase;"
.sql = "getalljobs"
.refresh
end with
set rs= dc.recordset
修改文件msdfmap.ini(若在win98,c:windowsmsdfmap.ini;若在win2000,d:winntmsdfmap.ini;若在win2000 server,d:winntsmsdfmap.ini)。
[sql getalljobs]
sql="select * from jobs"
[connect pubsdatabase]
access(小型网站之最爱)=readonly
connect="provider=sqloledb;data source=sql server(windows平台上强大的数据库平台);initial catalog=pubs;uid=userid;pwd=password"
打开注册表hkey_local_machinesystemcurrentcontrolset servicesw3svcparametersadclaunch 若无 rdsserver.datafactory,请添加。本例使用rds default handler访问数据库,若不通过rds handler访问数据库,修改注册表hkey_local_machinesoftwaremicrosoftdatafactory handlerinfo 将handlerrequired=1 设置成handlerrequired =0。请注意,若不通过rds handler或自定义商业对象访问数据库,将对数据库带来安全隐患,所以作者极力推荐采用只能通过rds handler或自定义商业对象才能访问数据库的方式。