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

fla*_*nik 5 c# oledb excel dbnull

我使用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\""
Run Code Online (Sandbox Code Playgroud)

jdo*_*dot 6

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

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

如前所述,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.
Run Code Online (Sandbox Code Playgroud)

  • 我添加了`IMEX = 1`来强制执行文本模式(我需要将这些单元格检索为文本而不是数字),但它没有帮助.在文本模式下重新输入值是不可能的,因为它们中有很多.有可能以某种方式自动化它吗? (3认同)