Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

I have a problem reading an excel file in .net

I have something like this:

 

string strConn = string.Empty;

strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +

"Data Source=D:\\ICRs.xls;" +

"Extended Properties=Excel 8.0;";

 

OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [sheet1$]", strConn);

 

In the excel file I have a column with data like these, and the format is fixed, I can´t change it:

1256

1256

1256A

 

When it finds the row with 12356A it retrieves a System.DbNull, instead of 1256A.

Can somebody tell me how can I read the data???

 

Thanks in advance.

Adolfo.
Posted

I haven't run any tests on this yet, and it has been almost a year since I messed with Excel. However, I think you are running into a data type issue. If no type is specified, JET may use the first 20 rows to determine the data type for a particular column of data. In the example you give, if the first 20 or so rows are all Integer types, then any String value found after that may be read as DbNull.

 

Having said that, I remember having some exceptions thrown in that case, but I could be remembering it wrong or you could masking the exception with an incomplete Try-Catch block.

 

Sorry I couldn't be more help at this time, but at least we can take care of the easy stuff first.

"Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
Posted

I solved the problem writing something like this in the connection string:

"Excel 8.0; HDR=NO; IMEX=1;"

 

But another problem appears, in some rows it retrieves 3.45678 e7, and if I convert the data into integer, I lose precision.

 

Does anybody know how to solve it??

Thanks.

Adolfo.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...