2017-08-07 141 views
0

我有一个形式,主要想法是,将允许用户从Excel导入数据动态数据库,他将指定他需要的信息的列显示在下面从Excel导入数据到DataTable动态vb.net

enter image description here

,但是当我将数据传输到数据表我用差周期的图像数据导入到一个数据表,但它最终总是看起来像下面

enter image description here图像

这是我的代码

昏暗dtTtable,tempdtTable,tmpClmDtTable作为新的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

我已经找到了答案月底结束的专栏中,我将让代码的人怎么可能有类似的问题

 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 
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