Skip to main content

AccessTr.neT


Excelden Datagride Veri Aktarımı

Excelden Datagride Veri Aktarımı

Çözüldü #1
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
drummers, 11-05-2009 tarihinden beri AccessTr.neT üyesidir.
Cevapla
#2
(28/06/2021, 09:45)drummers yazdı: Rows.Count - 1
Yerine alttaki gibi yazılabilr.A sütununun son dolu satırını bulur.
cells(rows.count,"A").end(3).row
Cevapla
#3
(28/06/2021, 16:52)feraz yazdı:
(28/06/2021, 09:45)drummers yazdı: Rows.Count - 1
Yerine alttaki gibi yazılabilr.A sütununun son dolu satırını bulur.
cells(rows.count,"A").end(3).row

Teşekkürler
drummers, 11-05-2009 tarihinden beri AccessTr.neT üyesidir.
Cevapla
#4
Boşu boşuna hiç kasmadan olayı bir videodan gördüğüm gibi yaptım. Gayet hızlı ve performanslı bi aktarım oldu

NuGet Package Manager den

"ExcelDataReader"
"ExcelDataReader.DataSet" bunları ekliyoruz

Forma
1 adet DataGridView
1 adet textbox
1 adet button
1 adet combobox ekliyoruz

Kodlar şu

Kod:
Imports System.Globalization
Imports System.IO
Imports ClosedXML.Excel
Imports ExcelDataReader

Dim tables As DataTableCollection

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Using ofd As OpenFileDialog = New OpenFileDialog() With {.Filter = "Excel File|*.xlsx;*.xls"}
            If ofd.ShowDialog() = DialogResult.OK Then
                TextBox1.Text = ofd.FileName

                Using stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read)
                    Using reader As IExcelDataReader = ExcelReaderFactory.CreateReader(stream)
                        Dim result As DataSet = reader.AsDataSet(New ExcelDataSetConfiguration() With {
                                                                 .ConfigureDataTable = Function(__) New ExcelDataTableConfiguration() With {
                                                                 .UseHeaderRow = True}})
                        tables = result.Tables
                        ComboBox1.Items.Clear()

                        For Each table As DataTable In tables
                            ComboBox1.Items.Add(table.TableName)
                            ComboBox1.Enabled = True
                        Next



                    End Using

                End Using

            End If

        End Using
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Try
            Dim dt As DataTable = tables(ComboBox1.SelectedItem.ToString())

            DataGridView1.DataSource = dt
            

        Catch ex As Exception

        End Try


    End Sub

Bu kadar
drummers, 11-05-2009 tarihinden beri AccessTr.neT üyesidir.
Cevapla
#5
Paylaştığınız için sağolun.
Cevapla
#6
Pekii Ado ile denediniz mi?
Ado ilede select yöntemi ile alınabilir bence.
Cevapla

Bir hesap oluşturun veya yorum yapmak için giriş yapın

Yorum yapmak için üye olmanız gerekiyor

ya da
Task