分享

我在asp中用excel 组件制作web报表,Set ExAp=Server.Create...

 E网天涯 2009-11-30
给你个例子看看先  
  在进行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>  
  如果错误是检查权限时Server.CreateObject   的调用失败。对此对象的访问被拒,有可能是没权限.那就  
  因为IIS没有权限调用Excel的缘故。  
  运行dcomcnfg.exe,配置Excel的“安全性”->默认访问权限,默认启动权限,默认配置权限为“自定义”,并在可访问用户中添加上IIS的账号就OK啦   
   

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约