Click here to Skip to main content
15,031,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am using Visual studio 2013 on Windows 7 64-bit with Office 2007 32-bit.

I have a c# WCF service library project which is trying to read data from an excel file as follows:

string con = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data Source=c:\temp\Book2.xlsx;" +
@"Extended Properties='Excel 8.0;HDR=Yes; IMEX=1;'";

using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
var TranArr = new string[999, 20];
using (OleDbDataReader dr = command.ExecuteReader())
{ etc...


This results in an error "The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine."

When I copy the same code into a c# console application it works fine. I am running Windows 7 64-bit and 32-bit Office 2007 excel. In the Build properties the platform is set to "Any CPU". In the WCF project the 32-bit preferred is unchecked and greyed out but in the Console application it is ticked. I have tried downloading Microsoft Access Database Engine 2010 Redistributable , but this has not helped.


If I change the Platform target to X86 then I get the following runtime error:

WcfSvcHost encountered a critical error and must exit. This may be caused by invalid configuration file. Please inspect additional information below for detail.

System.BadImageFormatException: Could not load file or assembly 'file:///C:\Users\User\documents\visual studio 2013\Projects\DCRules2\DCRules2\bin\Debug\DCRules2.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.



Has any one come across this before and more importantly does anyone have a fix ?



regards
Pat
Posted

The Excel file cannot be 32-bit or 64-bit (document is document), but Microsoft.ACE could be. Just for an experiment, you can always compile your application to x86 target: Project Properties -> "Build" tab -> "Platform target" list box. The x86 instruction-set architecture is compatible with two other (64-bit) architectures and is supported on 64-bit versions of Windows via WoW64 (http://en.wikipedia.org/wiki/WoW64[^]). I don't believe the problem is there; you rather need to check up your code. Please see these threads:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/b5ba0738-c03e-4a8c-bcc8-ea1e15657a24/the-microsoftaceoledb140-provider-is-not-registered-on-the-local-machine?forum=csharpgeneral[^],
http://stackoverflow.com/questions/17716207/the-microsoft-ace-oledb-12-0-provider-is-not-registered-on-the-local-machine-w[^].

One good alternative is using Open XML SDK instead. Please see my past answers referenced from this one: How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

—SA
   
Hi Sergey,
if you look at my original post you will see that I already set PLatform Target to X86 but this resulted in the run time error that I also posted.

As per example , why can I run code in Console application successfully but am unable to run identical code in WCF application ?

regards
Pat
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900