我编写了一个代码,首先获取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();
}
答案 0 :(得分:0)
也许检查任务管理器下的进程,看看是否有任何额外的EXCEL.EXE进程正在运行? Excel将在后台运行并保留您的文件,如果没有正确退出并导致各种混淆(至少它对我来说;)。)