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();
}