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

如何解决?

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

"Provider=Microsoft.Jet.OLEDB.4.0;
 Data Source=C:\\file.xls;
 Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""
.

有帮助吗?

解决方案

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

http://support.microsoft.com/kb/257819

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

例如:

* 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驱动程序认为列为类型数字,因为前8行中的值是数字数字。

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

强制驱动程序“请参阅”列作为字符串,我只需在数字值前面放置引用('100344567)。这将数值变为字符串。

此值8行在注册表HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Jet \ 4.0 \ Engines \ Excel \ - > TypeguessRows= 8中定义。

我希望它会有所帮助。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top