首页 > 解决方案 > VB.Net Excel 临时文件性能问题

问题描述

我有一个程序可以打开一些 excel 文件来检查值然后关闭它们。源文件位于网络上。一切都很好,我可以读取值,但随着时间的推移,我遇到了一些性能问题,打开它们并读取值需要超过 10 秒的时间。我看到它创建了很多没有数据的临时文件(0Ko)。当我删除所有临时文件时,时间回到不到一秒钟。

这是源代码:

Private Shared Sub Verification(listLots As List(Of String) )
Dim AppliExcel As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()

    For Each numLot As String In listLots
        Dim nomFichier As String = GetCheminFichier(numLot)
        

        If System.IO.File.Exists(nomFichier) Then
            Dim Classeur As Workbook = Nothing
            Dim Feuille As Worksheet
            
            'free all existing workbook
            For Each wb As Workbook In AppliExcel.Workbooks
                wb.Close(SaveChanges:=False)
                ReleaseExcelObject(wb)
            Next

            'Open excel file
            Classeur = AppliExcel.Workbooks.Open(nomFichier,, True)

            ......  
            
            Classeur.Close(False)
            ReleaseExcelObject(Classeur)
            Classeur = Nothing
        end if

    Next

    AppliExcel.Quit()
    ReleaseExcelObject(AppliExcel)
End Sub
Private Shared Sub ReleaseExcelObject(obj As Object)
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub

我想知道问题出在哪里以及如何解决。

希望有人有想法。亲切的问候NC。

标签: excelvb.nettemporary-files

解决方案


这是一个错误,而不是一个功能,所以没有一个正确的解决方案。

禁用所有防病毒软件。如果可行,请从 AV 中排除文件夹,从 AV 中排除文件类型。

如果这不起作用:

更改 excel 临时文件的位置。

尝试为客户端和服务器使用不同版本的 Windows。

禁用自动保存和自动恢复。

禁用远程差分压缩

还有一大堆可能导致此类问题的其他事情 - 同步设置、隐私(报告)设置、Windows Explorer 更新设置、缩略图更新设置......从 AV 开始,看看你在哪里。


推荐阅读