Search code examples
.netexcelvb.netoledb

How to optimize this simple but complex looking implementation in VB.Net?


Aim to achieve : Want to dump a fetched table to a excel sheet.

My implementation :

Imports System.Data.OleDb

    Private Sub getRawDataNextMonth()
    Dim sheetName As String = "RawData"
    Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\rawData.xlsx;Extended Properties=""Excel 12.0 XML;""")
    Dim adapter As New Data.OleDb.OleDbDataAdapter("SELECT * FROM [Sheet1$]", con)
    Dim dataSet As New Data.DataSet
    adapter.Fill(dataSet)
    Dim dataTable As Data.DataTable = dataSet.Tables(0)
    Dim rawData(dataTable.Rows.Count, dataTable.Columns.Count - 1) As Object
    Dim range As Excel.Range = WB.Sheets(sheetName).Range("A2:T" + dataTable.Rows.Count.ToString())
    For col = 0 To dataTable.Columns.Count - 1
        For row = 0 To dataTable.Rows.Count - 1
            rawData(row, col) = dataTable.Rows(row).ItemArray(col)
        Next
    Next
    range.Value2 = rawData
    End Sub

I am new to data fetching and ADO.Net concepts and just got it working. But..

This seems to be very inefficient and lame to me.

So, can you help reducing the complexity and may be improve performance ?

A totally different (Better) implementation is most welcome !

Please help me optimize this... with your experience !


Solution

  • This function writes out to a text file as CSV format and then opens the file in Excel and saves it as xlsx format. I tried this with a 53 column x 10,000 row table and it took ~2 seconds:

    Private Sub Export3(ByVal filename As String, ByRef dt As DataTable)
    
            Dim tempfile As String = Path.GetTempPath + Path.GetFileNameWithoutExtension(Path.GetTempFileName) + ".csv"
            Dim sb As StringBuilder
    
            Using sw As StreamWriter = New StreamWriter(tempfile)
                sb = New StringBuilder("")
                For c As Integer = 0 To dt.Columns.Count - 1
                    sb.Append(dt.Columns(c).ColumnName + ",")
                Next
                sw.WriteLine(sb.ToString.TrimEnd(","c))
    
                For r As Integer = 0 To dt.Rows.Count - 1
                    sb = New StringBuilder("")
                    For c As Integer = 0 To dt.Columns.Count - 1
                        sb.Append(dt.Rows(r).Item(c).ToString + ",")
                    Next
                    sw.WriteLine(sb.ToString.TrimEnd(","c))
                Next
            End Using
    
    
            Dim xlApp As Excel.Application = Nothing
            Dim xlBook As Excel.Workbook = Nothing
    
            Try
                xlApp = New Excel.Application
                'xlApp.Visible = True 'for debugging
                xlApp.DisplayAlerts = False
                xlBook = xlApp.Workbooks.Open(tempfile)
    
                xlBook.SaveAs(filename, Excel.XlFileFormat.xlWorkbookDefault)
    
                My.Computer.FileSystem.DeleteFile(tempfile)
    
            Finally
                If Not IsNothing(xlBook) Then
                    xlBook.Close()
                End If
                If Not IsNothing(xlApp) Then xlApp.Quit()
            End Try
        End Sub