【问题标题】:Error: <target>.ColumnName and <source>.ColumnName have conflicting properties: DataType property mismatch错误:<target>.ColumnName 和 <source>.ColumnName 具有冲突的属性:DataType 属性不匹配
【发布时间】:2010-10-06 20:25:28
【问题描述】:

我正在尝试使用 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,但仍然出现此错误。

【问题讨论】:

    标签: .net vb.net excel datatable


    【解决方案1】:

    在 Merge 中使用 MissingSchemaAction.Ignore 作为 MissingSchemaAction 参数

    table1.Merge(TempTable, True, MissingSchemaAction.Ignore)
    

    【讨论】:

    • 注意:问题和答案是VB.Net的,因此使用的True是大写的
    【解决方案2】:

    如果列是数字,请更正将该列视为文本的 xls 文件。
    当您合并数据时,您不希望列在结构上相同吗?

    【讨论】:

    • 两者都是文本类型。但是当使用 ADO 读取文件时,它会通过查看文件数据来决定列。 connectionstrings.com/excel-2007 我为此添加了 IMEX=1 但仍然无法正常工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 2011-04-03
    • 2022-12-03
    相关资源
    最近更新 更多