2017-08-04 20 views
0

Excelのアプリケーションを開いて範囲をダンプして閉じようとするコードセクションがあります。c#Interop Excel Close instances

私が何をするにしても、実用的な言葉をしようとすると、私はエクセルインスタンスをシャットダウンできません - 私が知っている限り、1ドットは2ドット規則に従います。

Excel.Application excelApp = new Excel.Application(); 

    Excel.Workbooks wbks = excelApp.Workbooks; 
    Excel.Workbook currentWorkbook = wbks.Add(); 
    Excel.Worksheet newWks = new Excel.Worksheet(); 

    try 
    { 
     newWks = currentWorkbook.Sheets["Sheet2"]; 
     newWks.Delete(); 
     newWks = currentWorkbook.Sheets["Sheet3"]; 
     newWks.Delete(); 
     newWks=currentWorkbook.Sheets["Sheet1"]; 

     for (int col = 0; col < dt.Columns.Count; col++) 
     { 
      newWks.Cells[1, col + 1] = dt.Columns[col].ColumnName; 
     } 
     string[,] arr = new string[dt.Rows.Count, dt.Columns.Count]; 

     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      for (int j = 0; j < dt.Columns.Count; j++) 
      { 
       arr[i, j] = dt.Rows[i][j].ToString(); 
      } 
     } 

     Excel.Range startCell = newWks.Cells[2, 1]; 
     Excel.Range endCell = newWks.Cells[dt.Rows.Count + 1, dt.Columns.Count]; 
     Excel.Range writeRange = newWks.Range[startCell, endCell]; 
     writeRange.Value2 = arr; 

     Excel.Application cwa = currentWorkbook.Application; 
     cwa.DisplayAlerts = false; 

     Global.CheckDirectory(path); 

     currentWorkbook.SaveAs(path + @"//filepath", Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV, Missing.Value, 
      Missing.Value, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, 
      Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlUserResolution, true, 
      Missing.Value, Missing.Value, Missing.Value); 

     cwa.Quit(); 
     Marshal.ReleaseComObject(cwa); 
     Marshal.ReleaseComObject(startCell); 
     Marshal.ReleaseComObject(endCell); 
     Marshal.ReleaseComObject(writeRange); 
    } 
    catch (Exception ex) 
    { 
     //err handler 
    } 
    finally 
    { 

     wbks.Close(); 
     excelApp.Quit(); 

     Marshal.ReleaseComObject(newWks); 
     Marshal.ReleaseComObject(currentWorkbook); 
     Marshal.ReleaseComObject(wbks); 
     Marshal.ReleaseComObject(excelApp); 

     GC.Collect(); 
     GC.WaitForPendingFinalizers(); 
     GC.Collect(); 
     GC.WaitForPendingFinalizers(); 
    } 
+0

参照[ここ](https://stackoverflow.com/questions/158706/how-do-i-properly-clean-up-excel-interop-objects)と[ここ](https://で25134024/clean-up-excel-interop-objects-with-idisposable/25135685#25135685)。 – Equalsk

+0

ExcelアプリケーションexcelAppのインスタンスがすでに1つあります。 Excel.Application cwa = currentWorkbook.Application;を設定する理由はありますか?ちょうどexcelApp.Quit();最後に仕事をして、ちょうどexcelAppを保つべきです:excelApp.DisplayAlerts = false; DisplayAlertsはExcelブックにはないアプリケーションに属します。 –

答えて

0

こんにちは、

この順序ですべてを閉じてリリースするようにしてください。

Marshal.ReleaseComObject(Range); 
Marshal.ReleaseComObject(Worksheet); 
//WRITE close and release 
WWorkbook.Close(); 
Marshal.ReleaseComObject(Workbook); 
//!WRITE quit and release 
App.Quit(); 
Marshal.ReleaseComObject(App); 
//cleanup 
GC.Collect(); 
GC.WaitForPendingFinalizers();