Microsoft Office Excel 无法访问文件“c:\inetpub\wwwroot\Timesheet\App_Data\Template.xlsx”
我的代码如下:-
Microsoft.Office.Interop.Excel.Application oXL = null;
Microsoft.Office.Interop.Excel.Sheets sheets;
Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Workbooks.Add(System.Reflection.Missing.Value);
/*
* Here is the complete detail's about Workbook.Open()
*
* Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format,
* Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin,
* Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)
*/
Workbook workbook = excel.Workbooks.Open(
System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Template.xlsx"),
Missing.Value, true, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value);
sheets = workbook.Worksheets;
现在该行:-
workbook = excel.Workbooks.Open(
System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Template.xlsx"),
Missing.Value, true, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value);
它直接从 Visual Studio (F5) 执行,但是当我尝试使用 IIS 访问它时,它无法工作。 抛出错误如下:-
Microsoft Office Excel 无法访问文件“c:\inetpub\wwwroot\Timesheet\App_Data\Template.xlsx”。可能的原因有以下几种:
• 文件名或路径不存在。
• 该文件正在被另一个程序使用。
• 您尝试保存的工作簿与当前打开的工作簿同名。
我已尝试解决方法:-
- 文件夹和文件存在,允许 IUSR_###(IIS 用户)和文件所在文件夹中的 ASPNET 用户访问。
- 在组件服务 (DCOM) 中向适当的用户授予访问权限。
我已经授予模板(.xlsx)所在文件夹的所有权限有
什么建议吗?
I have my code as follows :-
Microsoft.Office.Interop.Excel.Application oXL = null;
Microsoft.Office.Interop.Excel.Sheets sheets;
Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Workbooks.Add(System.Reflection.Missing.Value);
/*
* Here is the complete detail's about Workbook.Open()
*
* Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format,
* Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin,
* Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)
*/
Workbook workbook = excel.Workbooks.Open(
System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Template.xlsx"),
Missing.Value, true, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value);
sheets = workbook.Worksheets;
Now for the line :-
workbook = excel.Workbooks.Open(
System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Template.xlsx"),
Missing.Value, true, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value);
It gets executed directly from the visual studio (F5) but when i try to access it with IIS it wont work.
Throws error as follows:-
Microsoft Office Excel cannot access the file 'c:\inetpub\wwwroot\Timesheet\App_Data\Template.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.
I have tried the work around as:-
- Folder and the file exist, giving access to the IUSR_### (IIS user) and to the ASPNET user in the folder where the file is.
- At Component Services(DCOM) given access to appropriate user.
I have already given all permission's to the folder where the template(.xlsx) exists
Any suggestions??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
试试这个:
(适用于 64 位 Windows 计算机上的 32 位版本的 Excel/Office)或
(适用于 32 位 Windows 计算机上的 32 位版本的 Office 或 64 位 Windows 计算机上的 64 位版本的 Office)。
相关用户(例如在 Win7 和 IIS 7 中,DefaultAppPool 为用户设置权限 IIS AppPool\DefaultAppPool)。
带有答案的原始帖子:
Try this:
(for the 32-bit version of Excel/Office on a 64-bit Windows computer) or
(for a 32-bit version of Office on a 32-bit Windows computer or a 64-bit version of Office on a 64-bit Windows computer).
relevant user (for example in Win7 & IIS 7 & DefaultAppPool set permissions for user IIS AppPool\DefaultAppPool).
Original post with answer:
让我注意,在我的位置添加 c:\windows\syswow64\config\systemprofile\desktop
目录不起作用。
重点是WOW64代表Windows on Windows64,这意味着它实际上适用于在64位操作系统上运行的32位程序。
由于我安装了 64 位 Excel,因此正确的目录是
c:\windows\system32\config\systemprofile\desktop
Let me note that in my place, adding the c:\windows\syswow64\config\systemprofile\desktop
directory didn't work.
The point is that WOW64 stands for Windows on Windows64, that means it actually applies for 32-bit programs running on the 64bit OS.
Since I have 64-bit Excel installed, the proper directory turned out to be the
c:\windows\system32\config\systemprofile\desktop
就我而言,我遵循了提供的建议 此处 并解决了问题。
步骤:
dcomcnfg
\Component Services\Computers\My Computer\DCOM Config\Microsoft Excel Application
在步骤 2 中,如果您找不到该路径,请尝试运行
mmc comexp.msc /32
而不是dcomcnfg。
In my case, I followed the suggestions provided here and solved the problem.
Steps:
dcomcnfg
\Component Services\Computers\My Computer\DCOM Config\Microsoft Excel Application
In step 2, if you can't locate that path then try running
mmc comexp.msc /32
instead ofdcomcnfg
.我想在 Eric Bonnot 的回答中添加一些内容:
当我使用 powerpoint PIA 与非管理员用户一起使用 IIS 服务器时,答案部分得到了解决。
我注意到,如果 pptx 文件中有媒体(例如图片),我将无法打开该文件。
“黑客”还为 Windows 用户(使用 PIA 的用户)添加了 systemprofile/AppData 目录的权限。
希望这有帮助
I would like to add something to the Eric Bonnot's answer :
The answer worked out partially as I worked on a IIS server with a non-admin user with the powerpoint PIA.
I noticed that I could not open a pptx file if this one had a media (picture for example) in it.
The "hack" was to add also the rights to the windows user (the one using the PIA) on the systemprofile/AppData directories.
Hope this helps
以下解决方案对我有用。
创建目录
C:\Windows\SysWOW64\config\systemprofile\Desktop
The following solution worked for me.
Create the directory
C:\Windows\SysWOW64\config\systemprofile\Desktop
我之前尝试过这个问题然后我解决了。
解决方案:
我将完全权限授予特定文件夹(子文件夹和文件)并检查工作正常。
I was previously attempt this problem then i resolved.
Solution:
I put the full permission to particular folder(Sub folder and files) and checked working fine.
我已将 WCF 包装在 Windows 服务中。创建桌面文件夹确实在一台机器上为我解决了这个问题,但在另一台机器上却没有。
我的问题最终是,我的 Windows 服务没有在机器的活动用户下运行。到目前为止,将服务配置为在计算机上处于活动状态的用户下运行为我解决了这个问题。
服务的组合
才能让它为我工作。
本文引导我找到完整的解决方案:无法访问 Excel 文件
I've wrapped my WCF in a Windows Service. Creating the Desktop Folders did solve it for me on one machine, but not on another.
My problem in the end was, that my Windows Service did not run under an active User of the machine. Configuring the service to run under a user which is active on the machine solved this problem for me so far.
Only the combination of
got it working for me.
This article lead me to the full solution: Cannot access excel file
就我而言,给出的解决方案都不适合我(在 dcom 设置中更改用户、创建桌面文件夹并授予用户对其的完全访问权限等)。
我终于意识到我有 64 位版本的 excel。 卸载64位版本并安装32位版本的excel解决了我对这个错误的解决方案。这是使用 Windows 10 和 Microsoft Office Excel 2010 时的情况。
In my case, none of the solutions given worked for me (changing user in dcom settings, creating desktop folder and giving user full access to it, etc).
I finally realized I had a 64-bit version of excel. Uninstalling the 64-bit version and installing the 32-bit version of excel solved my solution to this error. This was with using Windows 10 and Microsoft Office Excel 2010.
我一直在四处寻找,以下两个解决方案都对我有用。
更改为交互用户
步骤:
运行 dcomcnfg
转到控制台根目录 \Component Services\Computers\My Computer\DCOM Config\Microsoft Excel Application
右键单击 Microsoft Excel 应用程序
选择属性
转到“身份”选项卡
选择交互式用户。
在第 2 步中,如果您找不到该路径,请尝试运行
mmc comexp.msc /32
而不是dcomcnfg
。创建目录 C:\Windows\SysWOW64\config\systemprofile\Desktop
I've been searching around and The following two solutions both worked for me.
Change to interactive user
Steps:
Run
dcomcnfg
Go to Console Root \Component Services\Computers\My Computer\DCOM Config\Microsoft Excel Application
Right click Microsoft Excel Application
Select Properties
Go to Identity tab
Select The interactive user.
In step 2, if you can't locate that path then try running
mmc comexp.msc /32
instead ofdcomcnfg
.Create the directory C:\Windows\SysWOW64\config\systemprofile\Desktop
这有效
这不会并给出与常规 excel 启动相同的错误
对于任何网络文件的所有 MS Office 2007 应用程序也会发生这种情况。本地文件访问没问题。
This works
This does not and gives the same error as the regular excel startup
This also occurs for all MS Office 2007 apps for ANY network file. Local file access is fine.