2012-10-09 103 views
2

波紋管功能用於甲酸鹽Excel文件,但在運行後的功能應用的Excel不會從嘗試。(不能殺的應用程序)closeing請幫我在這格式的Excel文件C#

private void FormateExcelFile() 
{ 
    try 
    { 
     int nI = 0;//For Loop 
     string nFieldName = string.Empty; 
     nUserName= WindowsIdentity.GetCurrent().Name; //Get Windows Login User 
     string reportFilenPath = Application.StartupPath + "\\OutPutFiles\\" + "NewTempFile.xls"; 
     string connString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + reportFilenPath + "';Extended Properties=Excel 8.0;"; 
     DataTable parts = new DataTable(); 
     using (OleDbConnection conn = new OleDbConnection(connString)) 
     { 
      string sqlParts = "Select * from [" + nSheetName + "]"; 
      OleDbDataAdapter adapter = new OleDbDataAdapter(sqlParts, conn); 
      adapter.Fill(parts); 
     } 

     for (nI = 0; nI < parts.Columns.Count; nI++) 
     { 
      DataColumn column = parts.Columns[nI]; 
      if (nI == 0) { nFieldName = column.ColumnName; } 
      else { nFieldName = nFieldName + "," + column.ColumnName; } 
     } 
     parts.Dispose(); parts = null; 

     oExcel = new Excel.Application(); 
     oBook = oExcel.Workbooks.Open(reportFilenPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); 
     oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.get_Item(nSheetName.Replace("$", "")); 
     oExcel.DisplayAlerts = false; 
     oExcel.Visible = true; 



     //Check the Field Is Avilable in the Sheet if not then Add 
     if (nFieldName.Contains("Sub Device") == false) 
     { 
      nRng = oSheet.get_Range("A1", oMissing); 
      nRng.EntireColumn.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, false); 
      oSheet.Cells[1, 1] = "Sub Device"; 
     } 
     if (nFieldName.Contains("Brand") == false) 
     { 
      nRng = oSheet.get_Range("A1", oMissing); 
      nRng.EntireColumn.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, false); 
      oSheet.Cells[1, 1] = "Brand"; 
     } 
     if (nFieldName.Contains("Model") == false) 
     { 
      nRng = oSheet.get_Range("A1", oMissing); 
      nRng.EntireColumn.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, false); 
      oSheet.Cells[1, 1] = "Model"; 
     } 
     if (nFieldName.Contains("Product Details") == false) 
     { 
      nRng = oSheet.get_Range("A1", oMissing); 
      nRng.EntireColumn.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, false); 
      oSheet.Cells[1, 1] = "Product Details"; 
     } 
     if (nFieldName.Contains("Price") == false) 
     { 
      nRng = (Excel.Range)oSheet.Cells[1, 1]; 
      //nRng = oSheet.get_Range("A1", oMissing); 
      nRng.EntireColumn.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, false); 
      oSheet.Cells[1, 1] = "Price"; 
     } 

     oBook.Save(); 
     oBook.Close(false, oMissing, oMissing); 
     oExcel.DisplayAlerts = true; 
     releaseObject(oSheet); 
     releaseObject(oBook); 
     oExcel.Quit(); 
     releaseObject(oExcel); 
     releaseObject(nRng); 
     nRng = null; 
     oExcel = null; 
     oSheet = null; 


    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
     releaseObject(oSheet); 
     releaseObject(oBook); 
     //oExcel.Quit(); 
     releaseObject(oExcel); 
    } 
} 

private void releaseObject(object obj) 
{ 
    try 
    { 
     System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; 
    } 
    catch (Exception ex) 
    { 
     obj = null; 
     MessageBox.Show("Exception Occured while releasing object " + ex.ToString()); 
    } 
    finally 
    { 
     GC.Collect(); 
    } 
} 
+0

確保釋放所有使用的對象。 – artsylar

+0

我做到了RNG,簿,工作表和Excel中的所有對象,但仍然在應用程序打開 – user1632718

