0

I had written a code to first get an SSRS report and write to excel and save in a folder on server , manipulate the same excel through code and then save it again and then render it in excel for download. The code was working fine but then suddenly I started getting the error:

The process cannot access the file 'G:\TestEnvironment\ TestSite_ForDeveloper\ Reports\10697696.xls' because it is being used by another process.

What could be the issue?

Following is the code used:

//Getting SSRS report

//code

//Saving SSRS report result in excel
try
{
    Microsoft.Office.Interop.Excel.Workbook workbook;
    Microsoft.Office.Interop.Excel.Worksheet NwSheet;

    appExl = new Microsoft.Office.Interop.Excel.Application();
    string serverPath = Server.MapPath(".");

    string filenameToLoad = serverPath + "\\Page1Reports\\" + Session["UserAccentureID"].ToString() + ".xls";

    FileStream fileStream = new FileStream(filenameToLoad, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
    fileStream.Write(result, 0, result.Length);
    fileStream.Close();

    //Manipulation of file

    //Done something

    workbook.Save();
    workbook.Close();
    appExl.Quit();

    System.IO.FileInfo file = new System.IO.FileInfo(filenameToLoad);

    //Rendering again to excel download
    if (file.Exists)
    {
        string lblrptname = "Page1SLScoreCardReport_" + ddlFiscalWeek.SelectedValue.ToString();
        Response.Clear();
        Response.ClearHeaders();
        Response.Charset = "";
        Response.ContentType = "Application/vnd.xls";
        Response.AddHeader("content-disposition", "attachment;filename=" + lblrptname + ".xls");
        Response.AddHeader("Cache-Control", "max-age=0");
        Response.WriteFile(file.FullName);
    }
}
catch (Exception ex)
{
    AlnErrorHandler.HandleError(ex);
}
finally
{ 
    if (appExl != null )
     appExl.Quit();
}
Jeroen
  • 60,696
  • 40
  • 206
  • 339
Richa
  • 407
  • 1
  • 10
  • 22

1 Answers1

0

maybe check Processes under Task Manager to see if any extra EXCEL.EXE processes are running? Excel will run in the background and hold on to your file if not exited properly and cause all kinds of confusion (at least it did for me ;) ).

Zack Kay
  • 454
  • 3
  • 8
  • Yes this is happening..but i later included release of all objects created (explicitly including cells) but still it is not helping.:( how should I free the excel..?i am quitting the excel as well in the code – Richa Jun 11 '13 at 12:55
  • 1
    Not sure what's causing the issue, but to quit, just right click on the entry in Task Manager and and press 'End Process'. – Zack Kay Jun 14 '13 at 13:35