Click here to Skip to main content
15,889,861 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
My code is
C#
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            
            string path = FileName;
            oXL = new Microsoft.Office.Interop.Excel.Application();
            oXL.Visible = true;
            oXL.DisplayAlerts = false;
            Workbooks workbooks = oXL.Workbooks;
            mWorkBook = workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);


What I have tried:

I am getting error, when I try to open Excel file, Its showing error -->
Sorry, we couldn't find Book1.xlsx. Is it possible it was moved, renamed or deleted?
Posted
Updated 22-Apr-16 2:43am
Comments
CHill60 22-Apr-16 8:38am    
Have a look at the contents of FileName - does it contain the full path to Book1.xlsx? Does the file exist on the server? Is it a relative path on the server? Does the account under which the website is running have access to the folder?

1 solution

Oh boy. Where to start?

First of all, the FileName property of the PostedFile object is just the name of the file on the client. Your code is executing on the server. You can't just open that path from the server, because it doesn't exist.

If you want to do something with the file on the server, you either need to save it to the server with the SaveAs method, or read the InputStream to access the raw file bytes.

Secondly, your code is executing on the server. If you open an instance of Excel, it will open on the server, where nobody will ever see it. And that's assuming you have Excel installed on the server, and have jumped through all the hoops documented in this Microsoft Knowledgebase article:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.


You need to go back to basics and get an understanding of how websites work, and the difference between the client and the server.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900