Click here to Skip to main content
12,698,292 members (23,163 online)
Click here to Skip to main content
Add your own
alternative version


168 bookmarked

Export to Excel using VB.Net

, 25 Apr 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Export data from VB.Net Dataset to Excel without using Datagrid/DataTable




Export the data's to Excel of Microsoft office is currently need in many concern. 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's from datagrid or by Datatable. I used Dataset which is easy to use. But for code optimization dataset is not advisable compare to Reader and DataTable

Using the code

In current available code for export the data to Excel are given. Which are so completed and not in 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()" which i used 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 in my company project which 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 Sambandam
Technical Lead
India India
No Biography provided

You may also be interested in...


Comments and Discussions

Questionthe point disappear in excel Pin
Omarcool Omar10-Jun-16 2:58
memberOmarcool Omar10-Jun-16 2:58 
AnswerRe: the point disappear in excel Pin
Senthil Sambandam19-Jun-16 20:31
professionalSenthil Sambandam19-Jun-16 20:31 
QuestionMacro Pin
cqncer14-Dec-15 9:05
membercqncer14-Dec-15 9:05 
GeneralMy vote of 5 Pin
Gun Gun Febrianza18-Dec-14 19:41
member Gun Gun Febrianza18-Dec-14 19:41 
QuestionExcel Pin
Member 1081187812-May-14 0:07
memberMember 1081187812-May-14 0:07 
QuestionThanks Pin
Kashif Alvi24-Aug-13 9:44
memberKashif Alvi24-Aug-13 9:44 
QuestionThanks Pin
nihadar12-Jun-13 23:57
membernihadar12-Jun-13 23:57 
QuestionExport multiple DataSets to multiple Excel sheets using Pin
Priyanka Jain4-May-13 0:40
memberPriyanka Jain4-May-13 0:40 
Questionhi Pin
Member 999349622-Apr-13 16:34
memberMember 999349622-Apr-13 16:34 
GeneralFree Export to Excel class, using OpenXML libraries Pin
Mike Gledhill1-Mar-13 8:54
memberMike Gledhill1-Mar-13 8:54 
GeneralRe: Free Export to Excel class, using OpenXML libraries Pin
Member 795657814-Mar-13 18:55
memberMember 795657814-Mar-13 18:55 
Generalgood Pin
pootuba31-Jan-13 5:00
memberpootuba31-Jan-13 5:00 
QuestionError "Microsoft.Office.Interop.Excel" Pin
morteza_msh15-Jan-13 11:15
membermorteza_msh15-Jan-13 11:15 
AnswerRe: Error "Microsoft.Office.Interop.Excel" Pin
Defilippo10-Dec-13 0:06
memberDefilippo10-Dec-13 0:06 
QuestionExcel Chart in Pin
Ofir G.20-Dec-12 3:03
memberOfir G.20-Dec-12 3:03 
GeneralThank you Your Coding Pin
vanchai hontong6-Dec-12 18:56
membervanchai hontong6-Dec-12 18:56 
QuestionThank you, very good example Pin
Member 868754429-Oct-12 22:26
memberMember 868754429-Oct-12 22:26 
GeneralMy vote of 5 Pin
D-Kishore4-Sep-12 20:25
memberD-Kishore4-Sep-12 20:25 
GeneralMy vote of 5 Pin
ALDER_MORIGGI13-Jul-12 3:38
memberALDER_MORIGGI13-Jul-12 3:38 
GeneralMy vote of 5 Pin
Himachandra6-Jul-12 0:06
memberHimachandra6-Jul-12 0:06 
QuestionExporting DataGridView contents in an excel file following designed column format in an excel template 97-2003 Pin
Member 898364524-May-12 23:32
memberMember 898364524-May-12 23:32 
AnswerRe: Exporting DataGridView contents in an excel file following designed column format in an excel template 97-2003 Pin
Kurfuerstendamm19-Nov-13 1:25
memberKurfuerstendamm19-Nov-13 1:25 
GeneralMy vote of 5 Pin
MacParekh21-May-12 22:20
memberMacParekh21-May-12 22:20 
GeneralMy vote of 5 Pin
Member 432084410-Feb-12 5:52
memberMember 432084410-Feb-12 5:52 
QuestionSeriously? Pin
ortund5-Jan-12 21:57
memberortund5-Jan-12 21:57 
AnswerRe: Seriously? Pin
Senthil Sambandam14-Nov-14 6:06
professionalSenthil Sambandam14-Nov-14 6:06 
SuggestionAlternative way to export to a (genuine) Excel 2007 file, using OpenXML Pin
MikeGledhill5-Dec-11 1:58
memberMikeGledhill5-Dec-11 1:58 
GeneralMy vote of 5 Pin
Member 815120624-Nov-11 3:55
memberMember 815120624-Nov-11 3:55 
QuestionKeep getting error "A first chance exception of type 'System.NotImplementedException' occurred" Pin
Member 833666720-Oct-11 12:22
memberMember 833666720-Oct-11 12:22 
AnswerRe: Keep getting error "A first chance exception of type 'System.NotImplementedException' occurred" Pin
Senthil Sambandam14-Nov-14 6:19
professionalSenthil Sambandam14-Nov-14 6:19 
QuestionThans Senthil S Pin
dongcr10-Aug-11 23:54
memberdongcr10-Aug-11 23:54 
GeneralMy vote of 5 Pin
dongcr10-Aug-11 23:39
memberdongcr10-Aug-11 23:39 
QuestionExport only visible columns Pin
Member 80141176-Jul-11 7:31
memberMember 80141176-Jul-11 7:31 
GeneralMy vote of 4 Pin
Member 80141176-Jul-11 7:29
memberMember 80141176-Jul-11 7:29 
GeneralMy vote of 5 Pin
deo cabral14-Jun-11 8:29
memberdeo cabral14-Jun-11 8:29 
GeneralRetrieve all the database table values to an excel Pin
Bhagya1112-Jun-11 23:56
memberBhagya1112-Jun-11 23:56 
Generalyet another very nice excel export library Pin
aron.sinoai22-Apr-11 11:00
memberaron.sinoai22-Apr-11 11:00 
Generali got error Pin
sanhadenn9-Mar-11 21:10
membersanhadenn9-Mar-11 21:10 
GeneralMy vote of 5 Pin
McGuire28-Feb-11 13:25
memberMcGuire28-Feb-11 13:25 
GeneralMessage Automatically Removed Pin
23-Feb-11 22:24
memberqgyn23-Feb-11 22:24 
Generalthanks Pin
ekomahendro22-Feb-11 23:05
memberekomahendro22-Feb-11 23:05 
GeneralMy vote of 5 Pin
vvsbabji18-Jan-11 5:28
membervvsbabji18-Jan-11 5:28 
Generalthanks Pin
jonesberyl17-Aug-10 23:15
memberjonesberyl17-Aug-10 23:15 
GeneralMy vote of 5 Pin
kumar_vk16-Aug-10 0:14
memberkumar_vk16-Aug-10 0:14 
GeneralMy vote of 5 Pin
sankar07Ind16-Aug-10 0:10
membersankar07Ind16-Aug-10 0:10 
GeneralDataset takes too much time Pin
lbs.allies1-Apr-10 0:32
memberlbs.allies1-Apr-10 0:32 
GeneralRe: Dataset takes too much time Pin
JamesHoward9726-Nov-11 23:50
memberJamesHoward9726-Nov-11 23:50 
QuestionHow to export to excel 3 related tables into excel using Pin
yustock16-Mar-10 22:32
memberyustock16-Mar-10 22:32 
GeneralError : inserted number value in to .xls file Pin
mohankundenagoud11-Mar-10 0:34
membermohankundenagoud11-Mar-10 0:34 
GeneralRe: Error : inserted number value in to .xls file Pin
Senthil S11-Mar-10 11:59
memberSenthil S11-Mar-10 11:59 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 26 Apr 2008
Article Copyright 2007 by Senthil Sambandam
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid