0

The code below doing the job but it takes almost 60 seconds to complete execution. there are about 2000 rows and 55 columns. I guess a nested loop is not the most efficient and professional way, I searched in Google but haven't found a better way...(I thought maybe there is a way to use SQL?). I will be thankful for suggestions of making this task more efficient. (its written in vb.net but c# is fine also for examples and suggestions)

   Public Sub LoadDataTableToExcell(ByVal d As DataTable, ByVal path As String, ByVal fileName As String, ByVal newFile As Boolean, ByVal sheetName As String)

   If CheckIfDirExist(path) = False Then
            MsgBox("the Path" & " " & path & " " & "Does not exist")
            Exit Sub
        End If
        CreateAnExcelFile(path, sheetName)

        Dim xlapp As New Excel.Application
        Dim ws As Excel.Worksheet
        Dim xlworkbook As Excel.Workbook

        xlworkbook = xlapp.Workbooks.Open(path)
        ws = xlworkbook.Sheets(sheetName)
        Dim Erows As Integer = Nothing
        Dim Ecolumns As Integer = Nothing

        For Ecolumns = 0 To d.Columns.Count - 1
            ws.Cells(1, Ecolumns + 1) = d.Columns(Ecolumns).ColumnName
            For Erows = 0 To d.Rows.Count - 1
                ws.Cells(Erows + 3, Ecolumns + 1) = d.Rows(Erows).Item(Ecolumns)
            Next
        Next


        Dim CarbonNewRng As Excel.Range

        CarbonNewRng = ws.Range(ws.Cells(1, 1), ws.Cells(d.Rows.Count + 2, d.Columns.Count))
        CarbonNewRng.Borders.Weight = 4
        CarbonNewRng.Borders.Color = Color.FromArgb(0, 64, 64)

        ws.Columns("A:AZ").autofit()

        xlworkbook.Save()
        xlworkbook.Close()
        xlapp.Quit()



        releaseObject(xlapp)
        releaseObject(ws)
        releaseObject(xlworkbook)


        xlworkbook = Nothing
        xlapp = Nothing
        ws = Nothing
    Else

    End If
 end sub
pnuts
  • 58,317
  • 11
  • 87
  • 139
  • 1
    Any time you need to open up the Excel application objects, it's going to take some time. If you just cared about the data, you could use an OleDBCommand, or simply write out a csv file. If you need to do formatting, check out ClosedXML https://closedxml.codeplex.com/ – dbugger Oct 03 '15 at 12:47
  • is it possible to add an example how to export dataTable to excel with OleDBCommand ? –  Oct 03 '15 at 13:19
  • See http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp?rq=1 (about 1/2 way down the answers) – dbugger Oct 03 '15 at 13:39

1 Answers1

0

It's strange but the fastest way is to the dumpiest one :

  1. Select all in datatable
  2. Copy
  3. Create xls file
  4. Paste

Seriously I tried lots of them, but with standard Win controls set it's the fastest one.

upd syntax for this you may easily find over the net, let me no to copy paste it here

Also you may want to check this http://closedxml.codeplex.com/

Syntax is quite easy then:

XLWorkbook wb = new XLWorkbook();
DataTable dt = GetDataTableOrWhatever();
wb.Worksheets.Add(dt,"WorksheetName");
kb9
  • 349
  • 2
  • 7
  • never did it...what is the syntax dataTable.Copy() worksheet.Paste("a1") ? –  Oct 03 '15 at 13:11
  • ive downloaded the DLL and made a reference in my project but i keep getting this error: Could not load file or assembly 'DocumentFormat.OpenXml, Version=2.5.5631.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. –  Oct 03 '15 at 18:45
  • Select 'References' right click - Manage NuGet Packages - search for 'closedxml' - select it and click install – kb9 Oct 03 '15 at 19:12
  • i cant see this option just in visual studio 2015, my projects IDE is visual studio 2010, cant find this option... –  Oct 03 '15 at 19:48
  • Don't know how to explain how to reference dll in the project - keep trying, what else can I say, solution will definitely work for you – kb9 Oct 03 '15 at 20:20
  • after doing a lot of searching in the net your solution seems to be the most efficient, i havent tried it yet (as i am having a technical issues), i marked your the soulution as answer. by the way it is very strange that microsoft didnt made any solution for this specific need , and the best solution is from external libariy... –  Oct 03 '15 at 20:26
  • @Jonathan ty, you r going to meet it very often – kb9 Oct 03 '15 at 20:35