vb.net - excel - windows任务问题

时间:2017-01-08 16:19:32

标签: excel vb.net taskscheduler

我构建了一个将数据保存到excel的.net应用程序。当我手动运行应用程序时,程序将数据表中的东西保存到excel中。问题是,当我计划在Windows 10 PC上运行任务时,我得到以下错误。

:System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:0x800A03EC    at Microsoft.Office.Interop.Excel.WorkbookClass.SaveAs(Object Filename,Object FileFormat,Object Password,Object WriteResPassword,Object ReadOnlyRecommended,Object CreateBackup,XlSaveAsAccessMode AccessMode,Object ConflictResolution,Object AddToMru,Object TextCodepage,Object TextVisualLayout,Object Local)< / p>

 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

1 个答案:

答案 0 :(得分:0)

将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的问题:

  

时,进程外COM服务器可能会出现此问题      
      
  1. 机器CPU负载较高,过程需要很长时间   在少于120的时间内启动并执行CoRegisterClassObjects()   秒。

  2.   
  3. COM服务器未注册正确的类ID。

  4.   
  5. COM服务器当前正在停止并且存在竞争   CoCreateInstance和COM服务器停止部分之间的条件。

  6.   
  7. COM服务器启动方式存在安全问题   (此页面似乎提示拼写错误的密码或缺少密码   “作为批处理作业登录”权限为“运行方式...”COM服务器,但是   无论如何,我建议你重新验证这些信息   具体配置)

  8.