Click here to Skip to main content
15,897,273 members
Articles / Programming Languages / Visual Basic

Read and Write Excel Files

Rate me:
Please Sign up or sign in to vote.
3.33/5 (20 votes)
3 Jul 2009CPOL 216.3K   27.3K   63  
Read and Write Excel Files using VB.NET
Public Class ReadFromExcel

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        If ofdOpenFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtFileName.Text = ofdOpenFile.FileName
        End If
    End Sub

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        If Not String.IsNullOrEmpty(txtFileName.Text) Then
            Try
                btnClose.Enabled = False
                Dim OExcelHandler As New ExcelHandler()
                Dim ds As DataSet = OExcelHandler.GetDataFromExcel(txtFileName.Text.Trim())

                If ds IsNot Nothing Then
                    dgvExcelData.SelectionMode = DataGridViewSelectionMode.FullRowSelect
                    dgvExcelData.EditMode = DataGridViewEditMode.EditProgrammatically
                    dgvExcelData.DataSource = ds.Tables(0)
                End If
               
            Catch ex As Exception

            Finally
                btnClose.Enabled = True
            End Try
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    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) Accenture
India India
I am a Dot Net Developer working in MNC. I am interested to learn new technologies. I am always trying to improve my skills.

MCTS:ASP.NET 3.5
MCTS:WCF 3.5

Comments and Discussions