各位大神們,我想做一個Windows服務,可以定時匯出Excel檔案,但現在遇到了不能保存的瓶頸。
在我自己的筆記本電腦上加載服務、匯出Excel檔案一點問題都沒有,我的筆記本電腦的系統是Windows7 Pro,用的是VS2008(用vb語言)寫的代碼,安裝MS Office 2013中文版,但把這個服務的檔案放到別的的機子,有Windows 7 Pro、Windows Server 2008 R2、Windows Server 2012 R2,都報錯
我把Dcom里的Excel Application都按照我筆記本里的設定一模一樣的進行了設定,也在網上找了一些解決的方法,但現在代碼執行到保存作業表時報錯。
-----------------------------------------------------------------------------------------------------------------------------------------
無法啟動服務。System.Runtime.InteropServices.COMException (0x800A03EC): 例外來自 HRESULT:0x800A03EC
在 Microsoft.Office.Interop.Excel._Worksheet.SaveAs(String Filename, Object FileFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, Object AddToMru, Object TextCodepage, Object TextVisualLayout, Object Local)
在 ServiceTest.ServiceTest.ExcelTest() 位置 E:\軟體\ServiceTest\ServiceTest\ServiceTest.vb:行號 50
在 ServiceTest.ServiceTest.OnStart(String[] args) 位置 E:\軟體\ServiceTest\ServiceTest\ServiceTest.vb:行號 66
在 System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
-----------------------------------------------------------------------------------------------------------------------------------------
這樣我先上代碼,麻煩各位大神們幫我看一下,代碼是否有問題,
Option Explicit On
Imports Microsoft.Office.Interop
Public Class ServiceTest
Private Sub ExcelTest()
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Microsoft.Office.Interop.Excel.Application
xlApp.DisplayAlerts = False
xlApp.SheetsInNewWorkbook = 1
xlApp = CreateObject("Excel.Application") '備注1:如果不加入這句和下句代碼會
xlApp.DefaultSaveFormat = Excel.XlFileFormat.xlExcel8 '備注1:在xlWorkBook = xlApp.Workbooks.Add報錯
xlWorkBook = xlApp.Workbooks.Add '備注1:稍后我把報錯資訊附在后面。
xlWorkSheet = xlWorkBook.Sheets.Item(1)
With xlWorkSheet
.Activate()
.Name = "進廠物料總帳"
.Cells.Select()
.Cells.NumberFormatLocal = "@"
.Cells.VerticalAlignment = Excel.Constants.xlCenter
.Cells.HorizontalAlignment = Excel.Constants.xlCenter
.Cells.WrapText = True
.Cells.Font.Size = 11
.Cells.Font.Name = "宋體"
.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape
.PageSetup.LeftMargin = xlApp.InchesToPoints(0.31496062992126)
.PageSetup.RightMargin = xlApp.InchesToPoints(0.31496062992126)
.PageSetup.CenterHorizontally = True
.PageSetup.Zoom = 96
.PageSetup.PrintTitleRows = "$1:$5"
.Range("A2:N2").Merge()
.Range("A2").RowHeight = 26.25
.Range("A2").Font.Size = 16
.Range("A2").Font.Bold = True
.Cells(2, 1) = "進廠物料總帳"
.Range("A3:C3").Merge()
.Range("A3").RowHeight = 12.75
.Range("A3").HorizontalAlignment = Excel.Constants.xlLeft
.Cells(3, 1) = "年度:" & Year(DateTime.Now).ToString.Trim & "年"
.SaveAs("C:\1.xlsx") '備注2:如果程式正常執行至這句代碼后也會報錯,
' 也嘗試過在其后面對應的加入引數都是報一樣的錯錯誤
' 我也試過把這段代碼放到Windows表單里執行是沒有任何的錯誤的,直接就匯出資料,
' 也不需要在SaveAs后面加入引數,只需要給個保存的路徑和檔案名就可以了
'.SaveAs(Filename:="c:\ST\1.xls", FileFormat:=Excel.XlFileFormat.xlOpenXMLWorkbook, CreateBackup:=False)
End With
'xlWorkBook.SaveAs("C:\1.xlsx", Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, False, False, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
xlWorkBook = Nothing
xlApp.Application.Workbooks.Close()
xlApp.SheetsInNewWorkbook = 3
xlApp.Quit()
xlApp = Nothing
End Sub
Protected Overrides Sub OnStart(ByVal args() As String)
ExcelTest()
End Sub
End Class
【備注1】報錯資訊(未加xlApp = CreateObject("Excel.Application") 和xlApp.DefaultSaveFormat = Excel.XlFileFormat.xlExcel8這兩句代碼就在 xlWorkBook = xlApp.Workbooks.Add 處報錯):
-----------------------------------------------------------------------------------------------------------------------------------------
“System.Runtime.IneropServices.CoMException"型別的未經處理的例外在ServicesTest.exe 中發生。
其他資訊:記憶體或磁盤空間不足,Microsoft Excel無法再次打開或保存任何檔案。
-----------------------------------------------------------------------------------------------------------------------------------------
【備注2】報錯資訊(如果已加xlApp = CreateObject("Excel.Application") 和xlApp.DefaultSaveFormat = Excel.XlFileFormat.xlExcel8這兩句代碼后,在xlWorkSheet.SaveAs("C:\1.xlsx") 處報錯 )
-----------------------------------------------------------------------------------------------------------------------------------------
無法啟動服務。System.Runtime.InteropServices.COMException (0x800A03EC): 例外來自 HRESULT:0x800A03EC
在 Microsoft.Office.Interop.Excel._Worksheet.SaveAs(String Filename, Object FileFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, Object AddToMru, Object TextCodepage, Object TextVisualLayout, Object Local)
在 ServiceTest.ServiceTest.ExcelTest() 位置 E:\軟體\ServiceTest\ServiceTest\ServiceTest.vb:行號 50
在 ServiceTest.ServiceTest.OnStart(String[] args) 位置 E:\軟體\ServiceTest\ServiceTest\ServiceTest.vb:行號 66
在 System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
-----------------------------------------------------------------------------------------------------------------------------------------
在我筆記本電腦上直接啟動服務一切正常實作Excel檔案的匯出,但放到別的機子上就不可以,我也嘗試過在這些機子上安裝VS的環境,都不行,都是一啟動服務就報錯,該設定的地方(DCOM里的Excel Application、檔案夾的權限、服務的登陸身份試過用本地登陸和此帳戶(指定帳戶和本地服務)登陸都不行。
請各位大神們,幫我看看這是哪個環節出現問題,謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/275333.html
標籤:VB.NET
