<p> 在asp中利用excel的一个方法是将excel文件作为一个数据库进行链接,然后的操作和对access数据库操作类似。但是这个方法不是总能有用的,应为excel不是关系型的数据库。对于一个固定格式,这个格式里有复杂的单元格合并,边框线条样式,还有图案,单元格之间还有公式关系等等的,我想最容易理解的就是在后台打开一个已有设定好模板的文件,然后在需要的地方插入数据,保存,输出... ... </p><p> 这里提到的这种方法是直接建立一个excel对象,在后台可以更方便的对excel文档进行各种操作。</p><p> 服务器端还必须要设置的一点是COM组件的操作权限。在命令行键入“DCOMCNFG”,则进入COM组件配置界面,选择 MicrosoftExcel后点击属性按钮,将三个单选项一律选择自定义,编辑中将Everyone加入所有权限。保存完毕后重新启动服务器。 </p><p> 很重要,不设置的话,别的电脑将无法完成excel对象的建立。</p><p> 但是这种方法也有一个缺憾,在我实际操作中,如果服务器上已有打开的excel文件,再执行同一文件,就会出错,暂时还搞不明白为何,可能还有一些地方没设置好吧。</p><p> 另外,上面引用的文章中实例代码格式不太完整,其中很多换行和空格格式不准确,如果代码完整,只要复制代码,就可以运行成功,然后再慢慢进行研究修改,就很容易上手了,现把修改后的代码如下(删去了其中画图表的部分):</p><p><code> <%<br /> On Error Resume Next<br /> strAddr=Server.MapPath(".")<br /> set objExcelApp=CreateObject("Excel.Application")<br /> objExcelApp.DisplayAlerts=false<br /> objExcelApp.Application.Visible=false<br /> objExcelApp.WorkBooks.Open(strAddr&"TempletNull.xls")<br /> set objExcelBook=objExcelApp.ActiveWorkBook<br /> set objExcelSheets=objExcelBook.Worksheets<br /> set objExcelSheet=objExcelBook.Sheets(1)<br /> objExcelSheet.Range("B2:k2").Value=Array("Week1","Week2","Week3","Week4","Week5","Week6","Week7")<br /> objExcelSheet.Range("B3:k3").Value=Array("67","87","5","9","7","45","45","54","54","10")<br /> objExcelSheet.Range("B4:k4").Value=Array("10","10","8","27","33","37","50","54","10","10")<br /> objExcelSheet.Range("B5:k5").Value=Array("23","3","86","64","60","18","5","1","36","80")<br /> objExcelSheet.Cells(3,1).Value="InternetExplorer"<br /> objExcelSheet.Cells(4,1).Value="Netscape"<br /> objExcelSheet.Cells(5,1).Value="Other"<br /> objExcelSheet.Range("b2:k5").Select<br /> SaveAs(strAddr&"TempExcel.xls")<br /> objExcelApp.Quit<br /> set objExcelApp=Nothing<br /> %><br /> <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN"><br /> <HTML><br /> <HEAD><br /> <TITLE>NewDocument</TITLE><br /> <METANAME="Generator"CONTENT=" MicrosoftFrontPage5.0"><br /> <METANAME="Author"CONTENT=""><br /> <METANAME="Keywords"CONTENT=""><br /> <METANAME="Description"CONTENT=""><br /> </HEAD><br /> <BODY><br /> </BODY><br /> </HTML></code><p> 操作完excel文件需要将文件进行输出,实际弄一下,用ASP的redirect到excel方法,有时候是在IE中直接打开,有时候是弹出"下载 打开 保存"窗口.如果需要直接在IE中打开,不放用FSO载入excel文件,然后在IE中输出。</p>
<p> </p>
<p><code> <%<br /> Dim Fso,FileExt,strFilePath,Mime<br /> strFilePath = "f:aspxuexi.doc"<br /> Set Fso=Server.CreateObject("Scripting.FileSystemObject")<br /> FileExt = Fso.GetExtensionName(strFilePath)<br /> Set fso=Nothing<br /> Select Case FileExt<br /> Case "doc"<br /> Mime="Application/msword"<br /> Case "xls"<br /> Mime="Application/msexcel"<br /> End Select<br /> Call OutPut(strFilePath,Mime)<br /> '######################################################<br /> Function OutPut(strFilePath,Mime)<br /> Response.ContentType = Mime<br /> Const adTypeBinary = 1<br /> Set objStream = Server.CreateObject("ADODB.Stream")<br /> objStream.Open<br /> objStream.Type = adTypeBinary<br /> objStream.LoadFromFile strFilePath<br /> Response.BinaryWrite objStream.Read<br /> objStream.Close<br /> Set objStream = Nothing<br /> End Function<br /> '######################################################<br /> %></code><p> 在实际应用中。当服务器端正在运行excel或ACCESS程序时,客户端提交建立excel。application对象无法成功。另一种情况,当一个客户端提交请求时,并没有结束时,另一个客户端提交的请求也无法成功!或许这个问题有别的解决方案,但是至少这是不稳定的。 </p><p> 正好我们部门内部网上有相关成功的例子,挖出来看,原来是在客户端建立excel。application的。这样,服务器端不会再冲突。对于客户端,因为是在局域网内运行,客户IE安全级别可以设低,并允许运行相关activeX(就是在IE工具选项中把各类涉及安全的东西都设为允许,具体的哪一项没有去追究),即使没有设置,IE也会弹出警告窗口:“是否允许运行activeX?” </p>
<p> </p>
<p> 实现的代码和上篇日志差不多,简单的如下:</p><p><code> <script language="vbscript"><br /> set objExcelApp=CreateObject("Excel.Application")<br /> objExcelApp.DisplayAlerts=true<br /> objExcelApp.WorkBooks.Open("http://XXX.XXX.XXX/XXX.xls")<br /> '完整网路地址的xls文件,这个文件已经设置好格式及打印样式,保存在服务器上<br /> set objExcelBook=objExcelApp.ActiveWorkBook<br /> set objExcelSheets=objExcelBook.Worksheets<br /> set objExcelSheet=objExcelBook.Sheets(1)<br /> '====此处为对excel单元格进行填写数据的语句,如果是从数据库提取数据,可由后台程序生成这些语句,ASP中的示列:<br /> '如:response.write "objExcelSheet.Range(""B2"").Value="""&rs("XXX")&""""<br /> '或 objExcelSheet.Range("B2").Value="<%=rs("XXX")%>"<br /> objExcelSheet.Range("B2:k2").Value=Array("Week1","Week2","Week3","Week4","Week5","Week6","Week7")<br /> objExcelSheet.Range("B3:k3").Value=Array("67","87","5","9","7","45","45","54","54","10")<br /> objExcelSheet.Range("B4:k4").Value=Array("10","10","8","27","33","37","50","54","10","10")<br /> objExcelSheet.Range("B5:k5").Value=Array("23","3","86","64","60","18","5","1","36","80")<br /> objExcelSheet.Cells(3,1).Value="InternetExplorer"<br /> objExcelSheet.Cells(4,1).Value="Netscape"<br /> objExcelSheet.Cells(5,1).Value="Other"<br /> '==================<br /> 'objExcelApp.Quit<br /> 'set objExcelApp=Nothing<br /> </script></code><p> 上述代码中</p></p><p> 'objExcelApp.Quit</p><p> 'set objExcelApp=Nothing </p><p> 作为取消执行,因为此处不要关闭excel.applicaition,不然数据填完后,excel会关闭掉。这时在客户端打开的excel文件需要由客户进行修改或打印等操作。同时objexcelapp对象还有打印设置及进入打印预览界面等方法,可参考相关excel相关资料。</p></p>