Click here to Skip to main content
15,174,830 members
Articles / Desktop Programming / Windows Forms
Posted 1 Aug 2007


171 bookmarked

Export to Excel using VB.NET

Rate me:
Please Sign up or sign in to vote.
4.45/5 (73 votes)
25 Apr 2008CPOL1 min read
Export data from VB.NET Dataset to Excel without using Datagrid/DataTable



Exporting data to Excel of Microsoft Office is currently needed. But many of them used very complicated coding for simple export either from Datagrid or DataTable. I used simple Dataset to export the data.


Introducing the way of sending data from datagrid or by Datatable. I used Dataset which is easy to use. But for code optimization, dataset is not advisable compared to Reader and DataTable.

Using the Code

Currently, available code for exporting the data to Excel are given which is so complete and not straight forward because they used the Datagrid and DataTable which are given below:

From DataTable to Excel sheet:

Dim dt1 As New DataTable
Dim I1, J1 As Integer
For I1 = 0 To dsmas1.Tables(0).Columns.Count - 1
 For I1 = 0 To dsmas1.Tables(0).Rows.Count - 1
      Dim DR As DataRow = Nothing
      DR = dt1.NewRow
      For J1 = 0 To dsmas1.Tables(0).Columns.Count - 1
          DR.Item(J1) = dsmas1.Tables(0).Rows(I1).ItemArray(J1)
 Dim dt As New DataTable
 Dim I, J As Integer
 For I = 0 To dschd1.Tables(0).Columns.Count - 1
 For I = 0 To dschd1.Tables(0).Rows.Count - 1
      Dim DR As DataRow = Nothing
      DR = dt.NewRow
      For J = 0 To dschd1.Tables(0).Columns.Count - 1
          DR.Item(J) = dschd1.Tables(0).Rows(I).ItemArray(J)

For DataGrid to Excel sheet:

'verfying the datagridview having data or not
If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
    Exit Sub
End If

'Creating dataset to export
Dim dset As New DataSet
'add table to dataset
'add column to that table
For i As Integer = 0 To DataGridView1.ColumnCount - 1
'add rows to the table
Dim dr1 As DataRow
For i As Integer = 0 To DataGridView1.RowCount - 1
    dr1 = dset.Tables(0).NewRow
    For j As Integer = 0 To DataGridView1.Columns.Count - 1
        dr1(j) = DataGridView1.Rows(i).Cells(j).Value

Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()

Dim dt As System.Data.DataTable = dset.Tables(0)
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
    excel.Cells(1, colIndex) = dc.ColumnName

For Each dr In dt.Rows
    rowIndex = rowIndex + 1
    colIndex = 0
    For Each dc In dt.Columns
        colIndex = colIndex + 1
        excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)


Dim strFileName As String = "D:\ss.xls"
Dim blnFileOpen As Boolean = False
    Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
Catch ex As Exception
    blnFileOpen = False
End Try

If System.IO.File.Exists(strFileName) Then
End If

excel.Visible = True

But I used only Dataset to Excel sheet.

In code, I used a user defined function called "Load_Excel_Details()" in which I used an old technique like create Object for Excel, add the Book for a sheet and sheet for the data.


  • Excel <-- System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
    • Book <------ Excel Workbooks.Add()
      • Sheet <----- Excel SheetsInNewWorkbook = 1
        • Data <----- Excel cells(1, i).value

which is used in the code like given below:

 With Excel
       .SheetsInNewWorkbook = 1

      Dim i As Integer = 1
      For col = 0 To ComDset.Tables(0).Columns.Count - 1
           .cells(1, i).value = ComDset.Tables(0).Columns(col).ColumnName
           .cells(1, i).EntireRow.Font.Bold = True
           i += 1

            i = 2

       Dim k As Integer = 1
       For col = 0 To ComDset.Tables(0).Columns.Count - 1
          i = 2
          For row = 0 To ComDset.Tables(0).Rows.Count - 1
              .Cells(i, k).Value = ComDset.Tables(0).Rows(row).ItemArray(col)
               i += 1
               k += 1
          filename = "c:\File_Exported.xls"
 End With