+0

您可以發佈您'releaseObject'方法? – JMK

回答

0

嘗試使用這些命令來結束您的Excel任務:

 Marshal.FinalReleaseComObject(sheet); 
     app.DisplayAlerts = false; //Very important! 
     range = null; 
     sheet = null; 
     // Garbage collecting 
     GC.Collect(); 
     GC.WaitForPendingFinalizers(); 
     book.Close(false, Missing.Value, Missing.Value); 
     Marshal.FinalReleaseComObject(book); 
     book = null; 
     app.Quit(); 
     Marshal.FinalReleaseComObject(app); 
     app = null; 

我有我的訪問Excel文件同樣的問題,但隨着代碼顯示它會關閉所有的時間。

當然,如果程序在到達代碼之前崩潰,請使用調試器檢查您的代碼。

在你的情況:書 - > oBook,應用程序 - > oExcel,表 - > oSheet。

+0

我不認爲調用'GC.Collect();'這裏是正確的行爲。 – JMK

+0

我也這麼認爲。 但經過數百萬次試圖結束Excel任務,我嘗試了GC.Collect和Marshal.FinalReleaseComObject,它工作。 這有可能是GC.Collect的是無用和Excel任務的擊殺通過Marshal.FinalReleaseComeObject法完成。 – jAC

+0

我寫了相同的代碼,但仍然沒有關閉應用程序。 – user1632718

0

拿走這些行:

nRng = null; 
oExcel = null; 
oSheet = null; 

我認爲你正在釋放您的Excel對象,然後通過使其等於空之後你正在做的與Excel一些東西,你的機器上啓動一個新的實例。

釋放對象後,您不需要將變量設置爲null,或者運行GC.Collect();,垃圾收集器將爲您處理此問題,並且我認爲在此實例中嘗試自行清理管理對象(正確清理非託管Excel對象後)實際上是造成您的問題。

0

我有和Excel互操作一樣的問題。這個問題應該通過這種線的(至少這是我的情況)是起源:

oBook = oExcel.Workbooks.Open(reportFilenPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); 

在你實在oBook結束,但是這是真的發生在這裏:爲了獲得oBook指向你打開工作簿,您訪問了Workbooks對象(使用oExcel.Workbooks)。這從來沒有發佈,並保持Excel不退出。

Microsoft.Interop.Excel.Workbooks oBooks = oExcel.Workbooks; 
oBook = oBooks.Open(reportFilenPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); 
releaseObject(oBooks); 

當然,這有這種(例如oBook.Worksheets.get_Item(...)nRng.EntireColumn.Insert(...),等等)的每一行做:

我通過改寫該行的解決了這個問題。

1

您需要發佈全部您引用的Excel對象。例如:

if (nFieldName.Contains("Sub Device") == false) 
{ 
    nRng = oSheet.get_Range("A1", oMissing); 
    nRng.EntireColumn.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, false); 
    oSheet.Cells[1, 1] = "Sub Device"; 
} 

應該是這樣的(離開了嘗試/終於爲簡單起見)

if (nFieldName.Contains("Sub Device") == false) 
{ 
    nRng = oSheet.get_Range("A1", oMissing); 
    var col = nRng.EntireColumn 
    col.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, false); 
    var cells = oSheet.Cells; 
    var firstCell = cells[1,1]; 
    firstCell.Value = "Sub Device"; 

    Marshal.ReleaseComObject(nRng); 
    Marshal.ReleaseComObject(col); 
    Marshal.ReleaseComObject(cells); 
    Marshal.ReleaseComObject(firstCell); 

} 

同理:

oBook = oExcel.Workbooks.Open(reportFilenPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); 
oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.get_Item(nSheetName.Replace("$", "")); 

需求是:

oBooks = oExcel.Workbooks 
oBook = oBooks.Open(...); 
oSheets = oBook.Worksheets 
oSheet = oSHeets.get_Item(...); 

你需要釋放oBooks和oS heets。