2009-02-11 118 views
1

我有多個excel文件,我將其作爲數據表打開 我想在.net中合併所有這些數據表單的DataTable。如何將多個數據表合併爲一個?

例如, Apr09.xls,May09.xls,Jun09.xls

所有有dataas如下

Apr09.xls

EMPCODE,PresentDays 
0001 ,30 
0002 ,21 

May09.xls

EMPCODE,PresentDays 
0001 ,25 
0002 ,30 

新的數據表如下

EMPCODE,PresentDays 
0001 ,30 
0002 ,21 
0001 ,25 
0002 ,30 

如何創建這個。

回答

1

Datatable.Merge可以使用如下

For Each fileName As String In Directory.GetFiles("C:\\TEMP\\", "*.xls") 
    Dim connectionString As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=""Excel 8.0;HDR=NO;IMEX=1;""", fileName) 
    Dim adapter As New OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString) 
    Dim ds As New DataSet 
    adapter.Fill(ds, "anyNameHere") 
    Dim TempTable As DataTable 
    TempTable = ds.Tables.Item("anyNameHere") 
    table1.Merge(TempTable) 
    MsgBox(fileName) 
Next 
DataGridView1.DataSource = table1 
MsgBox(table1.Rows.Count) 
相關問題