0

When the user clicks a button in the webpage, i want to open an Excel file. But it is only working when i debug my code in VS2010. When i just open the webpage and click the button it returns with an error:

Microsoft Excel cannot access the file 'K:\report_SYY_per_month.xlsx'. 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. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code...

The code i am currently using is this (not final yet of course..):

protected void ReportButton_Click(object sender, EventArgs e)
        {
            string path = @"K:\report_SYY_per_month.xlsx";

        var excel = new Excel.Application();
        excel.Visible = true;
        //excel.UserControl = true;

        System.Globalization.CultureInfo CI = System.Threading.Thread.CurrentThread.CurrentCulture;

        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

        Excel.Workbooks wbooks = excel.Workbooks;


        Excel.Workbook wBook = wbooks.Open(path,
            0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
            true, false, 0, true, false, false);

        Excel.Sheets sheets = wBook.Worksheets;

        string currentSheet = "per_month";
        Excel.Worksheet excelWorksheet = (Excel.Worksheet)sheets.get_Item(currentSheet);

        Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1");



        wbooks.Close();
        excel.Quit();

        GC.Collect();
        GC.WaitForPendingFinalizers();

        System.Threading.Thread.CurrentThread.CurrentCulture = CI;
    }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
georgekar
  • 11
  • 2
  • 2
    You mean it doesn't work when running from IIS? Check the app pool user has permission to read the file - it's usually a low-permission service account by default without usual machine user permissions. Or run the app pool as a domain account with appropriate permissions. That said, Microsoft [recommend against using Excel automation from a server application](http://support.microsoft.com/kb/257757) - see some alternatives [here](http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp). – Rup Dec 11 '13 at 15:37
  • 1
    Another idea: the app pool user may not have the K drive mapped if it's a network share. Try a full UNC path, and again make sure it has permissions. – Rup Dec 11 '13 at 15:46
  • out of topic, but have you considered using [OpenXML SDK](http://msdn.microsoft.com/en-us/library/office/bb448854.aspx), edited by microsoft, instead of interop. Doing so prevents you from installing (and paying) office on the server. – tschmit007 Dec 11 '13 at 16:12
  • First of all, it is my First question here so thank you both for getting back at me so quickly.. The main idea for what i want to do is to open an Excel file that is preformatted, then add dynamically data to its cells from a database so that the user can then save it where he likes. With that said, to answer to both: 1) K is not mapped, is a physical drive, 2) having Office is not a problem, we already do, and 3) currently the file has full permissions for: NETWORK SERVICE, \Users, IIS_IUSRS, DefaultAppPool – georgekar Dec 11 '13 at 16:28
  • @Rup Yes, that is exactly what i mean, its running fine when on vs2010 debugger, but not from IIS.. – georgekar Dec 11 '13 at 16:46
  • In which case I still think it's permissions, because when running under the debugger it'll be running as your user account, not the IIS user. Can you open the file in your ASP.NET code without Excel using some other .NET file operations? If you set a new app pool running as your user account does it work in that? – Rup Dec 11 '13 at 17:40
  • @Rup Yes i think you are right.. Could you please tell me or send me a link, how can i add the Classic .NET AppPool to have permission to access the file or how can i make the AppPool use my pc account? I am using this for the first time and nothing from what i have tried has been working.. – georgekar Dec 12 '13 at 08:19
  • Sorry, I haven't done this for ages. Application Pools, right-click, advanced settings, identity? – Rup Dec 12 '13 at 10:22
  • I tried it, no luck.. i ve tried a lot of things after a lot of googling but i have not succeded yet.. Dont know if it is relative but when i run excel.exe /automation from cmd works fine – georgekar Dec 12 '13 at 12:10

0 Answers0