我编写了一个代码,首先获取 SSRS 报告并写入 excel 并保存在服务器上的文件夹中,通过代码操作相同的 excel,然后再次保存,然后在 excel 中呈现以供下载。代码工作正常,但突然我开始收到错误:
该进程无法访问该文件
'G:\TestEnvironment\ TestSite_ForDeveloper\ Reports\10697696.xls'
,因为它正被另一个进程使用。
可能是什么问题?
以下是使用的代码:
//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();
}