title: Save datagridview to separate excel or PDF files based on content
body: <p>I currently have my datagridview importing data from a mysql table. Once the information is imported (usually 1000 rows by 15 columns), the user has the ability to export the data to excel. The user must then run a macro to separate the data into separate sheets by on the contents in column 1 (group the same rows together based on the value), then run another macro to save each individual sheet to it's own email file in a folder. The contents of column 1 can have the save values - for example</p>
  
  <p><img src="https://i.stack.imgur.com/r3n3A.jpg" alt="enter image description here"></p>
  
  <p>Is there any possibility of having vb.net automatically perform this function in the following steps once the user clicks a button:</p>
  
  <ol>
  <li>Sort the dgv by the contents in column 1,</li>
  <li>start saving the data based on the content in column 1 into separate files in a designated folder (all the jim's and bill's would be in a particular folder)</li>
  <li>the file name would be the name of the content in column 1.</li>
  <li>have a pop up once it's complete indicating there the items are saved.</li>
  </ol>
  
  <p>Current code I am using for dgv to excel export.</p>
  
  <pre><code>  'export to excel
      'verfying the datagridview having data or not
      If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
          Exit Sub
      End If
  
      'Creating dataset to export
      Dim dset As New DataSet
      'add table to dataset
      dset.Tables.Add()
      'add column to that table
      For i As Integer = 0 To DataGridView1.ColumnCount - 1
          dset.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText)
      Next
      'add rows to the table
      Dim dr1 As DataRow
      For i As Integer = 0 To DataGridView1.RowCount - 1
          dr1 = dset.Tables(0).NewRow
          For j As Integer = 0 To DataGridView1.Columns.Count - 1
              dr1(j) = DataGridView1.Rows(i).Cells(j).Value
          Next
          dset.Tables(0).Rows.Add(dr1)
      Next
  
      Dim excel As New Microsoft.Office.Interop.Excel.Application
      Dim wBook As Microsoft.Office.Interop.Excel.Workbook
      Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
  
      wBook = excel.Workbooks.Add()
      wSheet = wBook.ActiveSheet()
  
      Dim dt As System.Data.DataTable = dset.Tables(0)
      Dim dc As System.Data.DataColumn
      Dim dr As System.Data.DataRow
      Dim colIndex As Integer = 0
      Dim rowIndex As Integer = 0
  
      For Each dc In dt.Columns
          colIndex = colIndex + 1
          excel.Cells(1, colIndex) = dc.ColumnName
      Next
  
      For Each dr In dt.Rows
          rowIndex = rowIndex + 1
          colIndex = 0
          For Each dc In dt.Columns
              colIndex = colIndex + 1
              excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
  
          Next
      Next
  
      wSheet.Columns.AutoFit()
      Dim strFileName As String = "C:\exports\jim.xls"
      Dim blnFileOpen As Boolean = False
      Try
          Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
          fileTemp.Close()
      Catch ex As Exception
          blnFileOpen = False
      End Try
  
      If System.IO.File.Exists(strFileName) Then
          System.IO.File.Delete(strFileName)
      End If
  
      wBook.SaveAs(strFileName)
      excel.Workbooks.Open(strFileName)
      excel.Visible = True
  </code></pre>