利用ASP输出excel文件实例讲解(2)


程序代码:

<% 

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>
      

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2240.html