简体   繁体   English

使用C#查询Excel文件,发出数字单元格

[英]Query an Excel file with C#, issue with the numeric cells

I am querying an Excel file to retrieve data record and display them in a monitoring tool. 我正在查询Excel文件以检索数据记录并将其显示在监视工具中。 The thing is that whenever I am trying to select a record with its id, with the following request, it throws an error when executing the query: 问题是,每当我尝试选择具有其id的记录时,使用以下请求,它在执行查询时会抛出错误:

ExecuteCommandTextErrorHandling ExecuteCommandTextErrorHandling

telling me that the type in the query and the target cell are not compatible. 告诉我查询中的类型和目标单元格不兼容。

My request looks like 我的请求看起来像

SELECT TOP 20 [Fields...] 
FROM [RECORD$] 
WHERE (([id] = '1'))

The id column can contains more than just numbers so its cell type is the default type which is standard. id列可以包含多个数字,因此其单元格类型是标准的默认类型。

My question is : how can I fetch the record without knowing its type before? 我的问题是 :如何在不知道其类型之前获取记录?

Do I have to enforce the column type, if so how? 我是否必须强制执行列类型,如果是这样的话? Do I have to catch the exception and retry without the quotes ? 我是否必须捕获异常并在没有引号的情况下重试?

Here is the code used to query the Excel file and my connection string 这是用于查询Excel文件和我的连接字符串的代码

 public static DataTable queryXlsFile(String query, OleDbConnection dbConnection)
 {
        OleDbDataAdapter dbCommand = new OleDbDataAdapter(query, dbConnection);
        DataTable dt = new DataTable();
        dbCommand.Fill(dt);
        return dt;
    }

Provider=Microsoft.ACE.OLEDB.12.0; data source=C:\\test\\321.xls; Extended Properties=\"Excel 8.0;HDR=Yes;\";

Thanks in advance. 提前致谢。

try adding IMEX=1; 尝试添加IMEX = 1;

HDR=Yes;IMEX=1" HDR =是; IMEX = 1"

如果你有很多行,那么类型是最多的类型,否则类型取决于第一行,一个答案的类型是字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM