Have a really weird problem with reading xlsx file(I'm using OleDbDataReader).

I have a column there that consist of the following data:

  • 50595855
  • 59528522
  • C_213154
  • 23141411

The problem is that when I read this column the reader shows me that the third row is empty. The column format in Excel is set to 'General'. But when I set the format to 'Text', everything works fine and reader sees the data in that row.

So just for a sake of experiment, I prefixed first two rows with letter and made it look like following :

  • C_50595855
  • C_59528522
  • C_213154
  • 23141411

And the reader reads everything without problem even when the column format is set to 'General'.

So Excel apparently somehow analyses data in the column before loading it, and it gets confused when first cells of the column look like numeric and some of the rest are texts..

It is really weird to me as either there is data in the cell or there isn't.

Anyone have any ideas why this is happening?

Any help will be much appreciated.

Regards, Igor

有帮助吗?

解决方案

As you surmised it's an issue caused by mixed data types. If you search on "OleDBDataReader mixed types" you'll get some answers. Here's an MSDN page that describes the problem:

"This problem is caused by a limitation of the Excel ISAM driver in that once it determines the datatype of an Excel column, it will return a Null for any value that is not of the datatype the ISAM driver has defaulted to for that Excel column. The Excel ISAM driver determines the datatype of an Excel column by examining the actual values in the first few rows and then chooses a datatype that represents the majority of the values in its sampling."

... and the solution(s):

"Insure that the data in Excel is entered as text. Just reformatting the Excel column to Text will not accomplish this. You must re-enter the existing values after reformatting the Excel column. In Excel, you can use F5 to re-enter existing values in the selected cell.

You can add the option IMEX=1; to the Excel connect string in the OpenDatabase method. For example:

Set Db = OpenDatabase("C:\Temp\Book1.xls", False, True, "Excel 8.0; HDR=NO; IMEX=1;")

"

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