22

I am trying to merge multiple excel files using DataTable.Merge() option

    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)

But gives following error while merging

<target>.ColumnName and <source>.ColumnName have conflicting properties: DataType property mismatch.

This is due to one column in excel is read as text and another as double while both have numeric values.

To avoid this I also mentioned IMEX=1 in connection string, still getting this error.

2 Answers 2

52

Use MissingSchemaAction.Ignore as MissingSchemaAction parameter in Merge

table1.Merge(TempTable, True, MissingSchemaAction.Ignore)
1
  • 2
    Note: The question and answer is of VB.Net, hence the True used is for capitalized Feb 1, 2017 at 6:22
2

If the columns are numeric, correct the xls file treating that column as text.
Wouldn't you want the columns to be structurally same, when you merge the data?

1
  • 1
    Both are of type text. but this when the files are read using ADO it decides the column by looking at the fileds data. connectionstrings.com/excel-2007 For which I hv added IMEX=1 but still it is not working Feb 11, 2009 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.