«
ASP网站远程客户实现EXCEL打印功能

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  在进行ASP网站开发时,有时需在客户端调用MSSQL数据库的数据进行打印,若调用数据量小,可以通过在客户端运用FileSystemObject生成文件对象的方法实现打印,这里不再赘述。若需调用大量数据,可在客户端脚本中实例化RDS.DataSpace(Remote Data Service)对象,并采用远程提供程序通过ASP网站访问MSSQL数据库(设置成只能通过RDS Default Handler或自定义商业对象才能访问数据库,可保证数据库的安全),再在客户端实例化EXCEL.APPLICATION对象,把数据集中的数据写入EXCEL中,再进行保存或打印。代码如下: </p><p>  <html> </p><p>  <head> </p><p>  <META content="text/html; charset=gb2312" http-equiv=Content-Type> </p><p>  <title>客户端电子表格打印</title> </p><p>  </head> </p><p>  <body bgColor=skyblue topMargin=5 leftMargin="20" oncontextmenu="return false" rightMargin=0 bottomMargin="0"> </p><p>  <div align="center"><center> </p><p>  <table border="1" bgcolor="#ffe4b5" style="HEIGHT: 1px; TOP: 0px" bordercolor="#0000ff"> </p><p>  <tr> </p><p>  <td align="middle" bgcolor="#ffffff" bordercolor="#000080"> </p><p>  <font color="#000080" size="3"> </p><p>  客户端电子表格打印 </p><p>  </font> </p><p>  </td> </p><p>  </tr> </p><p>  </table> </p><p>  </div> </p><p>  <form name="myform"> </p><p>  <DIV align=left> </p><p>  <input type="button" value="Excel Report" name="report" language="vbscript" onclick="fun_excel()" style="HEIGHT: 32px; WIDTH: 90px"> </p>
<p> </p>

   <p>  </div> </p><p>  </form> </p><p>  </body> </p><p>  </html> </p><p>  <script language="vbscript"> </p><p>  sub fun_excel() </p><p>  Dim rds,rs,df </p><p>  dim strCn,strSQL,StrRs </p><p>  Dim xlApp, xlBook, xlSheet1 </p><p>  set rds = CreateObject("RDS.DataSpace") </p><p>  Set df = rds.CreateObject("RDSServer.DataFactory","<a href="http://192.168.0.1/">http://192.168.0.1</a>") '192.168.0.1 为WEB服务器IP地址 </p><p>  strcn="provider=ms remote;remote server=http://192.168.0.1;handler=msdfmap.handler;data source=pubsdatabase;" '192.168.0.1 为WEB服务器IP地址 </p><p>  strsql= "getalljobs" </p><p>  Set rs = df.Query(strCn, strSQL) </p><p>  <br />Set xlApp = CreateObject("EXCEL.APPLICATION") '注意不是:Server.CreateObject("EXCEL.APPLICATION") </p><p>  Set xlBook = xlApp.Workbooks.Add </p><p>  Set xlSheet1 = xlBook.Worksheets(1) </p><p>  xlSheet1.cells(1,1).value ="职务表" </p><p>  xlSheet1.range("A1:D1").merge </p><p>  xlSheet1.cells(2,1).value = "job_id" </p><p>  xlSheet1.cells(2,2).value = "job_desc" </p><p>  xlSheet1.cells(2,3).value = "max_lvl" </p><p>  xlSheet1.cells(2,4).value = "min_lvl" </p><p>  cnt =3 </p><p>  do while not rs.eof </p><p>  xlSheet1.cells(cnt,1).value = rs("job_id") </p><p>  xlSheet1.cells(cnt,2).value = rs("job_desc") </p>
 <p> </p>

   <p>  xlSheet1.cells(cnt,3).value = rs("max_lvl") </p><p>  xlSheet1.cells(cnt,4).value = rs("min_lvl") </p><p>  rs.movenext </p><p>  cnt = cint(cnt) + 1 </p><p>  loop </p><p>  xlSheet1.Application.Visible = True </p><p>  end sub </p><p>  </script> </p><p>  也可以实例化RDS DataControl,只需把以上部分代码进行修改: </p><p>  set rds = CreateObject("RDS.DataSpace") </p><p>  Set df = rds.CreateObject("RDSServer.DataFactory","<a href="http://192.168.0.1/">http://192.168.0.1</a>") '192.168.0.1 为WEB服务器IP地址 </p><p>  strcn="provider=ms remote;remote server=http://192.168.0.1;handler=msdfmap.handler;data source=pubsdatabase;" '192.168.0.1 为WEB服务器IP地址 </p><p>  strsql= "getalljobs" </p><p>  Set rs = df.Query(strCn, strSQL) </p><p>  修改为: </p><p>  set DC = createobject("RDS.DataControl") </p><p>  dc.ExecuteOptions =1 '设置成同步执行,可以简化下步代码 </p><p>  dc.FetchOptions = 1 </p><p>  With dc </p><p>  .Server = "<a href="http://192.168.0.1/">http://192.168.0.1</a>" </p><p>  .Handler = "MSDFMAP.Handler" </p><p>  .Connect = "Data Source=pubsdatabase;" </p><p>  .Sql = "getalljobs" </p><p>  .Refresh </p><p>  End With </p><p>  set rs= dc.Recordset </p><p>  修改文件MSDFMAP.INI(若在WIN98,C:\windows\msdfmap.ini;若在WIN2000,D:\winnt\msdfmap.ini;若在WIN2000 SERVER,D:\winnts\msdfmap.ini)。 </p>
 <p> </p>

   <p>  [sql getalljobs] </p><p>  Sql="SELECT * FROM jobs" </p><p>  <br />[connect pubsDatabase] </p><p>  Access=Readonly </p><p>  Connect="provider=sqloledb;data source=sql server;initial catalog=pubs;UID=userid;PWD=password" </p><p>  打开注册表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或自定义商业对象才能访问数据库的方式。 </p><p>  下面用VB编写一个自定义商业对象,代码如下: </p><p>  '编写ActiveX DLL,名称:rsget.dll,包含类rsreturn,方法returnrs </p><p>  Public Function ReturnRs(strDB As Variant, strSQL As Variant) As ADODB.Recordset </p><p>  'Returns an ADODB recordset. </p><p>  On Error GoTo ehGetRecordset </p><p>  Dim cn As New ADODB.Connection </p><p>  Dim rs As New ADODB.Recordset </p><p>  Select Case strDB </p><p>  Case "ydjjspdatabase" </p><p>  strDB = "ydjjsp" </p><p>  Case "pubsdatabase" </p><p>  strDB = "pubs" </p><p>  End Select </p><p>  <br />If strSQL = "getallbuy" Then </p><p>  strSQL = "select * from buyuser" </p><p>  GoTo nextstep </p><p>  End If </p>
 <p> </p>

   <p>  If Left(strSQL, InStr(strSQL, "(") - 1) = "getpubsbyid" Then </p><p>  If InStr(strSQL, ",") <= 0 Then </p><p>  Dim str As String </p><p>  str = Mid(strSQL, InStr(strSQL, "(") + 2, InStr(strSQL, ")") - InStr(strSQL, "(") - 3) </p><p>  strSQL = "select * from jobs where job_id='" & str & "'" </p><p>  Else </p><p>  Dim strstart, strend As String </p><p>  strstart = Mid(strSQL, InStr(strSQL, "(") + 2, InStr(strSQL, ",") - InStr(strSQL, "(") - 3) </p><p>  strend = Mid(strSQL, InStr(strSQL, ",") + 2, InStr(strSQL, ")") - InStr(strSQL, ",") - 3) </p><p>  strSQL = "select * from jobs where job_id>='" & strstart & "' and job_id<='" & strend & "'" </p><p>  End If </p><p>  End If </p><p>  nextstep: </p><p>  Dim strConnect As String </p><p>  strConnect = "Provider=SQLOLEDB;Server=ddk;uid=ydj;pwd=ydj; Database=" & strDB & ";" </p><p>  cn.Open strConnect </p><p>  rs.CursorLocation = adUseClient </p><p>  rs.Open strSQL, cn, adOpenStatic, adLockOptimistic, adCmdText </p><p>  Set ReturnRs = rs </p><p>  Exit Function </p><p>  ehGetRecordset: </p><p>  Err.Raise Err.Number, Err.Source, Err.Description </p><p>  End Function </p><p>  把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。 </p></p><p>  若使用自定义商业对象,修改上面的ASP文件代码: </p><p>  set rds = CreateObject("RDS.DataSpace") </p><p>  Set df = rds.CreateObject("RDSServer.DataFactory","<a href="http://192.168.0.1/">http://192.168.0.1</a>") '192.168.0.1 为WEB服务器IP地址 </p><p>  strcn="provider=ms remote;remote server=http://192.168.0.1;handler=msdfmap.handler;data source=pubsdatabase;" '192.168.0.1 为WEB服务器IP地址 </p><p>  strsql= "getalljobs" </p><p>  Set rs = df.Query(strCn, strSQL) </p><p>  改为: </p><p>  set rds = CreateObject("RDS.DataSpace") </p><p>  Set df = rds.CreateObject("rsget.rsreturn","<a href="http://192.168.0.1/">http://192.168.0.1</a>") </p><p>  set rs=df.returnrs("pubsdatabase","getpubsbyid('2','10')") </p><p>  另外在浏览器端需做如下配置: </p><p>  打开控制面板->INTERNET选项->安全性->自定义级别-> 对没有标记为安全的ActiveX控件进行初始化和脚本运行->开启</p></p>