Click here to Skip to main content
14,545,210 members
Rate this:
Please Sign up or sign in to vote.
See more:
Dear Friends,
I want to export data from DataTable to Excel in windows based VB.NET Application.

Updated 25-May-20 6:56am
Naveen.Sanagasetti 27-Feb-13 22:54pm
Your Question is not clear
april88t 6-Mar-13 20:31pm
their is some examples found in this site itself just visit this link---- >
Rate this:
Please Sign up or sign in to vote.

Solution 1

Check this link this useful for you.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi,i try to search this code.. i'm not really sure its work this,but you try it.. thank you?

you could try with this Excel C# / VB.NET library. They send .NET 1.1 library on demand.

Here is a sample Excel C# code how to export DataSet to Excel:

var ef = new ExcelFile();

foreach (DataTable dataTable in dataSet.Tables)
ef.Worksheets.Add(dataTable.TableName).InsertDataTable(dataTable, 0, 0, true);

ef.SaveXlsx(dataSet.DataSetName + ".xlsx");
Rate this:
Please Sign up or sign in to vote.

Solution 5

Hi Promit,

Please use these codes to export your data table to excel.

If dgInvoice_SalesReport_Accounts.RowCount = Nothing Then
            MessageBox.Show("Sorry nothing to export into excel sheet.." & vbCrLf & "Please retrieve data in datagridview", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        Dim rowsTotal, colsTotal As Short
        Dim I, j, iC As Short
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
        Dim xlApp As New Excel.Application

            Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
            Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
            xlApp.Visible = True

            rowsTotal = dgInvoice_SalesReport_Accounts.RowCount
            colsTotal = dgInvoice_SalesReport_Accounts.Columns.Count - 1
            With excelWorksheet
                For iC = 0 To colsTotal
                    .Cells(1, iC + 1).Value = dgInvoice_SalesReport_Accounts.Columns(iC).HeaderText
                For I = 0 To rowsTotal - 1
                    For j = 0 To colsTotal
                        .Cells(I + 2, j + 1).value = dgInvoice_SalesReport_Accounts.Rows(I).Cells(j).Value
                    Next j
                Next I
                .Rows("1:1").Font.FontStyle = "Bold"
                .Rows("1:1").Font.Size = 14

                .Cells(1, 1).Select()
            End With
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            xlApp = Nothing
        End Try

on very to please add the reference

Imports Excel = Microsoft.Office.Interop.Excel

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100