Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im new to this vb codes my problem is i want to export selected rows in datagridview to excel anyone who can help me ? what codes i can use? thank you in advance!

What I have tried:

im trying this codes but it gives me all rows in datagridview but i need is the selected row

Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer

xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")



For i = 0 To DataGridView1.RowCount - 2
DataGridView1.Rows(i).Height = 20


For j = 0 To DataGridView1.ColumnCount - 1
For k As Integer = 1 To DataGridView1.Columns.Count
xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
xlWorkSheet.Cells.RowHeight = 20
xlWorkSheet.Cells.ColumnWidth = 20


xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
Next
Next
Next
Dim sd As New SaveFileDialog 'declare save file dialog
If sd.ShowDialog = System.Windows.Forms.DialogResult.OK Then 'check if save file dialog was close after selecting a path
'MsgBox(sd.FileName)
xlWorkSheet.SaveAs(sd.FileName & ".xlsx") 'sd.filename reurns save file dialog path
xlWorkBook.Close()
xlApp.Quit()
End If

releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)

MsgBox("You can find the file" & sd.FileName & ".xlsx")
Posted
Updated 14-Mar-18 20:00pm
v2
Comments
Bryian Tan 15-Mar-18 0:07am    
where is the logic that check for selected row?
Member 13727051 15-Mar-18 1:21am    
i want to see the selected rows into excel

1 solution

The code need something like

VB
Dim selectedRowCount As Integer = _
        DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)

 If selectedRowCount > 0 Then
    Dim i As Integer
    For i = 0 To selectedRowCount - 1
      ....
      ....

      xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, dataGridView1.SelectedRows(i).Index).Value.ToString()
    next i
 End If

[How to: Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridView Control]
 
Share this answer
 
Comments
Maciej Los 15-Mar-18 2:36am    
5ed!
Member 13727051 15-Mar-18 22:02pm    
thank you Bryian Tan ! It worked perfectly fine in my Sytem. only a little bit change I has was :


If selectedRowCount > 0 Then

For j = 0 To DataGridView1.ColumnCount - 1


For k As Integer = 1 To DataGridView1.Columns.Count
xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
xlWorkSheet.Cells.RowHeight = 20
xlWorkSheet.Cells.ColumnWidth = 20


For i = 0 To selectedRowCount - 1

xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, DataGridView1.SelectedRows(i).Index).Value.ToString()
Next i

For i = 0 To DataGridView1.RowCount - 1


Select Case DataGridView1.Rows(i).Height = 20

End Select
Next
Next
Next

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900