Kod:
Private Sub Excel2Grid(ByVal sFile As String)
xlApp = New Excel.Application()
xlWorkBook = xlApp.Workbooks.Open(sFile)
xlWorkSheet = xlWorkBook.Worksheets("Sayfa1")
DataGridView1.Rows.Clear()
DataGridView1.Columns.Clear()
Dim iRow, iCol As Integer
For iCol = 1 To xlWorkSheet.Columns.Count
If xlWorkSheet.Cells(1, iCol).value Is Nothing Then
Exit For
Else
Dim col As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn()
col.HeaderText = xlWorkSheet.Cells(1, iCol).value
Dim colIndex As Integer = DataGridView1.Columns.Add(col)
End If
Next
For iRow = 2 To xlWorkSheet.Rows.Count - 1
If xlWorkSheet.Cells(iRow, 1).value Is Nothing Then
Exit For
Else
Dim row As String() = New String() {xlWorkSheet.Cells(iRow, 1).value, xlWorkSheet.Cells(iRow, 2).value.ToString(), xlWorkSheet.Cells(iRow, 3).value}
DataGridView1.Rows.Add(row)
End If
Next
xlWorkBook.Close()
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet)
End Sub
Excelden verileri datagride aktarıyorum fakat ben tüm satırların aktarılmasını istiyorum.
Aktarılacak satırların yazılmasını istemiyorum
Koddaki aşağıdaki yeri nasıl değiştirmeliyim
For iRow = 2 To xlWorkSheet.Rows.Count - 1
If xlWorkSheet.Cells(iRow, 1).value Is Nothing Then
Exit For
Else
Dim row As String() = New String() {xlWorkSheet.Cells(iRow, 1).value, xlWorkSheet.Cells(iRow, 2).value.ToString(), xlWorkSheet.Cells(iRow, 3).value}
DataGridView1.Rows.Add(row)
End If
Next