利用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文件的全部过程,希望对大家的学习有所帮助。