无法在 C# 中打开 Excel 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17530797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot open Excel file in C#
提问by user41013
I have the following C# function in my project, which is supposed to open and return an existing Excel workbook object:
我的项目中有以下 C# 函数,它应该打开并返回一个现有的 Excel 工作簿对象:
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);
}
}
But when I run it with "C:\" and "scratch.xlsx", the Open() call throws the following error:
但是当我使用“C:\”和“scratch.xlsx”运行它时,Open() 调用会引发以下错误:
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.
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.
文件和路径确实存在:我已经从错误消息中复制了路径并将其粘贴到命令窗口中,并且文件在 Excel 中加载。文件未锁定:Excel 可以正常打开它,但我的程序无法打开,即使在重新启动后也是如此。我不是要保存它,而是要打开它,因此最后一个选项无关紧要。
I am at a loss to understand why this simple piece of code is not working. Any suggestions would be hugely appreciated.
我不明白为什么这段简单的代码不起作用。任何建议将不胜感激。
[edit] I have now tried opening that file from my personal network drive (M:) and from a USB stick. All to no avail.
[编辑] 我现在尝试从我的个人网络驱动器 (M:) 和 U 盘打开该文件。一切都无济于事。
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]
该应用程序实际上是一个 Windows 服务,在本地系统帐户下运行并生成报告。它目前编写 CSV 报告,没有任何访问问题。我现在试图让它打开一个excel文件作为模板报告并填写各个字段。打开 Excel 文件时失败。我认为每个人都建议的管理员帐户选项是一个红鲱鱼,因为它可以毫无问题地写入 CSV 文件。[/编辑]
--- Alistair.
--- 阿利斯泰尔。
采纳答案by user41013
I found the following page:
我找到了以下页面:
Where it says that...
哪里说的...
it's not supported to automate office products UI less. It seems that Windows Server 2008 and Excel 2007 enforce the given statement.
不支持自动化办公产品 UI。似乎 Windows Server 2008 和 Excel 2007 强制执行给定的语句。
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.
然后提问者准确地描述了我使用 Windows 服务无法打开 Excel 文件的情况,尽管命令行程序中的相同代码没有问题。
The response advises to create the following folder:
响应建议创建以下文件夹:
Windows 2008 Server x64: C:\Windows\SysWOW64\config\systemprofile\Desktop
Windows 2008 Server x86: C:\Windows\System32\config\systemprofile\Desktop
Windows 2008 Server x64:C:\Windows\SysWOW64\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?
我试过这个,效果很好!谁能解释为什么需要它以及任何缺点?
Thanks,
谢谢,
--- Alistair.
--- 阿利斯泰尔。
回答by Pharap
Run the program as admin, the C:/ cannot be accessed by a program unless the user is running as admin. You can make your program prompt the user it must be run as admin by altering the ApplicationManifest: How do I force my .NET application to run as administrator?
以管理员身份运行程序,除非用户以管理员身份运行,否则程序无法访问 C:/。您可以通过更改 ApplicationManifest 使您的程序提示用户必须以管理员身份运行:如何强制我的 .NET 应用程序以管理员身份运行?
回答by thejustv
I was running into the same issue and I have investigated infomation about "registry hack".
我遇到了同样的问题,我调查了有关“注册表黑客”的信息。
After all, I found another solution that changes no registry values and everything works on properly.
毕竟,我找到了另一个不更改注册表值并且一切正常的解决方案。
This solution is ...
这个解决方案是...
?Windows 2008 Server x64
?Windows 2008 服务器 x64
Please make this folder.
请制作此文件夹。
C:\Windows\SysWOW64\config\systemprofile\Desktop
?Windows 2008 Server x86
?Windows 2008 服务器 x86
Please make this folder.
请制作此文件夹。
C:\Windows\System32\config\systemprofile\Desktop
...instead of dcomcnfg.exe.
...而不是 dcomcnfg.exe。
This operation took away office automation problems in my system.
此操作消除了我系统中的办公自动化问题。
A Desktop folder seems to be necessary in the systemprofile folder to open file by Excel.
systemprofile 文件夹中似乎需要一个 Desktop 文件夹才能通过 Excel 打开文件。
It disappears from Windows2008, Windows2003 had the folder, and I think it cause this error.
它从 Windows2008 消失,Windows2003 有文件夹,我认为它会导致此错误。
I think it is safer than "registry hack".
我认为它比“注册表黑客”更安全。
If you try this solution, please let me know results.
如果您尝试此解决方案,请告诉我结果。

