Excel 进程继续运行

发布于 2024-11-01 18:38:59 字数 2387 浏览 5 评论 0原文

我从按钮单击事件调用以下方法将数据表导出到 Excel。导出完成后,excel应用程序对象退出、释放并赋值为空。但实际上,除非整个应用程序关闭,否则它不会被释放并保持活动状态。因此,每次单击导出按钮时,都会有一个新的 Excel 应用程序对象继续运行。我该如何解决这个问题?请帮忙。问候。

如果不使用以下方法中的两行,则不会出现该问题。但我不能忽略它们,因为它们是真正需要的。检查带 * 标记的行。

''' <summary>
''' Exports data from a datatable to excel.
''' </summary>
Friend Shared Sub ExportToExcel(ByVal dtbl As DataTable)
    Dim exa As Excel.Application = Nothing
    Dim wkb As Excel.Workbook = Nothing
    Dim wks As Excel.Worksheet = Nothing
    Dim intColIndex, intRowIndex As Integer
    intColIndex = 0 : intRowIndex = 2

    Try
        exa = New Excel.Application
        exa.SheetsInNewWorkbook = 1
        wkb = exa.Workbooks.Add
        wks = wkb.ActiveSheet

        For intColIndex = 1 To dtbl.Columns.Count
            wks.Cells(1, intColIndex) = dtbl.Columns(intColIndex - 1).ColumnName
        Next

        For Each row As DataRow In dtbl.Rows
            For intColIndex = 1 To dtbl.Columns.Count
                wks.Cells(intRowIndex, intColIndex) = row(intColIndex - 1)
            Next

            intRowIndex += 1
        Next

        For intColIndex = 1 To dtbl.Columns.Count
            wks.Range(wks.Cells(1, intColIndex), wks.Cells(1, intColIndex)).Font.Bold = True
            wks.Range(wks.Cells(1, intColIndex), wks.Cells(1, intColIndex)).Font.Underline = True
        Next

    '***** The problem doesn't occur if the following two lines are not used *****
        wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns.WrapText = False
        wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns.AutoFit()
    '*****************************************************************************

        exa.Visible = True
        exa.UserControl = True

        If Not exa Is Nothing Then exa.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(wks)
        wks = Nothing
        System.Runtime.InteropServices.Marshal.ReleaseComObject(wkb)
        wkb = Nothing
        System.Runtime.InteropServices.Marshal.ReleaseComObject(exa)
        exa = Nothing
    Catch ex As Exception
        wks = Nothing
        wkb = Nothing
        exa = Nothing
        MsgBox("The following error has occurred:" & vbCrLf & ex.Message, MsgBoxStyle.Critical, "Error")
    Finally
        GC.Collect()
    End Try
End Sub

I'm calling the following method from a button click event to export a datatable to excel. After the export is completed, the excel application object is quit, released and assigned to nothing. But in reality it's not getting released and stays active unless the entire application is closed. So every time the button is clicked for export, a new excel application object keeps on running. How can I solve this? Please help. Regards.

The problem doesn't occur if two of the lines from the method below are not used. But I can't omit them as they are really needed. Check the * marked lines.

''' <summary>
''' Exports data from a datatable to excel.
''' </summary>
Friend Shared Sub ExportToExcel(ByVal dtbl As DataTable)
    Dim exa As Excel.Application = Nothing
    Dim wkb As Excel.Workbook = Nothing
    Dim wks As Excel.Worksheet = Nothing
    Dim intColIndex, intRowIndex As Integer
    intColIndex = 0 : intRowIndex = 2

    Try
        exa = New Excel.Application
        exa.SheetsInNewWorkbook = 1
        wkb = exa.Workbooks.Add
        wks = wkb.ActiveSheet

        For intColIndex = 1 To dtbl.Columns.Count
            wks.Cells(1, intColIndex) = dtbl.Columns(intColIndex - 1).ColumnName
        Next

        For Each row As DataRow In dtbl.Rows
            For intColIndex = 1 To dtbl.Columns.Count
                wks.Cells(intRowIndex, intColIndex) = row(intColIndex - 1)
            Next

            intRowIndex += 1
        Next

        For intColIndex = 1 To dtbl.Columns.Count
            wks.Range(wks.Cells(1, intColIndex), wks.Cells(1, intColIndex)).Font.Bold = True
            wks.Range(wks.Cells(1, intColIndex), wks.Cells(1, intColIndex)).Font.Underline = True
        Next

    '***** The problem doesn't occur if the following two lines are not used *****
        wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns.WrapText = False
        wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns.AutoFit()
    '*****************************************************************************

        exa.Visible = True
        exa.UserControl = True

        If Not exa Is Nothing Then exa.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(wks)
        wks = Nothing
        System.Runtime.InteropServices.Marshal.ReleaseComObject(wkb)
        wkb = Nothing
        System.Runtime.InteropServices.Marshal.ReleaseComObject(exa)
        exa = Nothing
    Catch ex As Exception
        wks = Nothing
        wkb = Nothing
        exa = Nothing
        MsgBox("The following error has occurred:" & vbCrLf & ex.Message, MsgBoxStyle.Critical, "Error")
    Finally
        GC.Collect()
    End Try
End Sub

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

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

发布评论

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

评论(2

唱一曲作罢 2024-11-08 18:38:59

这是您的解决方案。 切勿对 com 对象使用 2 个点。 您的 Range().Columns 创建的临时变量未被释放。

如何正确清理 Excel 互操作对象?

http://www.velocityreviews。 com/forums/showpost.php?s=f87f0674feda4442dcbd40019cbca65b&p=528575&postcount=2

Here is your solution. Never use 2 dots with com objects. Your Range().Columns creates temp variable which are not being released.

How do I properly clean up Excel interop objects?

http://www.velocityreviews.com/forums/showpost.php?s=f87f0674feda4442dcbd40019cbca65b&p=528575&postcount=2

像你 2024-11-08 18:38:59

避免使用互操作编写 Excel 文件,它通常充满此类问题。

首选方法是使用各种 excel api 之一来生成文件,例如 excelpackageNPOI,或 excellibrary。作为额外的好处,用户不必安装 Excel(他们可以使用开放式办公室等)

Avoid writing excel files using interop, it's usually riddled with these types of problems.

A preferred method would be to use one of the various excel api's to generate the files such as excelpackage, NPOI, or excellibrary. As an added bonus, the user doesn't have to have excel installed (they could use open office etc)

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