VB.NET-INFORMATIONS.COM

<< Back To - VB.NET Excel 2007 Tutorials

Read Data from Excel using OLEDB in VB.NET 2005

Vb.Net Help      Crystal Reports      Excel


Without using Excel Object we can insert , edit , delete , select etc. in cell content of an Excel 2007 file using OLEDB in VB.NET 2005 . Here we are using OleDbConnection , OleDbDataAdapter , DataSet for doing these operations in an Excel file. You have to import System.Data in the project for doing these operations . For read the content from Excel file using ado.net , We can use the SELECT command like in SQL Operations.

sample Select sql

sql = "select * from [Sheet1$]"

Here is the sample Excel file .

vb.net_excel.JPG

Open the connection using OLEDB Provider

(provider=Microsoft.Jet.OLEDB.4.0;Data Source='Your Filename';Extended Properties=Excel 8.0;)

Specify which data you want to read

select * from [Sheet1$]

Here is the screen short after reading from Excel file .

vb.net_excel_oledb.JPG

VB.NET Source Code

Imports System.Data
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim MyConnection As System.Data.OleDb.OleDbConnection
            Dim DtSet As System.Data.DataSet
            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
            MyConnection = New System.Data.OleDb.OleDbConnection _
            ("provider=Microsoft.Jet.OLEDB.4.0;"  _
            " Data Source='c:\testfile.xls'; " _
             "Extended Properties=Excel 8.0;")
            MyCommand = New System.Data.OleDb.OleDbDataAdapter _
                ("select * from [Sheet1$]", MyConnection)
            MyCommand.TableMappings.Add("Table", "TestTable")
            DtSet = New System.Data.DataSet
            MyCommand.Fill(DtSet)
            DataGridView1.DataSource = DtSet.Tables(0)
            MyConnection.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
When you execute this program you will get the contents in the excel file to the DataGrid.
VB.NET 2005 Excel 2007 Related Contents
How to create an Excel 2007 file in VB.NET 2005
How to open or read an existing Excel 2007 file in VB.NET 2005
How to read entire worksheet in an Excel workbook
How to Format Excel 2007 Page in VB.NET
How to insert a Picture in Excel 2007 through programing on VB.NET 2005
How to insert a background Picture in Excel 2007 through VB.NET 2005
How to create a Chart in Excel 2007 in VB.NET
How to export a Chart in Excel 2007 as Picture file from VB.NET
How to Excel 2007 Chart in VB.NET Picture Box
How to Excel 2007 DataBar in VB.NET
How to Excel 2007 Data Validation Input Message
How to insert cell data in an Excel file using OLEDB
How to update cell data in an Excel file using OLEDB
How to export from database to excel
How to export from DataGridView to excel

Search here for more Vb.Net Source Code :



    Categories

    HOME
    VB.NET

 
 
 
   













  |  Home   |  SiteMap   |  About   |
net-informations.com (C) 2008 All Rights Reserved