withSolutions   Script Sample Showcase
 
Skip Navigation Links.   This routine will allow a user to export the data from a datagrid/gridview to a local .csv file.

Public Class ExportToLocalExcelFile

  

              Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

  

                            Dim fName As String = ""

  

                            OpenFileDialog1.InitialDirectory = "c:\temp\"

  

                            OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"

  

                            OpenFileDialog1.FilterIndex = 2

  

                            OpenFileDialog1.RestoreDirectory = True

  

                            If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then

  

                                          fName = OpenFileDialog1.FileName

  

                            End If

  

                            Me.TextBox1.Text = fName

  

                            Dim TextLine As String = ""

  

                            Dim SplitLine() As String

  

  

  

                            If System.IO.File.Exists(fName) = True Then

  

                                          Dim objReader As New System.IO.StreamReader(fName)

  

                                          Do While objReader.Peek() <> -1

  

                                                        TextLine = objReader.ReadLine()

  

                                                        SplitLine = Split(TextLine, ",")

  

                                                        Me.DataGridView1.Rows.Add(SplitLine)

  

                                          Loop

  

                            Else

  

                                          MsgBox("File Does Not Exist")

  

                            End If

  

              End Sub

  

  

  

              Private Sub SaveGridDataInFile(ByRef fName As String)

  

                            Dim I As Integer = 0

  

                            Dim j As Integer = 0

  

                            Dim cellvalue$

  

                            Dim rowLine As String = ""

  

                            Try

  

                                          Dim objWriter As New System.IO.StreamWriter(fName, True)

  

                                          For j = 0 To (DataGridView1.Rows.Count - 2)

  

                                                        For I = 0 To (DataGridView1.Columns.Count - 1)

  

                                                                      If Not TypeOf DataGridView1.CurrentRow.Cells.Item(I).Value Is DBNull Then

  

                                                                                    cellvalue = DataGridView1.Item(I, j).Value

  

                                                                      Else

  

                                                                                    cellvalue = ""

  

                                                                      End If

  

                                                                      rowLine = rowLine + cellvalue + ","

  

                                                        Next

  

                                                        objWriter.WriteLine(rowLine)

  

                                                        rowLine = ""

  

                                          Next

  

                                          objWriter.Close()

  

                                          MsgBox("Text written to file")

  

                            Catch e As Exception

  

                                          MessageBox.Show("Error occured while writing to the file." + e.ToString())

  

                            Finally

  

                                          FileClose(1)

  

                            End Try

  

              End Sub

  

  

  

              Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

  

                            SaveGridDataInFile(Me.TextBox1.Text)

  

              End Sub

  

End Class