Click here to Skip to main content
Licence CPOL
First Posted 20 Mar 2007
Views 189,192
Downloads 394
Bookmarked 47 times

Reading and writing an Excel file using VB.NET

By | 20 Mar 2007 | Article
This code helps the user to interact with an Excel file using the OleDBDataProvider in VB.NET.

Screenshot - ExcelSheet.jpg

Introduction

This articles helps user to Insert, Update, Delete, and Select data in Excel files using the OLEDBDataProvider in VB.NET.

Here is the connection string to connect with Excel using OleDBDataProvider:

Private Const connstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=C:\Test.xls;Extended Properties=""Excel 8.0;HDR=YES;"""

Here is the code on the button click event to select and insert data in an Excel file:

Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
    Dim pram As OleDbParameter
    Dim dr As DataRow
    Dim olecon As OleDbConnection
    Dim olecomm As OleDbCommand
    Dim olecomm1 As OleDbCommand
    Dim oleadpt As OleDbDataAdapter
    Dim ds As DataSet
    Try
        olecon = New OleDbConnection
        olecon.ConnectionString = connstring
        olecomm = New OleDbCommand
        olecomm.CommandText = _
           "Select FirstName, LastName, Age, Phone from [Sheet1$]"
        olecomm.Connection = olecon
        olecomm1 = New OleDbCommand
        olecomm1.CommandText = "Insert into [Sheet1$] " & _
            "(FirstName, LastName, Age, Phone) values " & _
            "(@FName, @LName, @Age, @Phone)"
        olecomm1.Connection = olecon
        pram = olecomm1.Parameters.Add("@FName", OleDbType.VarChar)
        pram.SourceColumn = "FirstName"
        pram = olecomm1.Parameters.Add("@LName", OleDbType.VarChar)
        pram.SourceColumn = "LastName"
        pram = olecomm1.Parameters.Add("@Age", OleDbType.VarChar)
        pram.SourceColumn = "Age"
        pram = olecomm1.Parameters.Add("@Phone", OleDbType.VarChar)
        pram.SourceColumn = "Phone"
        oleadpt = New OleDbDataAdapter(olecomm)
        ds = New DataSet
        olecon.Open()
        oleadpt.Fill(ds, "Sheet1")
        If IsNothing(ds) = False Then
            dr = ds.Tables(0).NewRow
            dr("FirstName") = "Raman"
            dr("LastName") = "Tayal"
            dr("Age") = 24
            dr("Phone") = 98989898
            ds.Tables(0).Rows.Add(dr)
            oleadpt = New OleDbDataAdapter
            oleadpt.InsertCommand = olecomm1
            Dim i As Integer = oleadpt.Update(ds, "Sheet1")
            MessageBox.Show(i & " row affected")
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        olecon.Close()
        olecon = Nothing
        olecomm = Nothing
        oleadpt = Nothing
        ds = Nothing
        dr = Nothing
        pram = Nothing
    End Try
End Sub

License

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

About the Author

Raman Tayal

Software Developer (Senior)

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks a Lot brother Pinmemberravisankarvattikuti5:47 14 Mar '10  
GeneralRe: Thanks a Lot brother PinmemberRaman Tayal6:54 15 Mar '10  
Generalthanks Pinmemberbhavin123018:13 14 Sep '09  
GeneralPlease clarify my question( urgent!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ) Pinmembercatchthedream1:43 31 Aug '09  
QuestionPK??? PinmemberGlobalUni22:10 22 Jul '09  
AnswerRe: PK??? PinmemberRaman Tayal7:18 23 Jul '09  
Questionmaking dictionary Pinmemberthaileang8:31 25 Jun '09  
QuestionExcel Upload PinmemberWendellB8:47 4 May '09  
AnswerRe: Excel Upload Pinmembergg423723:19 9 Nov '09  
GeneralUpdate a shared excell file [modified] Pinmemberfacu20yo3:57 19 Apr '09  
Questionneed help...urgent! PinmemberMember 430050818:11 16 Mar '09  
AnswerRe: need help...urgent! PinmemberRaman Tayal20:28 17 Mar '09  
GeneralRe: need help...urgent! PinmemberMember 430050819:13 20 Mar '09  
GeneralRe: need help...urgent! Pinmembergg423713:07 10 Aug '09  
Generalhelp Pinmemberpjacquez687:06 16 Feb '09  
GeneralRe: help PinmemberRaman Tayal20:50 16 Feb '09  
Generali need your help bro, Pinmemberpjacquez686:56 16 Feb '09  
Generaldelete Pinmemberpjacquez684:15 16 Feb '09  
GeneralFrom Jacquez Pinmemberpjacquez683:18 16 Feb '09  
GeneralGreat more code being posted THAT DOESN'T WORK OUT OF THE BOX. PinmemberMember 389560114:00 27 Jan '09  
GeneralRe: Great more code being posted THAT DOESN'T WORK OUT OF THE BOX. PinmemberRaman Tayal5:04 28 Jan '09  
GeneralRe: Great more code being posted THAT DOESN'T WORK OUT OF THE BOX. PinmemberMember 38956015:28 28 Jan '09  
GeneralRe: Great more code being posted THAT DOESN'T WORK OUT OF THE BOX. PinmemberRaman Tayal18:14 28 Jan '09  
GeneralRe: Great more code being posted THAT DOESN'T WORK OUT OF THE BOX. PinmemberAlejandro Marín6:42 20 Oct '09  
GeneralRe: Great more code being posted THAT DOESN'T WORK OUT OF THE BOX. PinmemberRaman Tayal7:47 21 Oct '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 20 Mar 2007
Article Copyright 2007 by Raman Tayal
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid