0

我编写了一个代码,首先获取 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();
}
4

1 回答 1

0

也许检查任务管理器下的进程以查看是否有任何额外的 EXCEL.EXE 进程正在运行?如果没有正确退出,Excel 将在后台运行并保留您的文件并导致各种混乱(至少它对我来说是这样;))。

于 2013-06-10T17:32:54.077 回答