|
分類:[C#]
下記コードにてexcelがタスクマネージャーに残ってしまいます。
どこで解放漏れが起きているのか調べる方法はありますか。
//Excelオブジェクトの変数宣言
Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Excel.Workbooks books = null;
Microsoft.Office.Interop.Excel.Workbook book = null;
Microsoft.Office.Interop.Excel.Sheets sheets = null;
Microsoft.Office.Interop.Excel.Worksheet sheet = null;
Microsoft.Office.Interop.Excel.PageSetup page = null;
try
{
excel = new Microsoft.Office.Interop.Excel.Application();
excel.DisplayAlerts = false; // アラートを表示しない
books = excel.Workbooks;
filepath = dataGridView1.Rows[i].Cells["filepath"].Value.ToString();
filename = dataGridView1.Rows[i].Cells["filename"].Value.ToString();
footer = filename.Substring(0, filename.IndexOf("_"));
footer = footer + Path.GetExtension(filepath);
book = books.Open(filepath,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);
sname = dataGridView1.Rows[i].Cells["sheetname"].Value.ToString();
string[] ar1 = sname.Split(';');
sheets = book.Worksheets[ar1];
sheets.Select(Type.Missing);
if (FooterCheckBox.Checked == true)
{
foreach (string s in ar1)
{
sheet = sheets[s];
page = sheet.PageSetup;
page.LeftFooter = "&L" + footer;
}
}
//印刷処理
sheets.PrintOutEx(Type.Missing, Type.Missing, Type.Missing,
false, ptintername, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//Excelを閉じる
book.Close(false, Type.Missing, Type.Missing);
//COMオブジェクト解放
FinalReleaseComObject(page);
FinalReleaseComObject(sheet);
FinalReleaseComObject(sheets);
FinalReleaseComObject(book);
FinalReleaseComObject(books);
// Excelを終了する
excel.Quit();
FinalReleaseComObject(excel);
GC.Collect();
}
|