稍作修正,之前寫的有一些錯誤,Excel在Server並不會關閉,而且只能在Server執行,Client並不會出現Excel應用程式視窗,視窗反而出現在Server端。
Server端Excel不會關閉(在[工作管理員]→[處理程序]內),可以參考此文章。
這次使用內建的excel.dll來製作excel功能,如下:
Dim xlApp As Application Dim xlBook As Workbook Dim xlSheet As Worksheet Dim xlRange As Range On Error Resume Next xlApp = GetObject(, "Excel.Application") If Err.Number() <> 0 Then Err.Clear() xlApp = CreateObject("Excel.Application") If Err.Number() <> 0 Then errMsg.Text = Err.Description Return End If End If xlBook = xlApp.Workbooks.Open(Server.MapPath("~\報表範本.xls")) xlApp.DisplayAlerts = False xlApp.Visible = True xlBook.Activate() '設為做用中的目標 xlBook.Parent.Windows(1).Visible = True xlSheet = xlBook.Worksheets(1) '選取第1個sheet xlSheet.Activate() '設此sheet為做用中的目標 '填入資料,有三種方法 xlSheet.Range("A1").Value = "Value" '把資料填到A1的格子內 xlSheet.Range("A1","A2").Value = "Value" '把資料填到A1~A2的格子內 xlSheet.Range(xlSheet.Cells(1,1), xlSheet.Cells(2,1)).Value = "Value" '把資料填到A1~A2的格子內,Cells(row,column),A=1,B=2,C=3,D=4... xlApp.Run("報表範本.xls" & "!Caculater") xlBook.SaveAs("要另存的檔名.xls") xlBook.Close() System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) '關閉Excel xlApp = Nothing xlBook = Nothing xlSheet = Nothing xlRange = Nothing