我的代码如下:-
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 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.
我已经尝试过解决方法:-
- 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.
我已经授予模板(.xlsx)所在文件夹的所有权限
有什么建议吗??
最佳答案
试试这个:
- 创建目录
C:\Windows\SysWOW64\config\systemprofile\Desktop
(对于 64 位 Windows 计算机上的 32 位版本的 Excel/Office)或
C:\Windows\System32\config\systemprofile\Desktop
(对于 32 位 Windows 计算机上的 32 位版本的 Office 或 64 位 Windows 计算机上的 64 位版本的 Office)。
- 对于桌面目录,添加完全控制权限 相关用户(例如在 Win7 & IIS 7 & DefaultAppPool 中为用户 IIS AppPool\DefaultAppPool 设置权限)。
带答案的原帖:
关于c# - Microsoft Office Excel 无法访问文件 'c:\inetpub\wwwroot\Timesheet\App_Data\Template.xlsx',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7106381/