Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / Windows Forms

Reading and writing an Excel file using VB.NET

Rate me:
Please Sign up or sign in to vote.
2.72/5 (19 votes)
20 Mar 2007CPOL 393.8K   5.8K   49  
This code helps the user to interact with an Excel file using the OleDBDataProvider in VB.NET.
Imports System.Data.OleDb
Imports System.Data

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(72, 32)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(112, 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Fetch/Update"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(280, 126)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region


    Private Const connstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
   "Data Source=C:\Test.xls;Extended Properties=""Excel 8.0;HDR=YES;"""
   
    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
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions