Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27329

[RESOLVED] VS 2012 Express: Inserting New Records in Excel Slows to a Crawl

$
0
0
I have a project which reads data from a bunch of text files and then inserts some of the data into an Excel file. The file will be appended to as each new run happens. The first thing I needed to do was to find the last row and that seemed easy enough. The first run of the project it inserted the data into the Excel file very quickly. But now as I watch the second run its slowed down to a crawl. What took only seconds a moment ago on the first run is now taking several minutes to complete. There are 976 records being inserted. The first run took only seconds, but this second run, which for testing is using the same 976 records is going to take in excess of 10 minutes. Is it normal procedure for subsequent runs like this to take so much longer or am I doing something wrong?

Here is the code I'm using:

Code:

Imports System
Imports System.IO
Imports Microsoft.Office
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices

Module Module1

    Sub Main()

        '************************************************************************************************************
        ' Open Excel and Populate Cells with data from text files
        '************************************************************************************************************
        Dim lRow As Long = 0
        Dim HatchType As String
        Dim vExcelApp As Excel.Application = Nothing
        Dim vExcelWorkbooks As Excel.Workbooks = Nothing
        Dim vExcelWorkbook As Excel.Workbook = Nothing
        Dim vExcelSheet1 As Excel.Worksheet = Nothing
        Dim vExcelSheet2 As Excel.Worksheet = Nothing
        Dim vExcelRange1 As Excel.Range = Nothing
        Dim NovellPath As String = "\\my_path\"
        Dim ExcelBOM As String = NovellPath & "My_Log.xls"

        vExcelApp = New Excel.Application
        vExcelApp.DisplayAlerts = False
        vExcelApp.Visible = False

        vExcelWorkbooks = vExcelApp.Workbooks
        vExcelWorkbook = vExcelApp.Workbooks.Open(ExcelBOM)
        vExcelSheet1 = CType(vExcelWorkbook.Sheets("Sheet1"), Excel.Worksheet)

        ' Find the last row of data on Sheet1 of the Excel File
        With vExcelSheet1
            lRow = .Range("A" & .Rows.Count).End(Excel.XlDirection.xlUp).Row
        End With

        Dim vFolder As String = "H:\in\"
        Dim vCount As Integer = 1

        Dim sortedFiles = New DirectoryInfo(vFolder).GetFiles().OrderBy(Function(x) x.LastWriteTime()).ToList()
        For Each f As FileInfo In sortedFiles

            Dim TextArray As String() = File.ReadAllLines(vFolder & f.Name)
            If TextArray(5) <> "None" Then

                If TextArray(5) = "APD" And TextArray(7) <> "1" And TextArray(6) <> "1" Then HatchType = "APD"
                If TextArray(5) = "APD" And TextArray(7) <> "1" And TextArray(6) = "1" Then HatchType = "APDR"
                If TextArray(5) = "APD" And TextArray(7) = "1" And TextArray(6) <> "1" Then HatchType = "APDSS"
                If TextArray(5) = "APD" And TextArray(7) = "1" And TextArray(6) = "1" Then HatchType = "APDSSR"

                If TextArray(5) = "APS" And TextArray(6) <> "1" Then HatchType = "APS"
                If TextArray(5) = "APS" And TextArray(6) = "1" Then HatchType = "APSR"

                If TextArray(5) = "AHD" And TextArray(7) <> "1" And TextArray(6) <> "1" Then HatchType = "AHD"
                If TextArray(5) = "AHD" And TextArray(7) <> "1" And TextArray(6) = "1" Then HatchType = "AHDR"
                If TextArray(5) = "AHD" And TextArray(7) = "1" And TextArray(6) <> "1" Then HatchType = "AHDSS"
                If TextArray(5) = "AHD" And TextArray(7) = "1" And TextArray(6) = "1" Then HatchType = "AHDSSR"

                If TextArray(5) = "AHS" And TextArray(6) <> "1" Then HatchType = "AHS"
                If TextArray(5) = "AHS" And TextArray(6) = "1" Then HatchType = "AHSR"

                If TextArray(5) = "TPD" And TextArray(7) <> "1" And TextArray(6) <> "1" Then HatchType = "TPD"
                If TextArray(5) = "TPD" And TextArray(7) <> "1" And TextArray(6) = "1" Then HatchType = "TPDSS"

                If TextArray(5) = "TPS" Then HatchType = "TPS"

                If TextArray(5) = "THD" And TextArray(7) <> "1" And TextArray(6) <> "1" Then HatchType = "TPD"
                If TextArray(5) = "THD" And TextArray(7) <> "1" And TextArray(6) = "1" Then HatchType = "THDSS"

                If TextArray(5) = "THS" Then HatchType = "THS"

            End If

            vExcelRange1 = vExcelSheet1.Range("A" & lRow + vCount)
            vExcelRange1.Value = f.LastWriteTime
            vExcelRange1 = vExcelSheet1.Range("B" & lRow + vCount)
            vExcelRange1.Value = Left(Mid(f.Name, 1, Len(f.Name) - 4), InStr(f.Name, "-") - 1)
            vExcelRange1 = vExcelSheet1.Range("C" & lRow + vCount)
            vExcelRange1.Value = HatchType
            vExcelRange1 = vExcelSheet1.Range("D" & lRow + vCount)
            vExcelRange1.Value = TextArray(1)
            vExcelRange1 = vExcelSheet1.Range("E" & lRow + vCount)
            vExcelRange1.Value = TextArray(2)

            Console.WriteLine("Adding Record #" & vCount)

            vCount += 1
        Next

        '*********************************************************************************************************
        ' NOW SHUT DOWN EXCEL
        '*********************************************************************************************************
        vExcelApp.DisplayAlerts = False
        vExcelWorkbook.Close(True)
        vExcelApp.UserControl = True
        vExcelApp.Quit()

        Marshal.FinalReleaseComObject(vExcelRange1)
        Marshal.FinalReleaseComObject(vExcelSheet1)
        Marshal.FinalReleaseComObject(vExcelWorkbook)
        Marshal.FinalReleaseComObject(vExcelWorkbooks)
        Marshal.FinalReleaseComObject(vExcelApp)

        vExcelRange1 = Nothing
        vExcelApp = Nothing
        vExcelWorkbooks = Nothing
        vExcelWorkbook = Nothing
        vExcelSheet1 = Nothing
        vExcelSheet2 = Nothing

        GC.Collect()
        GC.WaitForPendingFinalizers()
        GC.Collect()
        GC.WaitForPendingFinalizers()

    End Sub

End Module

I just emptied the spread sheet out and started with a fresh copy and it's slow going there to. BTW Sine the first run I did add some new fields in the same sheet1. I'm wondering if I should turn off automatic calculations and only recalc it all once after it's over?

Viewing all articles
Browse latest Browse all 27329

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>