|
-
April 15th, 2009, 06:52 AM
#1
Excel process remains in task manager when using OleDB
Hi everyone,
I'm working on a project where i need to check the values within an existing excel file.
The problem is the the Excel.exe process is still active in task manager even after I release all the appropriate COM objects I've created. I've searched a lot for this issue and it seems that
da.Fill(dt) is the problem.
Here is the code.
Any suggestions??
Code:
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dlg1.FileName + ";Extended Properties=\"Excel 8.0;HDR=YES\"");
dt = new DataTable();
ExcApp = new Excel.Application();
ExcApp.DisplayAlerts = false;
Excel.Workbooks _Wb = ExcApp.Workbooks;
wb = _Wb.Open(dlg1.FileName, 0, false, 5,
"", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
0, false, false, false);
Excel.Sheets _Sheet = wb.Worksheets;
Excel.Worksheet _WSheet = (Excel.Worksheet)_Sheet[1];// = (Excel.Worksheet)wb.Worksheets[1];//xcel.Worksheet(wb.WorkSheets("Sheet1"));
Excel.Range _range = _WSheet.UsedRange;
string name = _WSheet.Name;
// _range.Tab
cmd = string.Format("Select * from [{0}$]", name);
// OleDbCommand c = new OleDbCommand(cmd, conn);
da = new OleDbDataAdapter(cmd, conn);
da.Fill(dt);
//do something with the datatable
dlg1.FileName = dlg1.FileName + ".txt";
wb.Save();
wb.SaveAs(dlg1.FileName, Excel.XlFileFormat.xlUnicodeText, "", "", true, false, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlOtherSessionChanges,
false, false, Type.Missing, Type.Missing);
wb.Save();
ExcApp.DisplayAlerts = true;
#region Dispose region
dt.Dispose();
dt = null;
cmd = null;
conn.ConnectionString = null;
conn.Dispose();
conn = null;
da.SelectCommand.Dispose();
da.Dispose();
da = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(_range);
System.Runtime.InteropServices.Marshal.ReleaseComObject(_Sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(_WSheet);
wb.Close(true, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
_Wb.Close();
ExcApp.Workbooks.Close();
ExcApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(_Wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcApp.Workbooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcApp);
_range = null;
_Sheet = null;
_WSheet = null;
wb = null;
_Wb = null;
ExcApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|