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文件需要由客戶進行修改或打印等操作。同時objexcelapp對象還有打印設置及進入打印預覽界面等方法,可參考相關excel相關資料。