eZine4i.com - Free Articles Directory From Search4i Network
   
   
 
 

ARTICLE CATEGORIES

SEARCH4i NETWORK

Follow Me on Twiter

ADVERTISEMENTS

 

TOP 5 AUTHORS

Alan Smith 533
Julia Bennet 370
Angelo Everton 369
Ashish Pandey 333
Kaye Z. Marks 291

Import Data from Excel File using VB .NET

addthis
     
Author:

Chetas

Category: HomearrowProgramming
Summary:

sample code how to query an Excel spreadsheet from an ASP.NET page using VB .NET. get the name of the first sheet in the Excel workbook.

Article:
Import Data from Excel File using VB .NET

The following is the sample code how to query an Excel spreadsheet from an ASP.NET page using VB .NET:

Dim strConn As String
Dim da As OleDbDataAdapter
Dim ds As New DataSet

strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\test.xls;Extended Properties=""Excel 8.0;"""
da = New OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn)
da.TableMappings.Add("Table", "Excel")
da.Fill(ds)

dataGrid1.DataSource = ds.Tables(0).DefaultView
dataGrid1.DataBind()
da.Dispose()

Note:

* The code above is just select data from Sheet1 (Worksheet) only.

If you want to select data from first sheet in the Excel workbook, you need to know the name of the first sheet first.
To get the name of the first sheet in the Excel workbook, refer code below:

Dim dao_dbE As dao.DBEngine
Dim dao_DB As DAO.Database
Dim strFirstSheetName As String

dao_dbE = New dao.DBEngine
dao_DB = dao_dbE.OpenDatabase("C:\test.xls", False, True, "Excel 8.0;")
strFirstSheetName = dao_DB.TableDefs(0).Name
da0_DB.Close()

Note:

* Microsoft DAO 3.5 Library needs to add to the project when source code above is used. From the Project menu, click References, click Add Reference… and then select the Microsoft DAO 3.5 Library to add.

So, complete source code should be like this:

Dim strConn As String
Dim da As OleDbDataAdapter
Dim ds As New DataSet
Dim dao_dbE As dao.DBEngine
Dim dao_DB As DAO.Database
Dim strFirstSheetName As String

dao_dbE = New dao.DBEngine
dao_DB = dao_dbE.OpenDatabase("C:\test.xls", False, True, "Excel 8.0;")
strFirstSheetName = dao_DB.TableDefs(0).Name
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\test.xls;Extended Properties=""Excel 8.0;"""

da = New OleDbDataAdapter("SELECT * FROM [" & _
strFirstSheetName & "]", strConn)
da.TableMappings.Add("Table", "Excel")
da.Fill(ds)

dataGrid1.DataSource = ds.Tables(0).DefaultView
dataGrid1.DataBind()

da.Dispose()
da0_DB.Close()
Source: Free Articles from ezine4i.com
About Author:

More About VB.NET and ASP.NET

Rate It:
     

TOP

RELATED ARTICLES

bullet Important Facts About iPhone Application Development
By:Shital Thakkar Category:Programming
bullet PHP Web Development Company and its Advantages - Rising in Demand of PHP Developers
By:Dave Sprint Category:Programming
bullet Open Source Development - Make your website user- friendly and SEO-friendly
By:Dave Sprint Category:Programming
bullet iOS Development: An Introduction to iPhone App Development
By:Dave Sprint Category:Programming
bullet PhpStorm: Smart & fast PHP IDE
By:Alexander Morozov Category:Programming
bullet Magento Development - Get perfect Ecommerce Solution with Magento
By:Dave Sprint Category:Programming
bullet Hire Joomla Developer - Customize your business needs with Joomla Development
By:Dave Sprint Category:Programming
bullet Google Calendar Component (Delphi Internet Components)
By:Sergey Shirokov Category:Programming
bullet Source code analysis for all your Databases, Crystal Reports, data and software program files
By:Ken Gnazdowsky Category:Programming
bullet android development,android developers
By:Godwin Josh Category:Programming