Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过OleDB读取Excel文件时,非空单元格中的DBNull_C#_Excel_Oledb_Dbnull - Fatal编程技术网

C# 通过OleDB读取Excel文件时,非空单元格中的DBNull

C# 通过OleDB读取Excel文件时,非空单元格中的DBNull,c#,excel,oledb,dbnull,C#,Excel,Oledb,Dbnull,我使用OleDB读取Excel文件。其中一列具有“通用”格式,包含字母字符串和仅由数字组成的值。字符串值的检索没有问题,但纯数值的检索方式为DBNull 如何解决 我使用以下连接字符串打开Excel 2003文件(xls): 我在Microsoft网站上找到了一些适用于您的场景的文档。他们建议将所有数字转换为字符串 如前所述,ADO必须猜测Excel工作表或范围中每列的数据类型。(这不受Excel单元格格式设置的影响。)如果同一列中的数值与文本值混合,则可能会出现严重问题。Jet和ODBC提供

我使用OleDB读取Excel文件。其中一列具有“通用”格式,包含字母字符串和仅由数字组成的值。字符串值的检索没有问题,但纯数值的检索方式为
DBNull

如何解决

我使用以下连接字符串打开Excel 2003文件(xls):


我在Microsoft网站上找到了一些适用于您的场景的文档。他们建议将所有数字转换为字符串

如前所述,ADO必须猜测Excel工作表或范围中每列的数据类型。(这不受Excel单元格格式设置的影响。)如果同一列中的数值与文本值混合,则可能会出现严重问题。Jet和ODBC提供程序都返回多数类型的数据,但少数数据类型返回NULL(空)值。如果这两种类型在列中相等地混合,则提供程序将选择数字而不是文本

例如:

* In your eight (8) scanned rows, if the column contains five (5) numeric values and three (3) text values, the provider returns five (5) numbers and three (3) null values.
* In your eight (8) scanned rows, if the column contains three (3) numeric values and five (5) text values, the provider returns three (3) null values and five (5) text values.
* In your eight (8) scanned rows, if the column contains four (4) numeric values and four (4) text values, the provider returns four (4) numbers and four (4) null values.

我有同样的问题,但字符串值。返回数值,但字符串值返回为NULL。我想将数值作为字符串值加载。该列可以包含数字和非数字代码(例如:100344567和PRD001X01)

Excel驱动程序认为该列的类型为numeric,因为前8行中的值的类型为numeric

即使我将单元格定义为文本(格式单元格),它也不起作用

为了迫使驱动程序将列作为字符串“查看”,我只需在数值('100344567)前面加一个引号。这会将数值转换为字符串

此8行的值在注册表HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel\-->TypeGuessRows=8中定义


我希望它会有帮助。

我添加了
IMEX=1
来强制文本模式(我需要以文本而不是数字的形式检索此单元格),但它没有帮助。无法在文本模式下重新输入值,因为其中有许多值。是否有可能以某种方式将其自动化?如果您尝试使用查询而不是直接表/工作表名称的建议,会怎么样?一个小小的缺点是,所有的列都会以字符串的形式出现。
* In your eight (8) scanned rows, if the column contains five (5) numeric values and three (3) text values, the provider returns five (5) numbers and three (3) null values.
* In your eight (8) scanned rows, if the column contains three (3) numeric values and five (5) text values, the provider returns three (3) null values and five (5) text values.
* In your eight (8) scanned rows, if the column contains four (4) numeric values and four (4) text values, the provider returns four (4) numbers and four (4) null values.