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

7tofc5zh  于 2023-02-25  发布在  .NET
关注(0)|答案(3)|浏览(113)

我有一个表单,其主要思想是允许用户动态地将数据从Excel导入到数据库中,他将指定他需要信息的列,如下图所示
enter image description here
但当我将数据传输到数据表时,我使用了差异循环将数据导入到数据表,但结果总是如下图所示
enter image description here
这是我的密码
将dtTtable、tempdtTable、tmpClmDtTable作为新数据表dtTtable =弹出查询("选择 * 来自"&组合框Edit1.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来显示它是如何在最后的列中结束的

vyswwuz2

vyswwuz21#

我已经找到了答案,我会让代码为任何人怎么可能有类似的问题

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
woobm2wo

woobm2wo2#

试试这个。

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
vaqhlq81

vaqhlq813#

我有类似的情况,我允许用户从Excel工作表中选择列名称显示为下拉列表中的Datagrid,将Map数据库列,并在导入时单击合并到数据库表。
私有子SimpleButton1_Click(sender As Object,e As EventArgs)处理SimpleButton 1.单击Dim pfad As String Dim filename As String Dim ds As New DataSet使用fbd As New打开文件对话框fbd.Filter =“Excel文件|.xls格式;.xlsx;“

If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
            Try
                TextEdit1.Text = fbd.FileName

                pfad = fbd.FileName

                ds = GetExcelDataSet(pfad, True)
                dt = ds.Tables(0)
                Dim columnNames = (From c In ds.Tables(0).Columns.Cast(Of DataColumn)() Select c.ColumnName).ToArray()
                System.Console.WriteLine(columnNames(0))

                

             
               
                riComboBox.Items.Clear()
                'riComboBox.Items.AddRange(columnNames)
                For i As Integer = 0 To columnNames.Length - 1

                    riComboBox.Items.Add(columnNames(i))

                Next

                Update_ImportField()

            Catch ex As Exception

            End Try

           

        End If

    End Using

End Sub
Public Shared Function GetExcelDataSet(ByVal excelFile As String, ByVal hasHeaders As Boolean) As DataSet


    Using stream = File.Open(excelFile, FileMode.Open, FileAccess.Read)

        Using reader = ExcelReaderFactory.CreateReader(stream)
            Dim conf As New ExcelDataSetConfiguration With {
                .ConfigureDataTable = Function(__) New ExcelDataTableConfiguration With {
                    .UseHeaderRow = hasHeaders
                }
            }
            Dim dataSet = reader.AsDataSet(conf)

            Return dataSet
        End Using
    End Using



End Function

作为布尔值的私有函数Update_ImportField()

Dim id As Integer = GridView1.GetFocusedRowCellValue("ID")
    Dim spalte As String = GridView1.GetFocusedRowCellValue("Spalte").ToString
    Using o As New Importfelder
        o.ID = id
        o.Spalte = spalte
        ObjektData.InsertExcelColumn(o)
    End Using
End Function[enter image description here][1]

相关问题