【发布时间】:2017-02-07 02:21:54
【问题描述】:
我有一个DataGridView,我正在从多个 Excel 文件中导入数据。但是每次我导入数据时,它都会覆盖以前的数据。如何让它在数据网格视图中将下一个 excel 文件添加到上一个文件的末尾?
If DataGridView1.RowCount > 0 Then
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\Work\4pc_test1.xlsx;Extended Properties=Excel 12.0;")
'MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fd.FileName & "';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)
Dim tab As DataTable = DtSet.Tables(0)
DataGridView1.DataSource = tab
MyConnection.Close()
Else
'The below connection allows for the opening of .xls files and .xlsx. The one reamed out below doesnt open up .xls files.
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\Work\4pc_test1.xlsx;Extended Properties=Excel 12.0;")
'MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fd.FileName & "';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 If
【问题讨论】:
-
一个 DGV 不能有多个数据源——它是单数的。是我,还是 If 和 Else 块都在做同样的事情,除了一个有一个额外的 DataTable 变量
-
不是没有,但你问了16个问题,得到了18个答案;只接受了一个,只投票了一次。接受您认为有用的答案和点赞帖子可以帮助其他人找到好的答案。 Tour 解释了它。如果来源相同,可以追加到数据表中。
标签: .net excel vb.net winforms datagridview