vb.net - excel - windows 任务问题
vb.net - excel - windows task issue
我构建了一个将数据保存到 excel 的 .net 应用程序。当我 运行 手动应用程序时,程序会将数据 table 中的内容保存到 excel 中。问题是,当我在 Windows 10 PC 上将任务安排到 运行 时,出现以下错误。
:System.Runtime.InteropServices.COMException (0x800A03EC):HRESULT 异常:0x800A03EC
在 Microsoft.Office.Interop.Excel.WorkbookClass.SaveAs(对象文件名、对象文件格式、对象密码、对象 WriteResPassword、对象 ReadOnlyRecommended、对象 CreateBackup、XlSaveAsAccessMode AccessMode、对象冲突解决、对象 AddToMru、对象 TextCodepage、对象 TextVisualLayout、对象本地)
Public Shared Function CreateExcel() As Microsoft.Office.Interop.Excel.ApplicationClass
For retry As Integer = 1 To 5
Try
clsScrape.SendMail(" Return New Microsoft.Office.Interop.Excel.ApplicationClass")
Return New Microsoft.Office.Interop.Excel.ApplicationClass
Exit For
Catch ex As Exception
If ex.HResult <> &H80080005 Then Throw ex
End Try
Next
Return Nothing
End Function
Public Shared Sub ExportExceltest(ByVal excel As Microsoft.Office.Interop.Excel.ApplicationClass)
Try
Dim dt As New DataTable
dt.Columns.Add("ID")
dt.Columns.Add("Name")
Dim R As DataRow = dt.NewRow
R("Name") = "MY Name"
dt.Rows.Add(R)
Dim strFile As String = "C:\Users\CodeMonger\Documents\Development\Test" & DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss") & ".xlsx"
' Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
clsScrape.SendMail("The datatable was built and this is Right before save")
wSheet.Columns.AutoFit()
wBook.SaveAs(strFile)
wBook.Close()
Catch ex As Exception
clsScrape.SendMail("Here is the Issue :" & ex.ToString)
End Try
End Sub
将 Excel 对象的构造代码移至工厂子例程,该子例程将在捕获到异常时执行重试,如下所示
Function CreateExcel() as Microsoft.Office.Interop.Excel.ApplicationClass
For retry As Integer = 1 To 5
Try
Return New Microsoft.Office.Interop.Excel.ApplicationClass
Exit For
Catch ex As Exception
If ex.HResult <> &H80080005 Then Throw ex
End Try
Next
Return Nothing
End Function
此 article 详细说明了 CO_E_SERVER_EXEC_FAILURE:
的问题
This problem can arise for an out of process COM server when
The machine has a high CPU load and the process takes a long time
to start and execute the CoRegisterClassObjects() in less than 120
seconds.
The COM server doesn’t register for the right class IDs.
The COM server is currently stopping and there is a race
condition between CoCreateInstance and the COM server stopping part.
There is a security problem in the way the COM server is started
(this page seems to suggest misspelled passwords or lacking the
“Login as Batch Job” privilege for “Run As..” COM servers, but
anyway I would suggest re-verifying this information for your
specific configuration)
我构建了一个将数据保存到 excel 的 .net 应用程序。当我 运行 手动应用程序时,程序会将数据 table 中的内容保存到 excel 中。问题是,当我在 Windows 10 PC 上将任务安排到 运行 时,出现以下错误。
:System.Runtime.InteropServices.COMException (0x800A03EC):HRESULT 异常:0x800A03EC 在 Microsoft.Office.Interop.Excel.WorkbookClass.SaveAs(对象文件名、对象文件格式、对象密码、对象 WriteResPassword、对象 ReadOnlyRecommended、对象 CreateBackup、XlSaveAsAccessMode AccessMode、对象冲突解决、对象 AddToMru、对象 TextCodepage、对象 TextVisualLayout、对象本地)
Public Shared Function CreateExcel() As Microsoft.Office.Interop.Excel.ApplicationClass
For retry As Integer = 1 To 5
Try
clsScrape.SendMail(" Return New Microsoft.Office.Interop.Excel.ApplicationClass")
Return New Microsoft.Office.Interop.Excel.ApplicationClass
Exit For
Catch ex As Exception
If ex.HResult <> &H80080005 Then Throw ex
End Try
Next
Return Nothing
End Function
Public Shared Sub ExportExceltest(ByVal excel As Microsoft.Office.Interop.Excel.ApplicationClass)
Try
Dim dt As New DataTable
dt.Columns.Add("ID")
dt.Columns.Add("Name")
Dim R As DataRow = dt.NewRow
R("Name") = "MY Name"
dt.Rows.Add(R)
Dim strFile As String = "C:\Users\CodeMonger\Documents\Development\Test" & DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss") & ".xlsx"
' Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
clsScrape.SendMail("The datatable was built and this is Right before save")
wSheet.Columns.AutoFit()
wBook.SaveAs(strFile)
wBook.Close()
Catch ex As Exception
clsScrape.SendMail("Here is the Issue :" & ex.ToString)
End Try
End Sub
将 Excel 对象的构造代码移至工厂子例程,该子例程将在捕获到异常时执行重试,如下所示
Function CreateExcel() as Microsoft.Office.Interop.Excel.ApplicationClass
For retry As Integer = 1 To 5
Try
Return New Microsoft.Office.Interop.Excel.ApplicationClass
Exit For
Catch ex As Exception
If ex.HResult <> &H80080005 Then Throw ex
End Try
Next
Return Nothing
End Function
此 article 详细说明了 CO_E_SERVER_EXEC_FAILURE:
的问题This problem can arise for an out of process COM server when
The machine has a high CPU load and the process takes a long time to start and execute the CoRegisterClassObjects() in less than 120 seconds.
The COM server doesn’t register for the right class IDs.
The COM server is currently stopping and there is a race condition between CoCreateInstance and the COM server stopping part.
There is a security problem in the way the COM server is started (this page seems to suggest misspelled passwords or lacking the “Login as Batch Job” privilege for “Run As..” COM servers, but anyway I would suggest re-verifying this information for your specific configuration)