2012-10-09 67 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。