Error when try read data from Excel File in ASP.NET MVC

mostafa ahmed 41 Reputation points
2023-10-03T17:37:39.96+00:00

In an ASP.NET MVC app, I am trying to read data from Excel file but I get this error:

The error appears in production mode, but in development mode there's no problem.

I am making host on Local Host on my computer

Microsoft Excel cannot access the file 'C:\Users\PC\Desktop\qqq\Content\Uploads\dataFile.xls'.

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.

Internet Information Services
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,520 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,053 questions
{count} votes

Accepted answer
  1. XuDong Peng-MSFT 10,761 Reputation points Microsoft Vendor
    2023-10-04T06:22:51.3266667+00:00

    Hi @mostafa ahmed,

    In production mode I mean the publish where I host the site on IIS local server

    I created a simple example and reproduced your problem. Please check if the following directories exist:

    1. 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
    C:\Windows\System32\config\systemprofile\Desktop
    
    1. For the 32-bit version of Excel/Office on a 64-bit Windows computer:
    C:\Windows\SysWOW64\config\systemprofile\Desktop
    

    In most cases (my test too), you should be able to solve this problem by creating the first directory, unless your environment is somewhat special, you can try creating a second directory to see if it works for you.

    In addition, I found that you posted the same post in the Stackoverflow forum. If they are all posted by you, I will continue to follow up this issue in Q&A, thank you for your understanding.

    Best regards,

    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Olaf Helper 45,126 Reputation points
    2023-10-04T06:36:44.6066667+00:00

    'C:\Users\PC\Desktop\qqq\Content\Uploads\dataFile.xls'.

    You placed the file in your user profile folder, where only you have access permissions for, but not IIS service account.

    Locate the file in a "public" folder and grant permissions for IIS.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.