D
Denis Brkljacic
Hello,
Please, if enaybody could help a little bit... Namely, I need to build C#
ASP.NET Web that opens some Template.xls, write something to it and then
closes it (and releases the objects). Well, what actually happens is that I
simply manage to open the Excel file (somewhere in the filesystem on the
server), then I manage even to write something to it, but when I release all
the object, there's always EXCEL.EXE in task manager on the server...
Could please someone tell me what's wrong that causes EXCEL.EXE to persist
even after all the GC?
Here's some example:
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
Excel.Application objExcel = new Excel.Application();
Excel.Workbooks objWorkbooks = objExcel.Workbooks;
System.Reflection.Missing objMissingValue = System.Reflection.Missing.Value;
String strTemplateFile = "C:\\Test.xls";
Excel.Workbook objCurrentWorkbook = objWorkbooks.Open(strTemplateFile,
objMissingValue, objMissingValue, objMissingValue, objMissingValue,
objMissingValue, objMissingValue, objMissingValue, objMissingValue,
objMissingValue, objMissingValue, objMissingValue, objMissingValue,
objMissingValue, objMissingValue);
Excel.Sheets objExcelSheets = objCurrentWorkbook.Worksheets;
objExcel.UserControl=true;
objExcel.Visible=true;
string currentSheet = "Sheet1";
Excel.Worksheet objExcelCurrentWorksheet =
(Excel.Worksheet)objExcelSheets.get_Item(currentSheet);
Excel.Range objExcelRange =
(Excel.Range)objExcelCurrentWorksheet.get_Range("C4", "C4");
this.LabelCreateExcel.Text = ((string)objExcelRange.Value2);
objExcelRange.Value2 = "Blabla!";
objExcelCurrentWorksheet.Cells[1, 1] = "test sheet";
objExcelCurrentWorksheet.get_Range("A1", "A1").Font.Bold = true;
objExcelCurrentWorksheet.get_Range("A3", "F3").EntireColumn.AutoFit();
// GC ... I destroy everything but no success
(
objExcel.Quit();
Marshal.ReleaseComObject(objWorkbooks);
Marshal.ReleaseComObject(objCurrentWorkbook);
Marshal.ReleaseComObject(objExcelSheets);
Marshal.ReleaseComObject(objExcelCurrentWorksheet);
Marshal.ReleaseComObject(objExcelRange);
Marshal.ReleaseComObject(objExcel);
objWorkbooks = null;
objCurrentWorkbook = null;
objExcelSheets = null;
objExcelCurrentWorksheet = null;
objExcelRange = null;
objExcel = null;
GC.Collect();
Thank you,
/Denis
Please, if enaybody could help a little bit... Namely, I need to build C#
ASP.NET Web that opens some Template.xls, write something to it and then
closes it (and releases the objects). Well, what actually happens is that I
simply manage to open the Excel file (somewhere in the filesystem on the
server), then I manage even to write something to it, but when I release all
the object, there's always EXCEL.EXE in task manager on the server...
Could please someone tell me what's wrong that causes EXCEL.EXE to persist
even after all the GC?
Here's some example:
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
Excel.Application objExcel = new Excel.Application();
Excel.Workbooks objWorkbooks = objExcel.Workbooks;
System.Reflection.Missing objMissingValue = System.Reflection.Missing.Value;
String strTemplateFile = "C:\\Test.xls";
Excel.Workbook objCurrentWorkbook = objWorkbooks.Open(strTemplateFile,
objMissingValue, objMissingValue, objMissingValue, objMissingValue,
objMissingValue, objMissingValue, objMissingValue, objMissingValue,
objMissingValue, objMissingValue, objMissingValue, objMissingValue,
objMissingValue, objMissingValue);
Excel.Sheets objExcelSheets = objCurrentWorkbook.Worksheets;
objExcel.UserControl=true;
objExcel.Visible=true;
string currentSheet = "Sheet1";
Excel.Worksheet objExcelCurrentWorksheet =
(Excel.Worksheet)objExcelSheets.get_Item(currentSheet);
Excel.Range objExcelRange =
(Excel.Range)objExcelCurrentWorksheet.get_Range("C4", "C4");
this.LabelCreateExcel.Text = ((string)objExcelRange.Value2);
objExcelRange.Value2 = "Blabla!";
objExcelCurrentWorksheet.Cells[1, 1] = "test sheet";
objExcelCurrentWorksheet.get_Range("A1", "A1").Font.Bold = true;
objExcelCurrentWorksheet.get_Range("A3", "F3").EntireColumn.AutoFit();
// GC ... I destroy everything but no success

objExcel.Quit();
Marshal.ReleaseComObject(objWorkbooks);
Marshal.ReleaseComObject(objCurrentWorkbook);
Marshal.ReleaseComObject(objExcelSheets);
Marshal.ReleaseComObject(objExcelCurrentWorksheet);
Marshal.ReleaseComObject(objExcelRange);
Marshal.ReleaseComObject(objExcel);
objWorkbooks = null;
objCurrentWorkbook = null;
objExcelSheets = null;
objExcelCurrentWorksheet = null;
objExcelRange = null;
objExcel = null;
GC.Collect();
Thank you,
/Denis