在Windows7 64位上运行的C#WCF应用程序无法读取excel 2007文件(32位) [英] C# WCF application running on windows7 64-bit is unable to read excel 2007 file (32-bit)

查看:84
本文介绍了在Windows7 64位上运行的C#WCF应用程序无法读取excel 2007文件(32位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我在Windows 7 64位上使用Visual Studio 2013和Office 2007 32位。



我有ac#WCF服务库项目,它试图从excel文件中读取数据,如下所示:



string con = @Provider = Microsoft.ACE.OLEDB .12.0; +

@数据源= c:\ temp \ Book2.xlsx; +

@扩展属性='Excel 8.0; HDR =是; IMEX = 1;';



使用(OleDbConnection连接=新的OleDbConnection(骗局)

{

connection.Open();

OleDbCommand command = new OleDbCommand(select * from [Sheet1 $ ],连接);

var TranArr =新字符串[999,20];

使用(OleDbDataReader dr = command.ExecuteReader())

{etc ...





导致错误The'Microsoft.ACE.OLEDB.12.0 '提供商未在本地计算机上注册。



当我将相同的代码复制到ac#console应用程序时,它可以正常工作。我正在运行Windows 7 64位和32位Office 2007 excel。在Build属性中,平台设置为Any CPU。在WCF项目中,32位首选项未选中并显示为灰色,但在控制台应用程序中,它已被勾选。我已经尝试下载Microsoft Access数据库引擎2010 Redistributable,但这没有帮助。





如果我将平台目标更改为X86然后我收到以下运行时错误:



WcfSvcHost遇到严重错误,必须退出。这可能是由配置文件无效引起的。请查看下面的其他信息以获取详细信息。



System.BadImageFormatException:无法加载文件或程序集'file:/// C:\Users \ User \ documents \visual studio 2013 \Projects \DCRules2 \DCRules2 \ bin \Debug \DCRules2.dll'或其中一个依赖项。尝试加载格式不正确的程序。







有没有人遇到这个问题,更重要的是有人有修复?







问候

Pat

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

推荐答案

,连接);

var TranArr =新字符串[999,20];

使用(OleDbDataReader dr = command。 ExecuteReader())

{等等......





导致错误'Microsoft.ACE.OLEDB.12.0'提供程序未在本地计算机上注册。



当我将相同的代码复制到ac#console应用程序时,它可以工作我正在运行Windows 7 64位和32位Office 2007 excel。在Build属性中,平台设置为Any CPU。在WCF项目中,32位首选项未经检查并显示为灰色,但在控制台中应用程序已勾选。我已尝试下载Microsoft Access数据库引擎2010 Redis tributable,但这没有帮助。





如果我将平台目标更改为X86,那么我会收到以下运行时错误:



WcfSvcHost遇到严重错误,必须退出。这可能是由配置文件无效引起的。请查看下面的其他信息以获取详细信息。



System.BadImageFormatException:无法加载文件或程序集'file:/// C:\Users \ User \ documents \visual studio 2013 \Projects \DCRules2 \DCRules2 \ bin \Debug \DCRules2.dll'或其中一个依赖项。尝试加载格式不正确的程序。







有没有人遇到这个问题,更重要的是有人有修复?







问候

Pat
", 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


Excel文件不能是32位或64位(文档是文档),但Microsoft.ACE可能是。仅用于实验,您始终可以将应用程序编译为x86目标:项目属性 - > 构建标签 - > 平台目标列表框。 x86指令集架构与其他两种(64位)架构兼容,并通过WoW64( http://en.wikipedia.org/wiki/WoW64 [ ^ ])。我不相信问题存在;你需要检查你的代码。请看这些主题:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/b5ba0738-c03e-4a8c-bcc8 -ea1e15657a24 / the-microsoftaceoledb140-provider-is-not-registered-on-local-machine?forum = csharpgeneral [ ^ ],

http://stackoverflow.com/questions/17716207/the-microsoft-ace-oledb-12-0-provider-is-not-registered-on-the -local-machine-w [ ^ ]。



一个不错的选择是使用Open XML SDK。请参阅此过去引用的过去的答案:如何从MS Visual中添加参考添加microsoft excel 15.0对象库Studio 2010 [ ^ ] 。



-SA
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


嗨谢尔盖,

如果你看看我原来的帖子你会看到我已经将PLatform Target设置为X86,但这导致了我发布的运行时错误。



例如,为什么我可以成功在控制台应用程序中运行代码但是无法在WCF应用程序中运行相同的代码吗?



问候

Pat
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


这篇关于在Windows7 64位上运行的C#WCF应用程序无法读取excel 2007文件(32位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