私はこの問題に数日間苦労してきました.researchを行い、さまざまなフォーラムで見つけたすべての提案を適用しましたが、まだ解決できません.
私の問題は、相互運用ライブラリを使用したExcelにあります。テンプレートとしてExcelファイルを使用しているため、それを開いて新しい名前で新しい場所に保存しています。ファイルが作成されて閉じられた後も Excel プロセスが実行され続けることを除いて、すべてがうまく機能します。
これは私のコードです
protected string CreateExcel(string strProjectID, string strFileMapPath)
{
string strCurrentDir = HttpContext.Current.Server.MapPath("~/Reports/Templates/");
string strFile = "Not_Created";
Application oXL;
Workbook oWB;
oXL = new Application();
oXL.Visible = false;
Workbooks wbks = oXL.Workbooks;
//opening template file
oWB = wbks.Open(strFileMapPath);
oXL.Visible = false;
oXL.UserControl = false;
strFile = strProjectID + "_" + DateTime.Now.Ticks.ToString() + ".xlsx";
//Saving file with new name
oWB.SaveAs(strCurrentDir + strFile, XlFileFormat.xlWorkbookDefault, null, null, false, false, XlSaveAsAccessMode.xlExclusive, false, false, null, null);
oWB.Close(false, strCurrentDir + strFile, Type.Missing);
wbks.Close();
oXL.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
oWB = null;
oXL = null;
wbks = null;
GC.Collect();
return strFile;
}
ご覧のとおり、すべてのオブジェクトを閉じて解放していますが、アプリケーションは終了しません。
私は、Windows Server 2008 (運用) と Windows 7 (開発) の両方で、IIS7 を使用して 32 ビットでテストしています。