在进行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” 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服务器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 & “” else dim strstart, strend as string strstart = mid(strsql, instr(strsql, “(”) + 2, instr(strsql, “,”) – instr(strsql, “(”) – 3) strend = mid(strsql, instr(strsql, “,”) + 2, instr(strsql, “)”) – instr(strsql, “,”) – 3) strsql = “select * from jobs where job_id>=” & strstart & ” and job_id<=” & strend & “” end if end if nextstep: dim strconnect as string strconnect = “provider=sqloledb;server=ddk;uid=ydj;pwd=ydj; database=” & strdb & “;” cn.open strconnect rs.cursorlocation = aduseclient rs.open strsql, cn, adopenstatic, adlockoptimistic, adcmdtext set returnrs = rs exit function ehgetrecordset: err.raise err.number, err.source, err.description end function |
把rsget.dll复制到c:\windows或d:\winnt,开始\运行,输入regsvr32.exe c:\windows\rsget.dll或regsvr32.exe d:\winnt\rsget.dll,按确定按钮,注册成web服务器组件,并在注册表hkey_local_machine\system\currentcontrolset\ services\w3svc\parameters\adclaunch 添加rsget.rsreturn。
若使用自定义商业对象,修改上面的asp文件代码:
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 rds = createobject(”rds.dataspace”)
set df = rds.createobject(”rsget.rsreturn”,”http://192.168.0.1″) set rs=df.returnrs(”pubsdatabase”,”getpubsbyid(2,10)”) |
另外在浏览器端需做如下配置:
打开控制面板->internet选项->安全性->自定义级别-> 对没有标记为安全的activex控件进行初始化和脚本运行->开启