Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Program slow
Message
From
09/08/2004 11:12:02
 
 
To
All
General information
Forum:
ASP.NET
Category:
Other
Title:
Program slow
Miscellaneous
Thread ID:
00931780
Message ID:
00931780
Views:
58
Hello everyone, I need some help speeding up an application. Basically the program reads data from an excel spreadsheet and then writes it to a csv file. Although, I have a lot of the code, the main problem seems to occur inside the Do Loop of the first posted function 'CreateCsv'. I update a form (every 20 rows) with how many records have been processed. There is normally a 15 second or greater gap between the form updates. I just don't think it should take that long. Any help is appreciated. I apologize for how much code is posted. Thanks.
Private Function CreateCsv() As String
        ' Current Row and Final Row in the excel file. Start at row 13.
        Dim intRow As Integer
        Dim intFinalRow As Integer
        ' To determine EOF in the excel file, keep track of the empty cells between each 
        ' set of data. If the interval > 10, then no more date should exist in the file.
        Dim intInterval As Integer
        ' A valid year should be contained in row 7 column 4 of the excel file.
        Dim strYear As String
        ' Company code should always exist in column 1. When populated, write out the data
        ' for the row in a csv file
        Dim strCompanyCode As String
        ' For loop to write data for each month
        Dim intForMonth As Integer
        ' Capture Data to write to a csv file
        Dim strData As New StringBuilder
        ' Capture the Type of Sale
        Dim strSaleType As String
        ' To retrieve only the sale type from a cell, we must find the first occurance of ')'
        Dim intFindParen As Int16

        intRow = 13
        CreateCsv = ""
        strSaleType = ""

        Try
            ' Retrieve the year
            strYear = objActiveSheet.Cells(4, 7).Value
            If Len(strYear) > 4 Then
                strYear = Mid(strYear, 1, 4)
                If Not IsDate("01/01/" & strYear) Then
                    CreateCsv = "File - " & strPlanSalesFile & " /  Invalid Date (" & _
                                    "01/01/" & strYear & ")"
                    'MsgBox("Invalid Date for " & strPlanSalesFile, MsgBoxStyle.Information)
                    intRow = -1
                End If
            Else
                CreateCsv = "File - " & strPlanSalesFile & " /  Invalid Date (" & _
                                    "01/01/" & strYear & ")"
                'MsgBox("Invalid Date for " & strPlanSalesFile, MsgBoxStyle.Information)
                intRow = -1
            End If

            strSaleType = Trim(objActiveSheet.Cells(7, 7).Value)
            intFindParen = strSaleType.IndexOf(")")
            strSaleType = Mid(strSaleType, 1, intFindParen + 1)

            Do While intRow <> -1
                strData.Equals("")
                intRow = intRow + 1

                strCompanyCode = objActiveSheet.Cells(intRow, 1).Value
                If Len(strCompanyCode) > 0 Then
                    intInterval = 0

                    For intForMonth = 0 To 11
                        strData.Append(strCompanyCode & _
                                    "," & _
                                    objActiveSheet.Cells(intRow, 2).Value & _
                                    "," & _
                                    objActiveSheet.Cells(intRow, 3).Value & _
                                    "," & _
                                    objActiveSheet.Cells(intRow, 4).Value & _
                                    "," & _
                                    objActiveSheet.Cells(intRow, 5).Value & _
                                    "," & _
                                    objActiveSheet.Cells(intRow, 6).Value & _
                                    "," & _
                                    strSaleType & _
                                    "," & _
                                    objActiveSheet.Cells(intRow, 7).Value & _
                                    "," & strYear & "-" & _
                                         PadLeft((intForMonth + 1), 2, "0") & "-" & _
                                         "01" & _
                                    "," & _
                                    Format( _
                                    objActiveSheet.Cells(intRow, 8 + intForMonth).Value, _
                                    "###,##0") & _
                                    "," & _
                                    Format( _
                                    objActiveSheet.Cells(intRow, 26 + intForMonth).Value, _
                                    "##0.00") & _
                                    "," & _
                                    Format( _
                                    objActiveSheet.Cells(intRow, 44 + intForMonth).Value, _
                                    "#,###,##0.00") & IIf(intForMonth < 11, vbCrLf, ""))
                    Next

                    objResultFile.WriteData(strData.ToString)
                    CreateCsv = "SUCCESS"
                    intFinalRow = intRow

                Else
                    If objActiveSheet.Cells(intRow, 7).Value = "SUMMARY OF SALES" Then
                        intRow = intRow + 4
                        strSaleType = Trim(objActiveSheet.Cells(intRow, 7).Value)
                        intFindParen = strSaleType.IndexOf(")")
                        strSaleType = Mid(strSaleType, 1, intFindParen + 1)
                        intInterval = 1
                    Else
                        intInterval = intInterval + 1
                    End If

                End If

                If intInterval > 32 Then
                    intRow = -1
                End If

                If strCommand = "USER" And (intRow Mod 20) = 0 Then
                    formMain.lblRecord.Text = intFinalRow
                    formMain.lblRecord.Refresh()
                    formMain.Refresh()
                End If

            Loop

            If Len(CreateCsv) < 1 Then
                CreateCsv = "File - " & strPlanSalesFile & " /  NO DATA"
            End If

            If strCommand = "USER" Then
                formMain.lblRecord.Text = intFinalRow
                formMain.lblRecord.Refresh()
                formMain.Refresh()
            End If
        Catch ex As Exception
            CreateCsv = "ADDED DESCRIPTION: " & "File - " & strPlanSalesFile & _
                                            " /  Row (" & intRow & ")" & vbCrLf & _
                        "MESSAGE:           " & ex.Message & vbCrLf & _
                        "SOURCE:            " & ex.Source & vbCrLf & _
                        "ERL:               " & Err.Erl & vbCrLf & _
                        "STACKTRACE:        " & ex.StackTrace & vbCrLf & _
                        "HELPLINK:          " & ex.HelpLink
        End Try
    End Function



