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

我使用OleDB来读取一个Excel文件。 其中一列具有“通用”格式,并且包含两个字母和数字组成的string。 string值检索没有问题,但纯数值检索为DBNull

怎么解决?

我使用下面的连接string打开Excel 2003文件 (xls):

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

我在微软网站上发现了一些适用于您的场景的文档。 他们build议将所有数字转换为string。

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

如前所述,ADO必须猜测Excel工作表或范围中每列的数据types。 (这不受Excel单元格格式设置的影响。)如果在同一列中将数值与文本值混合,则会出现严重问题。 Jet和ODBC提供程序都返回多数types的数据,但为less数数据types返回NULL(空)值。 如果这两种types在列中均匀混合,提供者将在文本上select数字。

例如:

 * 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. 

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

Excel驱动程序认为该列是数字types的,因为前8行中的值是数字types。

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

为了强制驱动程序“看到”string作为string,我只是把一个报价前面的数字值('100344567)。 这将数字值转换为一个string。

在registryHKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Jet \ 4.0 \ Engines \ Excel \ – > TypeGuessRows = 8中定义了这8行的值。

我希望这会有所帮助。