快速加载文本到excel

时间:2016-08-18 15:21:48

标签: vb.net excel optimization

我正在尝试将CS​​V或TSV加载到Excel中,对于小文件,它可以很好地工作;小文件是< 5KB。问题是,当我尝试将较大的文件加载到Excel时,该过程可能需要很长时间。我需要加载应用程序的文件可以包含5到100列的任何位置,其中包含5到20,000行。

我尝试过使用BackgroundWorker,Threadpools,Parallel.For,Parallel.ForEach,但它们似乎都具有相同的性能。

应用程序本身旨在从单独的文本文件中获取标题列表,然后将其加载到Excel中,应用格式设置,然后将实际的CSV / TSV文件加载到Excel中。

这是我到目前为止所做的,这个子被后台工作者开除了:

Private Sub LoadTextFile(ByVal xlApp As Excel.Application, ByVal xlWb As Excel.Workbook, ByVal xlWs As Excel.Worksheet, ByVal xlRange As Excel.Range)
    Dim SheetName As String = "Sheet1"
    If xlWs Is Nothing Then
        xlWs = DirectCast(xlWb.Sheets.Add(After:=xlWb.Sheets(xlWb.Sheets.Count), Count:=1, Type:=Excel.XlSheetType.xlWorksheet), Excel.Worksheet)
    End If

    'Read lines and store in a string array
    Dim lines() As String = File.ReadAllLines(FileToLoad)

    'Parse and write lines to Excel
    For i As Integer = 0 To lines.Length - 1
        'Set new row range
        xlRange = xlWs.Range(startCol + (i + 2).ToString + ":" + endCol + (i + 2).ToString)

        'Parse the line to load
        Dim lineDetail() As String =  lines(i).Split(fileDelimiter)

        'Load into Excel
        xlRange.Value = lineDetail
    Next
End Sub

以下是一些演出时间:

89列 - 2,000行:平均加载时间= 7秒

89列 - 4,000行:平均加载时间= 12秒

91列 - 10,000行:平均加载时间= 28秒

91列 - 24,000行:平均加载时间= 70秒

107Columns - 8,732行:平均加载时间= 17秒

我一直在想,“Excel如何立即加载这些文件?!?”无论如何,我将非常感谢任何可以帮助我优化这一点的人,因此将数据导入Excel并不需要这么长时间。提前谢谢。

1 个答案:

答案 0 :(得分:0)

这是我提出的,我认为它运作良好。感谢TnTinMn让我指向正确的方向:)

    Dim xlApp As New Excel.Application
    Dim xlWb As Excel.Workbook
    Dim xlWs As Excel.Worksheet
    Dim xlRange As Excel.Range

    'Start Excel and Create Application Object
    xlApp = CreateObject("Excel.Application")

    'Set invisible until all loading is completed
    xlApp.Visible = False

    'Get/Set references of active workbook/sheet
    xlWb = xlApp.Workbooks.Add
    xlWb = xlApp.ActiveWorkbook
    xlWs = xlWb.ActiveSheet
    xlRange = xlWs.Range("$A$1")

    'Used to specify the data type for each column.  2 = Text
    Dim array = New Object() {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, _
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 _
        , 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, _
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}

    'TextFilePlatform ANSI: 1252
    With xlWs.QueryTables.Add(Connection:="TEXT;" + cfg.filePath, Destination:=xlRange)
        .Name = "sec"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = Excel.XlCellInsertionMode.xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 1252
        .TextFileStartRow = 1
        .TextFileParseType = Excel.XlTextParsingType.xlDelimited
        .TextFileTextQualifier = Excel.XlTextQualifier.xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = cfg.fileTSV
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = cfg.fileCSV
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = array
        .TextFileTrailingMinusNumbers = True
        .Refresh(BackgroundQuery:=False)
    End With

    'Add headers do formatting here
    '<Additional Worksheet formats here>

    xlApp.Visible = True