Excel アプリケーション ワークブックやワークシートなどの Excel COM オブジェクトを適切に閉じようとしています。このサイトと他のサイトのすべてのソリューションを閲覧しました。何も機能しません。唯一の方法は、ループを実行してプロセスを強制終了またはエクセルすることです。単一のExcelファイルプロセスのみを強制終了するより良い方法または方法はありますか?
#region VARIABLE
int nColumn = 0;
int nIMPORT = 1;
int endofsheet = 0;
string IMPORTfilepath = null;
string nsheet = null;
string nCell = null;
string lastfilename = "";
double rRFDS = 0;
double cRFDS = 0;
List<String> filename = new List<String>();
object misValue1 = System.Reflection.Missing.Value;
//Opening Central DB
Excel.Application CentralDB;
Excel.Workbook CentralDBWorkBook;
Excel.Worksheet CentralDBWorkSheet;
Excel.Range CentralDBrange;
//Opening RFDS
Excel.Application IMPORT;
Excel.Workbook IMPORTWorkBook;
Excel.Worksheet IMPORTWorkSheet;
//Excel.Range RFDSrange;
Excel.Range CellAddress;
#endregion
CentralDB = new Microsoft.Office.Interop.Excel.Application();
CentralDBWorkBook = CentralDB.Workbooks.Open(Odl1.FileName, 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
CentralDBWorkSheet = CentralDB.Worksheets.get_Item(1);
IMPORT = new Microsoft.Office.Interop.Excel.Application();
IMPORTfilepath = Odl2.FileNames[nIMPORT];
IMPORTWorkBook = IMPORT.Workbooks.Open(IMPORTfilepath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
IMPORTWorkSheet = IMPORT.Worksheets.get_Item(1);
CentralDBrange = CentralDBWorkSheet.UsedRange;
filename = Odl2.FileNames.ToList();
endofsheet = CentralDBrange.Rows.Count;
foreach(string fn in filename)
{
try
{
IMPORTWorkBook = IMPORT.Workbooks.Open(fn, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
CentralDBWorkSheet.Cells[nIMPORT + endofsheet, 1] = IMPORTWorkBook.Name;
for (nColumn = 3; nColumn <= CentralDBrange.Columns.Count; nColumn++)
{
nsheet = (string)(CentralDBrange.Cells[2, nColumn].Value2);
foreach (Excel.Worksheet ws in IMPORTWorkBook.Sheets)
{
if (ws.Name.Equals(nsheet))
{
IMPORTWorkSheet = IMPORTWorkBook.Worksheets.get_Item(nsheet);
nCell = (string)(CentralDBrange.Cells[3, nColumn].Value2);
CellAddress = CentralDBWorkSheet.get_Range(nCell, nCell);
rRFDS = CellAddress.Row;
cRFDS = CellAddress.Column;
CentralDBWorkSheet.Cells[nIMPORT + endofsheet, nColumn] = IMPORTWorkSheet.Cells[rRFDS, cRFDS];
label4.Text = nIMPORT.ToString() + "/" + Convert.ToString(Odl2.FileNames.Count());
}
}
}
nIMPORT++;
nColumn = 3;
CentralDBWorkBook.Save();
IMPORTWorkBook.Close(false, null, null);
}
catch (System.Exception ex)
{
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
lastfilename = fn;
}
CentralDB.Quit();
releaseObject(CentralDBWorkSheet);
releaseObject(CentralDBWorkBook);
releaseObject(CentralDB);
IMPORT.Quit();
releaseObject(IMPORTWorkSheet);
releaseObject(IMPORTWorkBook);
releaseObject(IMPORT);
GC.Collect();
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (System.Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}