我有一个表单主要是允许用户动态地将数据从excel导入数据库,他将指定他需要信息的列,如下图所示
但是当我将数据传输到数据表时,我使用差异周期将数据导入到数据表中,但它总是看起来像下面的图像
这是我的代码
Dim dtTtable,tempdtTable,tmpClmDtTable As New DataTable 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来展示它最终是如何结束
答案 0 :(得分:0)
我找到了答案,我会让任何人的代码可能有类似的问题
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
答案 1 :(得分:0)
试试这个。
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