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.

The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
  • 1
    Have you tried different providers, like JET instead of ACE? Take a look here for other possible connection string formats: http://www.connectionstrings.com/excel – goric Dec 30 '10 at 01:15
  • 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... – The Muffin Man Dec 31 '10 at 00:40
  • 1
    @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. – arcain Jan 11 '11 at 23:05

1 Answers1

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.

arcain
  • 14,920
  • 6
  • 55
  • 75
  • 11
    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\Excel` – amarsuperstar Jul 06 '11 at 17:14
  • I edit Registry, based on this [link](http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/5b22e94c-37a9-4be5-ad55-3d9229220194/). 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? – soclose Jul 12 '11 at 08:30
  • 1
    @soclose The `HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node` key is only present on 64-bit Windows installations. http://en.wikipedia.org/wiki/WoW64 – arcain Jul 12 '11 at 14:41
  • 3
    Is there a way to do this programmatically? – Loogawa Mar 01 '13 at 20:39
  • 1
    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. – arcain Mar 01 '13 at 20:59
  • 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? – Shock Jun 04 '16 at 02:14
  • @Shock 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. – Salah Alshaal Oct 11 '16 at 06:56
  • Besides, worth to read osreads [anwser](https://stackoverflow.com/questions/5738407/is-there-a-better-way-to-indicate-null-values-in-excel) – Fredrick Gauss Jun 19 '17 at 09:03