Points of Interest

I used simple For loops.... and insert the values by Rows, Columns and Cells formats.


I proposed this after I used it in my company project which was appreciate by the customers.


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


About the Author

Senthil S
Software Developer
India India
This is Senthil.S. I am a Software Engineer at TCS. I am Currently Standing on .Net and Flex Platform.

Comments and Discussions

GeneralThank you Pin
Indiana_Ken24-Dec-08 16:04
MemberIndiana_Ken24-Dec-08 16:04 
GeneralRe: Thank you Pin
Senthil S17-Jan-09 10:04
MemberSenthil S17-Jan-09 10:04 
GeneralMy vote of 2 Pin
rajthapliyal15-Dec-08 22:33
Memberrajthapliyal15-Dec-08 22:33 
GeneralRe: My vote of 2 Pin
Senthil S17-Jan-09 10:02
MemberSenthil S17-Jan-09 10:02 
GeneralMy vote of 2 Pin
Will77015-Dec-08 20:31
MemberWill77015-Dec-08 20:31 
GeneralHi Pin
vks_gautam120-Nov-08 21:54
Membervks_gautam120-Nov-08 21:54 
QuestionFrom gridivie to Excel with some excel formating like colomn width and row height Pin
Member 427610218-Nov-08 4:27
MemberMember 427610218-Nov-08 4:27 
QuestionExcel Query Pin
nehamayank4-Nov-08 18:45
Membernehamayank4-Nov-08 18:45 
Generali m confused Pin
Blue stone21-Oct-08 4:04
MemberBlue stone21-Oct-08 4:04 
GeneralRe: i m confused Pin
Senthil S25-Oct-08 0:21
MemberSenthil S25-Oct-08 0:21 
GeneralFine but have doubt Pin
kasimmohamed21-Oct-08 1:55
Memberkasimmohamed21-Oct-08 1:55 
GeneralRe: Fine but have doubt Pin
Senthil S25-Oct-08 0:29
MemberSenthil S25-Oct-08 0:29 
GeneralFurther Help Sir Senthil S Pin
jacebeleren2-Oct-08 23:53
Memberjacebeleren2-Oct-08 23:53 
QuestionHello experts: Problems writing SQL code for Excell workBOOK please help me Pin
sivi0071-Oct-08 6:39
Membersivi0071-Oct-08 6:39 
Generalhelp Pin
priyajeni15-Sep-08 19:54
Memberpriyajeni15-Sep-08 19:54 
Generalpls help me out senthil Pin
shalu.mathews13-Sep-08 18:58
Membershalu.mathews13-Sep-08 18:58 
Question3 errors occured [modified] Pin
shalu.mathews13-Sep-08 7:03
Membershalu.mathews13-Sep-08 7:03 
GeneralCode not working at my end Pin
bkukki15-Aug-08 4:19
Memberbkukki15-Aug-08 4:19 
GeneralFinally..Thank You Senthil Pin
henry_yacob13-Aug-08 18:52
Memberhenry_yacob13-Aug-08 18:52 
GeneralExport Datagridview data to Excel Pin
giverofjoy9-Aug-08 2:22
Membergiverofjoy9-Aug-08 2:22 
GeneralRe: Export Datagridview data to Excel Pin
Senthil S14-Aug-08 15:28
MemberSenthil S14-Aug-08 15:28 
Generalhelp with error Pin
Norminator7-Aug-08 11:53
MemberNorminator7-Aug-08 11:53 
Generalerror Old format or invalid type library. Pin
cheehongjb31-Jul-08 18:58
Membercheehongjb31-Jul-08 18:58 
GeneralRe: error Old format or invalid type library. Pin
Senthil S14-Aug-08 15:24
MemberSenthil S14-Aug-08 15:24 
GeneralRe: error Old format or invalid type library. Pin
Member 382269826-Aug-08 2:32
MemberMember 382269826-Aug-08 2:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.