guys - most of the export to excel code i've found really doesn't wind up being excel. it's simply HTML w/ a .xls tag on it. and i've done this for years in asp.net, winforms in both c# & vb.net. but it's always a hack. can someone point me in the direction of how to export a datatable to the real excel? like what reference i need to use how i build the headers and the info (data). i'm just not finding anything.
thanks
rik

Recommended Answers

All 3 Replies

I don't think anyone has any idea what you are talking about. What is "real excel"? There are tons of examples on how to create excel files on the net. Present some code, or at least some pseudo code of what you are trying to do.

i actually found something but you must use the : Microsoft.Office.Interop assembly.
so, this is what i'm talking about. thanks

Public Sub ExportToExcel(ByVal dtTemp As DataTable, ByVal filepath As String)
        Dim strFileName As String = filepath
      Dim _excel As New Excel.Application
        Dim wBook As Excel.Workbook
        Dim wSheet As Excel.Worksheet
        wBook = _excel.Workbooks.Add()
        wSheet = wBook.ActiveSheet()
        Dim dt As System.Data.DataTable = dtTemp
        Dim dc As System.Data.DataColumn
        Dim dr As System.Data.DataRow
        Dim colIndex As Integer = 0
        Dim rowIndex As Integer = 0
        For Each dc In dt.Columns
            colIndex = colIndex + 1
            wSheet.Cells(1, colIndex) = dc.ColumnName
        Next
        For Each dr In dt.Rows
            rowIndex = rowIndex + 1
            colIndex = 0
            For Each dc In dt.Columns
                colIndex = colIndex + 1
                wSheet.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
            Next
        Next
        wSheet.Columns.AutoFit()
        wBook.SaveAs(strFileName)
        ReleaseObject(wSheet)
        wBook.Close(False)
        ReleaseObject(wBook)
        _excel.Quit()
        ReleaseObject(_excel)
        GC.Collect()
        MessageBox.Show("File Export Successfully!")
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.