Cannot Write to Excel File RRS feed

  • Question

  • I don't get it!  It works just fine with C# and Winforms!  What is so special about MVC?

    An exception of type 'System.Runtime.InteropServices.COMException' occurred in Movies.dll but was not handled in user code

    Additional information: The file could not be accessed. Try one of the following:

    • Make sure the specified folder exists. 

    • Make sure the folder that contains the file is not read-only.

    • Make sure the file name does not contain any of the following characters:  <  >  ?  [  ]  :  | or  *

    • Make sure the file/path name doesn't contain more than 218 characters.


                oXL = new Microsoft.Office.Interop.Excel.Application();
                //oWB = oXL.Workbooks.Add(Type.Missing);

                // This doesn't work either

                //oWB = oXL.Workbooks.Open(System.Web.Configuration.WebConfigurationManager.AppSettings["ExcelFile"]);
                //oWS = (_Worksheet)oWB.ActiveSheet;
                oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(""));
                oWS = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;
                oWS.EnableCalculation = false;
                oWS.Cells[1, 1] = "Movie";
                oWS.Cells[1, 2] = "Actors";
                oWS.Cells[1, 3] = "Crew";
                oWS.Cells[1, 4] = "Description";
                oWS.Cells[1, 5] = "Category";

                //bookmark -- Write results to Excel
                foreach (var lst in lstMovies)
                    oWS.Cells[intRow, 1] = ("" + lst.Item1).ToString();
                    oWS.Cells[intRow, 2] = ("" + lst.Item2).ToString();
                    oWS.Cells[intRow, 3] = ("" + lst.Item3).ToString();
                    oWS.Cells[intRow, 4] = ("" + lst.Item4).ToString();
                    oWS.Cells[intRow++, 5] = ("" + lst.Item5).ToString();



    It blows up on the SaveAs statement above yet I am running as an administrator 

    System.Web.Configuration.WebConfigurationManager.AppSettings["ExcelFile"] is 

        <add key="webpages:Version" value="" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
        <add key="ExcelFile" value="C:\\Movies\\Movies.xlsx" />

    Tuesday, January 29, 2019 12:27 AM

All replies

  • Hello,

    I would advise moving away from Excel automation for web solutions, instead use either Open XML for Excel or a open source library like SpreadSheetLight or EPPlus. My choice would be SpreadSheetLight.

    All three all not COM but at the core Open XML while OpenXML raw requires a fair amount of knowledge while SpreadSheetLight and EPPlus require very little knowledge.

    Simple example for SpreadSheetLight. Pass an existing Excel file name and sheet. You can also create a new Excel file also. See the documentation on the site.

    private void WriteDemo(string pFileName, string pSheetName)
        using (var doc = new SLDocument(pFileName, pSheetName))
            doc.SetCellValue(1, 1, "Karen");
            doc.SetCellValue(1, 2, true);
            doc.SetCellValue(1, 3, DateTime.Now);
            doc.SetCellValue(1, 4, 100.5M);

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, January 29, 2019 1:29 AM
  • Are you running this on a web server?  Are you sure the folder C:\Movies exists on that server?

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Tuesday, January 29, 2019 7:11 AM
  • Hi OldeEnglishD,

    Since your question is more related to MVC, you could post a new thread in MVC forum for suitable support.


    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 29, 2019 7:26 AM
  • Office automation is not recommended to be used on server side. You'd better use OpenXML or other 3rd party libraries instead. Maybe you can take a look at Free Spire.XLS for .NET, which is a totally free excel library for commercial and personal use, very easy to use but with some limitations for .xls (excel97-2003) format. Visit this link for more information.
    Wednesday, January 30, 2019 3:05 AM