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