小编典典

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

c#

我的代码如下:-

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)处授予了适当用户的访问权限。

我已经授予了template(.xlsx)所在文件夹的所有权限

有什么建议么??


阅读 361

收藏
2020-05-19

共1个答案

小编典典

尝试这个:

  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. 对于 Desktop 目录,为相关用户添加“ 完全控制” 权限(例如,在Win7&IIS 7和DefaultAppPool为用户 IIS AppPool \ DefaultAppPool 设置的权限中)。

原始帖子的答案:

2020-05-19