Help with a OleDB connection string for excel files
Asked Answered
P

1

20

The problem i'm having is that the data adapter is looking at only the first row in each column to determine the data type. In my case the first column "SKU" is numbers for the first 500 rows then I happen to have SKU's which are mixed numbers and letters. So what ends up happening is rows in the SKU column are left blank, but I still get the other information for each column row.

I believe it is the connection string that controls that and with my current settings it should work, however it is not.

Connection String:

conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nick\Desktop\Pricing2.xlsx" + @";Extended Properties=""Excel 12.0 Xml;HDR=Yes;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";


ImportMixedTypes=Text;TypeGuessRows=0

Should be the important keywords, look at 0 rows and just use text as the value types for everything.

The "bandaid" I have put on this is to make the first row in the spreadsheet a mixture of letters and numbers and specifically leave that row out in my query.

Publican answered 29/12, 2010 at 3:12 Comment(3)
Have you tried different providers, like JET instead of ACE? Take a look here for other possible connection string formats: connectionstrings.com/excelCeja
I've already been to that site and tried everything. I wish there was a better way to do this. If they want to design the system to take a long string they need to release a free generator. Ugh...Publican
@theprise JET will still have the same problem since the values that need to be modified are all in the registry, and cannot be set from the connection string.Pericarp
P
31

Unfortunately, you can't set ImportMixedTypes or TypeGuessRows from the connection string since those settings are defined in the registry. For the ACE OleDb driver, they're stored at

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\14.0\Access Connectivity Engine\Engines\Excel

in the registry. So, you can simplify your connection string to:

conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nick\Desktop\Pricing2.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=Yes;IMEX=1;""";

Once you set TypeGuessRows to 0 and ImportMixedTypes to Text in the registry, you should get the behavior you are expecting. You might, however, consider using a suitably large number like 1000 instead of zero if you find import performance to be less than ideal.

Pericarp answered 11/1, 2011 at 23:2 Comment(8)
Just to note for anyone looking at this, if you use a 64 bit machine then you need to add Wow6432Node into the registry key so it becomes: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Office\12.0\Access Connectivity Engine\Engines\ExcelKofu
I edit Registry, based on this link. It works in window xp but doesn't work in window 7. Do I need to put Wow6432Node in the registry key for 32 or 64 bit?Rozanneroze
@Rozanneroze The HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node key is only present on 64-bit Windows installations. en.wikipedia.org/wiki/WoW64Pericarp
Is there a way to do this programmatically?Tinytinya
One way you could set the values via code would be using the Microsoft.Win32.Registry class, but I wouldn't suggest doing that on the fly just before opening the connection.Pericarp
I've searched my registry for "TypeGuessRows" and changed it from 8 to 0 in about 5 different places. It has no effect at all. I have a column that has text in the 1,000th row and the import still tries to make it a float based on the first few rows being numeric. Any ideas?Pratfall
@Pratfall I wrote a macro that added a character " ' " to the start of each column, and I cut it before reading, it seems to work fine.Springfield
Besides, worth to read osreads anwserTrochilus

© 2022 - 2024 — McMap. All rights reserved.