【问题标题】:vb.net - excel - windows task issuevb.net - excel - Windows 任务问题
【发布时间】:2017-01-08 16:19:31
【问题描述】:

我构建了一个 .net 应用程序,可以将数据保存到 Excel。当我手动运行应用程序时,程序会将数据表中的内容保存到 excel 中。问题是,当我安排任务在我的 Windows 10 PC 上运行时,我收到以下错误。

:System.Runtime.InteropServices.COMException (0x800A03EC):HRESULT 异常:0x800A03EC 在 Microsoft.Office.Interop.Excel.WorkbookClass.SaveAs(对象文件名、对象 FileFormat、对象密码、对象 WriteResPassword、对象 ReadOnlyRecommended、对象 CreateBackup、XlSaveAsAccessMode AccessMode、对象 ConflictResolution、对象 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 vb.net taskscheduler


    【解决方案1】:

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

    1. 机器CPU负载高,进程耗时长 启动并执行 CoRegisterClassObjects() 少于 120 秒。

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

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

    4. COM 服务器的启动方式存在安全问题 (此页面似乎提示密码拼写错误或缺少 “Run As..”COM 服务器的“Login as Batch Job”权限,但是 无论如何,我建议您重新验证此信息 具体配置)

    【讨论】:

    • 好的,所以我尝试了你的建议,这似乎解决了问题
    • 我更改了上面的代码,以纳入您对我如何使用构造函数和构建 excel 的回答。
    • @CodeMonger 继续考虑接受答案并提出新的问题,因为最初的问题/答案可能对将来的某人有所帮助。您的新问题可能与此question 重复。仔细检查您的文件路径,检查您的程序是否可以读取/写入路径等。
    猜你喜欢
    • 2021-05-08
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多