c# - Microsoft Office Excel 无法访问文件 'c:\inetpub\wwwroot\Timesheet\App_Data\Template.xlsx'

标签 c# excel export-to-excel excel-interop

我的代码如下:-

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)所在文件夹的所有权限

有什么建议吗??

最佳答案

试试这个:

  1. 创建目录

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)。

  1. 对于桌面目录,添加完全控制权限 相关用户(例如在 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/

相关文章:

python - 防止 csvkit 修改日期/时间?

asp.net - 从 ASP.NET 导出到 Excel 会产生无法使用 Excel Viewer 读取的文件

c# - 渲染局部 View 时如何调用 Controller Action ?

c# - 在 ASP.NET MVC 中创建模拟参数化查询的模型/ View / Controller ?

sql - VBA (Excel) 与 SQL - 比较两行中的值(循环)

VBA AddDataField CubeFields - 错误 1004

export-to-excel - 如何自动下载 Google 文档电子表格的备份?

c# - 从 asp.net 导出数据到 excel 时添加自定义文本

c# - 如何从这个陌生的域中获取 Cookie?

c# - VS2013 - 如何使用外部程序在 C# 项目的命令行参数中传递解决方案文件夹 $(SolutionDir)