Click here to Skip to main content
15,896,111 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 WriteInExcel

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

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
        If Not String.IsNullOrEmpty(txtFileName.Text) Then

            Dim ErrorMessage As String = String.Empty
            Dim OExcelHandler As New ExcelHandler()
            btnClose.Enabled = False
            Try
                Dim ds As DataSet = GetGridData()
                If ds IsNot Nothing Then
                    OExcelHandler.ExportToExcel(txtFileName.Text.Trim(), ds, "Write In Excel", ErrorMessage)
                End If
            Catch ex As Exception

            Finally
                btnClose.Enabled = True
                If Not String.IsNullOrEmpty(ErrorMessage) Then
                    MessageBox.Show(ErrorMessage)
                Else
                    MessageBox.Show("Operation Successful!")
                End If
            End Try

        End If
    End Sub

    Private Function GetGridData() As DataSet
        Dim ds As New DataSet
        ds.Tables.Add("Table0")
        ds.Tables(0).Columns.Add("Column1")
        ds.Tables(0).Columns.Add("Column2")
        ds.Tables(0).Columns.Add("Column3")
        ds.Tables(0).Columns.Add("Column4")
        ds.Tables(0).Columns.Add("Column5")

        For i As Integer = 0 To dgvExcelData.Rows.Count - 1
            Dim dtr As DataRow = ds.Tables(0).NewRow()
            dtr("Column1") = dgvExcelData.Item("Column1", i).Value
            dtr("Column2") = dgvExcelData.Item("Column2", i).Value
            dtr("Column3") = dgvExcelData.Item("Column3", i).Value
            dtr("Column4") = dgvExcelData.Item("Column4", i).Value
            dtr("Column5") = dgvExcelData.Item("Column5", i).Value
            ds.Tables(0).Rows.Add(dtr)
        Next
        Return ds
    End Function

    Private Sub WriteInExcel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    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