繁体   English   中英

将数据从Excel动态导入到DataTable vb.net

[英]Import data from Excel to a DataTable Dynamically vb.net

我有一个表单,其主要思想是允许用户动态地将数据从excel导入数据库,他将指定需要信息的列,如下图所示。

在此处输入图片说明

但是,当我将数据传输到数据表时,我会使用不同的周期将数据导入到数据表中,但最终总是看起来像下面的图片

在此处输入图片说明

这是我的代码

Dim dtTtable,tempdtTable,tmpRTIDtTable作为新的数据表dtTtable = Ejecutar_Query(“ Select * From”&ComboBoxEdit1.EditValue,False)

        For clmn = 0 To ImportacionDtTable.Rows.Count - 1
            Dim sRange As String = ImportacionDtTable(clmn).Item(1)

            If String.IsNullOrEmpty(sRange) = False Then
                Dim nrw As DataRow = dtTtable.NewRow
                tmpClmDtTable = New DataTable
                tmpClmDtTable = GetDataExcel(OpenFileDialog1.FileName, SpreadsheetControl1.ActiveWorksheet.Name, sRange)
                tempdtTable.Merge(tmpClmDtTable)

                'tempdtTable = tmpClmDtTable.Copy
            End If

        Next
        'dtTtable.AcceptChanges()

        Dim tablas As New Form_Tabla
        With tablas
            .PdtTable = tempdtTable
            .ShowDialog()
            .Close()
        End With

我现在使用marge只是为了展示它如何结束最后的列

我找到了答案,我将让任何人的代码都可能遇到类似的问题

       Dim dtTtable, TempT1, TempT2 As New DataTable
        dtTtable = Ejecutar_Query("Select * From " & ComboBoxEdit1.EditValue, False)

 'Ejecurtar_Query is a pu`blic function that returns a datarow or data table i get the structure of the table I want.`

        Dim nfil As Integer = 0
        Dim dRange As String = Nothing
        For clmn = 0 To ImportacionDtTable.Rows.Count - 1
            Dim sRange As String = ImportacionDtTable(clmn).Item(1),
                clmName As String = ImportacionDtTable(clmn).Item(0)
            If String.IsNullOrEmpty(sRange) = False Then
                TempT1 = New DataTable
                TempT1 = GetDataExcel(OpenFileDialog1.FileName, SpreadsheetControl1.ActiveWorksheet.Name, sRange)
                Dim dtCl As New DataColumn
'I create a new datacolumn then I add it in the my second data table 

                dtCl = TempT1.Columns(0)
                TempT2.Columns.Add(clmName, dtCl.DataType)

                If TempT2.Rows.Count < TempT1.Rows.Count Then
                    For n = 0 To TempT1.Rows.Count - TempT2.Rows.Count
                        TempT2.Rows.Add()
                    Next
                End If
                Dim nCl As Integer = TempT2.Columns.Count

                For fila = 0 To TempT1.Rows.Count - 1
                    TempT2(fila).Item(nCl - 1) = TempT1(fila).Item(0)
                Next

            End If
        Next


  'then after I create the table, my data table where I need the data on, will add the rows then I will navigate in my temporary datatable to insert the data to later display it in a grid

        For x = 0 To TempT2.Rows.Count - 1
            dtTtable.Rows.Add()
        Next
        Dim nitm As Integer = 0
        For clmn = 0 To ImportacionDtTable.Rows.Count - 1
            Dim sRange As String = ImportacionDtTable(clmn).Item(1)
            If String.IsNullOrEmpty(sRange) = False Then
                For nFila = 0 To TempT2.Rows.Count - 1
                    dtTtable(nFila).Item(clmn) = TempT2(nFila).Item(nitm)
                Next
                nitm += 1
            End If
        Next

尝试这个。

Imports System.Data.SqlClient
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop

Public Class Form1

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

        Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim DtSet As System.Data.DataSet
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
        MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Users\Excel\Desktop\Book1.xls';Extended Properties=Excel 8.0;")
        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
        MyCommand.TableMappings.Add("Table", "Net-informations.com")
        DtSet = New System.Data.DataSet
        MyCommand.Fill(DtSet)
        DataGridView1.DataSource = DtSet.Tables(0)
        MyConnection.Close()

    End Sub

End Class

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM