Search code examples
c#asp.netasp.net-mvcexcelserver.mappath

How to use Server.MapPath for Excel File


Using Asp.Net MVC.

I have one Excel file which has default styles, borders etc and it's name is MyExcel.xlsx. It opens "C:\Users\myusername\Documents" from my local.

Here is my C# code for it:

            Application excel = new Application();
            excel.Visible = true;
            Workbook wb = excel.Workbooks.Open("MyExcel.xlsx");
            Worksheet sh = wb.Sheets[1];

I uploaded my file to my Server 2012 like C:\MyExcel.xlsx. So i want to open server side MyExcel.xlsx file.

Server.MapPath("C:\MyExcel.xlsx");

But i get "Unexpected M" error. How can i fix this code?

>         string myServerPath = Server.MapPath("C:\MyExcel.xlsx");
>         Application excel = new Application();
>         excel.Visible = true;
>         Workbook wb = excel.Workbooks.Open(myServerPath);
>         Worksheet sh = wb.Sheets[1];

Solution

  • Try putting it like this:

    Server.MapPath(@"C:\MyExcel.xlsx");