从 VB.Net 应用程序复制到 Excel 在某些环境中会导致 0x800A03EC 错误

发布于 2024-07-20 15:55:18 字数 1646 浏览 4 评论 0原文

这是错误:

System.Runtime.InteropServices.COMException,粘贴,来自 HRESULT 的异常:0x800A03EC

所有计算机都是 Windows XP,具有相同版本的 Excel (2003)。 有些机器会出现错误,有些则不会。 我们还没有找到模式。

这是代码:

Public Shared Sub ExportToExcel(ByVal dt As System.Data.DataTable)
        Dim app As New Microsoft.Office.Interop.Excel.Application
        app.Visible = False
        Dim Book = app.Workbooks.Add
        Try
            Dim Sheet = CType(Book.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet)
            With Sheet
                Dim c As Long = Asc("A")
                For Each dc As DataColumn In dt.Columns
                    .Range(Chr(CInt(c)) & "1").Value = dc.ColumnName.ToString
                    .Range(Chr(CInt(c)) & "1").Font.Bold = True
                    c += 1
                Next

                Sheet.Activate()
                Sheet.Range("A2:A2").Select()
                Sheet.Paste()
                .Columns.AutoFit()
                CType(app.ActiveWorkbook.Sheets(2), Microsoft.Office.Interop.Excel.Worksheet).Delete() 
                CType(app.ActiveWorkbook.Sheets(2), Microsoft.Office.Interop.Excel.Worksheet).Delete()
            End With
            app.Visible = True
            app.UserControl = True
        Catch ex As Exception
            ExceptionManager.HandledException(ex, "Unable to Copy to Excel.", "You are SOL", , False)
        Finally
            Book = Nothing
            System.Runtime.InteropServices.Marshal.ReleaseComObject(app)
            app = Nothing
            GC.Collect()
            GC.WaitForPendingFinalizers()
        End Try

    End Sub

Here is the error:

System.Runtime.InteropServices.COMException, Paste, Exception from HRESULT: 0x800A03EC

All machines are Windows XP with the same version of Excel (2003). Some machines get the error, some don't. We have yet to find a pattern.

Here is the code:

Public Shared Sub ExportToExcel(ByVal dt As System.Data.DataTable)
        Dim app As New Microsoft.Office.Interop.Excel.Application
        app.Visible = False
        Dim Book = app.Workbooks.Add
        Try
            Dim Sheet = CType(Book.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet)
            With Sheet
                Dim c As Long = Asc("A")
                For Each dc As DataColumn In dt.Columns
                    .Range(Chr(CInt(c)) & "1").Value = dc.ColumnName.ToString
                    .Range(Chr(CInt(c)) & "1").Font.Bold = True
                    c += 1
                Next

                Sheet.Activate()
                Sheet.Range("A2:A2").Select()
                Sheet.Paste()
                .Columns.AutoFit()
                CType(app.ActiveWorkbook.Sheets(2), Microsoft.Office.Interop.Excel.Worksheet).Delete() 
                CType(app.ActiveWorkbook.Sheets(2), Microsoft.Office.Interop.Excel.Worksheet).Delete()
            End With
            app.Visible = True
            app.UserControl = True
        Catch ex As Exception
            ExceptionManager.HandledException(ex, "Unable to Copy to Excel.", "You are SOL", , False)
        Finally
            Book = Nothing
            System.Runtime.InteropServices.Marshal.ReleaseComObject(app)
            app = Nothing
            GC.Collect()
            GC.WaitForPendingFinalizers()
        End Try

    End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里寻她 2024-07-27 15:55:18

COM 是引用计数的。 系统会在每次使用东西时保留一个计数器,因此您应该在退出之前清理这些引用,如下所示:

Marshal.ReleaseComObject(myVaraible);

您可能不需要 WaitForPendingFinalizers,甚至 GC.Collect 也是一个不应该需要的昂贵调用如果您使用上面的代码释放每个 com 对象来降低引用计数。

另外,我不确定您是否使用主互操作程序集,但您需要确保针对您的目标 Excel 版本使用正确的版本,否则您可能会遇到类似您遇到的错误。

COM is reference counted. The system keeps a counter of each time your using things, so you should clean up those references before you exit like so:

Marshal.ReleaseComObject(myVaraible);

You probably don't need the WaitForPendingFinalizers, and even the GC.Collect is an expensive call that shouldn't be needed if you release each com object using the code above to lower the reference count.

Also I'm not sure if your using the Primary Interop Assemblies but you need to ensure your using the correct version of them for the version of Excel your targeting or you can get errors such as the one your experiencing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文