我正在尝试使用 DataTable.Merge() 选项合并多个 excel 文件
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)
但是在合并时会出现以下错误
<target>.ColumnName and <source>.ColumnName have conflicting properties: DataType property mismatch.
这是因为 excel 中的一列被读取为文本,另一列被读取为双精度,而两者都有数值。
为了避免这种情况,我还在连接字符串中提到了 IMEX=1,但仍然出现此错误。