不能在C#中打开Excel文件 [英] Cannot open Excel file in C#
问题描述
我有下面的C#函数在我的项目,这是应该打开并返回一个现有的Excel工作簿对象:
I have the following C# function in my project, which is supposed to open and return an existing Excel workbook object:
Application _excelApp;
// ...
private Workbook OpenXL(string path, string filename)
{
try
{
if (_excelApp == null)
{
_excelApp = new Application();
}
Workbook workBook = _excelApp.Workbooks.Open(path + filename, // Name
0, // Do not update links
true); // Open read-only
return workBook;
}
catch (Exception e)
{
_excelApp = null;
throw new ArgumentException("Error opening " + path + filename, e);
}
}
但是,当我用C运行它:\ 和scratch.xlsx中,open()调用引发以下错误:
But when I run it with "C:\" and "scratch.xlsx", the Open() call throws the following error:
Microsoft Excel cannot access the file 'C:\scratch.xlsx'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
文件和路径是存在的:我抄从错误信息的路径,并将其粘贴到命令窗口和文件加载了在Excel中。
中的文件没有锁定:Excel可以打开它不错,但我的程序不能,即使重新启动后直。
我不是想救它,我试图打开它,因此最后一个选项是不相关的。
The file and path does exist: I have copied the path from the error message and pasted it into a command window and the file loads up in Excel. The file is not locked: Excel can open it fine but my program cannot, even straight after a reboot. I am not trying to save it, I am trying to open it so the last option is irrelevant.
我很茫然理解为什么这个简单这段代码是行不通的。任何建议将非常感激。
I am at a loss to understand why this simple piece of code is not working. Any suggestions would be hugely appreciated.
我现在已经尝试从我个人的网络驱动器(M :)和U盘打开该文件。一切都是徒劳。
[edit] I have now tried opening that file from my personal network drive (M:) and from a USB stick. All to no avail.
应用程序实际上是一个Windows服务,本地系统帐户,并生成报告下运行。目前,它写的CSV没有任何访问问题报告。现在我试图让它打开一个excel文件作为模板报告,并在各个领域继续进行。它是开放,它失败的Excel文件时。我想到的是管理员帐户选项每个人都在暗示是一个红色的鲱鱼,因为它可以写的CSV文件wityh没有问题。
[/编辑]
The application is actually a Windows service, running under the local system account and generating reports. It currently write CSV reports with no access problems whatsoever. I am now trying to get it to open an excel file as a template report and fill in various fields. It is when opening the Excel file that it fails. I am thinking that the administrator account option everyone is suggesting is a red herring since it can write CSV files wityh no problem. [/edit]
---阿利斯泰尔。
推荐答案
我发现了以下页面:
凡说...
它不支持自动办公产品UI少。看来,Windows Server 2008和Excel 2007中执行定语句。
it’s not supported to automate office products UI less. It seems that Windows Server 2008 and Excel 2007 enforce the given statement.
提问者接着描述正是我与Windows上午的情况。服务无法打开一个Excel文件,虽然在一个命令行程序相同的代码没有问题
The questioner then describes exactly the situation I am in with a Windows Service that cannot open an Excel file, although the same code in a command-line program has no problem.
响应建议创建以下文件夹:
The response advises to create the following folder:
的Windows 2008服务器的x64:C:\Windows\SysWOW64\config\systemprofile\Desktop
Windows 2008 Server x64: C:\Windows\SysWOW64\config\systemprofile\Desktop
的Windows 2008服务器的x86:C:\Windows\System32\config\systemprofile\Desktop
Windows 2008 Server x86: C:\Windows\System32\config\systemprofile\Desktop
我曾经尝试这样做,它的工作一种享受!任何人都可以解释为什么它需要的任何缺点?
I have tried this and it worked a treat! Can anyone explain why it is needed and any downsides?
谢谢,
---阿利斯泰尔。
这篇关于不能在C#中打开Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!