Imports System.IO

Public Class clsWriteToFile

    Private intHandle As Integer
    Private s As StreamWriter
    Public bolFileOpen As Boolean

    Sub New()
        bolFileOpen = False
    End Sub

    Public Sub OpenFile(ByVal strFile As String)
        Dim oFS As New FileInfo(strFile)

        If oFS.Exists Then
            oFS.Delete()
        End If
        oFS = Nothing

        ' Declaring a FileStream and creating a csv file with access mode of writing
        Dim fs As New FileStream(strFile, FileMode.Create, FileAccess.Write)
        ' Creating a new StreamWriter and passing the filestream object fs as argument
        s = New StreamWriter(fs)
        ' The seek method is used to move the cursor to next position to avoid text
        ' being overwritten
        's.BaseStream.Seek(0, SeekOrigin.End)

        bolFileOpen = True
    End Sub

    Public Sub WriteData(ByVal strData As String)
        s.WriteLine(strData)
        'Print(intHandle, strData)
    End Sub

    Public Sub CloseFile()
        If bolFileOpen = True Then
            s.Close()
            'FileClose(intHandle)
        End If
        bolFileOpen = False
    End Sub

End Class


Public Class clsExcel

    Public ExcelApp As Excel.Application
    Public bolExcelOpen As Boolean

    Sub New()
        bolExcelOpen = False
    End Sub

    Public Sub StartExcel()
        ' Create an Active X object
        ExcelApp = CreateObject("Excel.Application")
        bolExcelOpen = True
        'ExcelApp.Visible = True

        ' Prevent Excel from asking if you want to overwrite the file
        ' if it already exists
        ExcelApp.DisplayAlerts = False
        ' Instead of alphabet letters, change the column settings to reference by number
        ExcelApp.ReferenceStyle = Excel.XlReferenceStyle.xlR1C1
    End Sub

    Public Sub OpenExcelFile(ByVal strFile As String)
        ' Open the declared excel file
        ExcelApp.Workbooks.Open(strFile)

        ' Make sheet 1 the active sheet
        ExcelApp.Sheets(1).Select()
    End Sub

    Public Sub CloseExcel()
        If bolExcelOpen = True Then
            ' Close the worksheet
            ExcelApp.Workbooks.Close()

            ' Close Excel
            ExcelApp.Application.Quit()
            ExcelApp.Quit()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp)

            ExcelApp = Nothing
        End If
    End Sub

End Class
Randy Belcher
AFG Industries, Inc.
Next
Reply
Map
View

Click here to load this message in the networking platform