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

发布于 09-08 09:09 字数 314 浏览 6 评论 0原文

我使用 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\""

I use OleDB to read an Excel file. One of the columns has a "Common" format and contains both strings with letters and values consisting of numbers only. String values are retrieved without problem, but pure numerical values are retrieved as DBNull.

How to solve?

I use the following connection string to open Excel 2003 file (xls):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天冷不及心凉2024-09-15 09:09:17

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

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.

I found some documentation on Microsoft's website that applies to your scenario. They recommend converting all the numerics to strings.

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

As stated previously, ADO must guess at the data type for each column in your Excel worksheet or range. (This is not affected by Excel cell formatting settings.) A serious problem can arise if you have numeric values mixed with text values in the same column. Both the Jet and the ODBC Provider return the data of the majority type, but return NULL (empty) values for the minority data type. If the two types are equally mixed in the column, the provider chooses numeric over text.

For example:

* 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.
强辩2024-09-15 09:09:17

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

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

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

为了强制驱动程序将列“视为”字符串,我只需在数值前面加上引号('100344567)。这会将数值转换为字符串。

这个 8 行值在注册表 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel\ -->TypeGuessRows=8 中定义。

我希望它会有所帮助。

I have the same problem but with string values. Numeric values are returned but string values are returned as NULL. I want to load numeric values as string values. The column can contain digits and non-numeric codes (example: 100344567 and PRD001X01).

The Excel driver considers that the column is of type numeric because the values in the first 8 rows are of type numeric.

Even if I define the cells as Text (Format Cell) it does not work.

To force the driver to "see" the column as string I have simply put a quote in front of the numeric value ('100344567). This turns the numeric value into a string.

This value of 8 rows is defined in the Registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel\ -->TypeGuessRows=8.

I hope it will help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文