Export DataTable object to Excel file using vb.net in VB.NET for Visual S... http://code.msdn.microsoft.
com/Export-DataTable-object-to-b3e2c1f0
Dev centers Learning resources Community Support
Microsoft Virtual Academy Forums Self support
Windows
Channel 9 Blogs Other support options
Windows Phone Interoperability Bridges Codeplex
MSDN Magazine
Office
Windows Azure Programs
BizSpark (for startups)
Visual Studio DreamSpark
Faculty Connection
More...
Microsoft Student
United States (English) Newsletter Privacy & cookies Terms of Use Trademarks © 2013 Microsoft
Browse sample requests
Download VB.NET (104.4 KB)
Ratings (0) Last updated 10/17/2012
Downloaded 1,348 times License Apache License, Version 2.0
Favorites Add to favorites Share
Requires Visual Studio 2010
Technologies VB.Net, Excel 2010, DataTable
Hirendra Sisodiya Topics Excel, VB.Net, COM Interop, DataTable, Data Export
PSPL Report abuse to Microsoft
www.authorcode.com
3,610 Points 0 2 13 Description Browse code Q and A
View samples View Profile
More from Hirendra
Sisodiya
Picture gallery in thumbnails view and
playing slide show in vb.net
(1)
Drop down Checked ListBox control in
vb.net
(0)
Multi page image viewer - user control in
vb.net
(0)
Get memory information of local system
using WMI in vb.net
(0)
1 of 3 16/08/2013 1:14 PM
Export DataTable object to Excel file using vb.net in VB.NET for Visual S... http://code.msdn.microsoft.com/Export-DataTable-object-to-b3e2c1f0
Introduction
The sample applciation demonstrates that how you can export the datatable object in c# to excel format.
How to use
1. Download the sample project.
2. Open with your visual studio.
3. Build the project and Run.
Description
Let's say we have a datatable object that contains the emoloyees' details ( employee id, name and age). And we need to
export this data to an excel file. For it firstly i create a datatable object of the employees' details at runtime. I am using
DataGrodVoew cxontrol to show this data with the help of its DataSource property.
The sample application allows you to chosse the your excel file name and the option for inserting the column names in to
excel file ot not.
We need to add the reference of the Microsoft.Office.Interop.excel library.
Code
Create the datatable and set datasource to datagridview.
The following code shows how to create the data table object at runtime.
Visual Basic
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles My
Base.Load
DataGridView1.DataSource = GetDatatable()
End Sub
Private Function GetDatatable() As DataTable
Dim dt As New DataTable
dt.Columns.Add("id")
dt.Columns.Add("Name")
dt.Rows.Add()
dt.Rows(0)(0) = "1"
dt.Rows(0)(1) = "David"
dt.Rows.Add()
dt.Rows(1)(0) = "2"
dt.Rows(1)(1) = "Ram"
dt.Rows.Add()
dt.Rows(2)(0) = "3"
dt.Rows(2)(1) = "John"
Return dt
End Function
Exmport to Excel
Visual Basic
Private Sub ExportToExcel(ByVal dtTemp As DataTable, ByVal filepath As String)
Dim strFileName As String = filepath
If System.IO.File.Exists(strFileName) Then
If (MessageBox.Show("Do you want to replace from the existing file?", "Export to
Excel", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) =
System.Windows.Forms.DialogResult.Yes) Then
System.IO.File.Delete(strFileName)
Else
Return
End If
End If
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
2 of 3 16/08/2013 1:14 PM
Export DataTable object to Excel file using vb.net in VB.NET for Visual S... http://code.msdn.microsoft.com/Export-DataTable-object-to-b3e2c1f0
3 of 3 16/08/2013 1:14 PM