C#, ASP.NET, MS Excel

  • Thread starter Denis Brkljacic
  • Start date
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
 
D

Denis Brkljacic

Thanks!

But, I wouldn't say that only Excel is up (and doc itself closed), because
after I quit the Excel, then try to open the document, it says that this
document is currently used by another user (IUSR_*). After I manually kill
the EXCEL.EXE in task manager, file can be opened with no problem...


Dan Bass said:
Denis,

I've seen this before... What happens is the Excel quit command seems to
close the document down, but leave a blank instance of Excel open. The catch
is this blank instance appears to lock up.

try the shutdown method in this
http://www.thescarms.com/dotNet/ExcelObject.asp

Thanks
Daniel.


Denis Brkljacic said:
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
 
A

Alvin Bruney [MVP]

its one of the reasons to stay away from server-side automation of excel.
You may want to consider using the Office Web Components which has a
well-behaved Excel object included.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
----------------------------------------------------------


Denis Brkljacic said:
Thanks!

But, I wouldn't say that only Excel is up (and doc itself closed), because
after I quit the Excel, then try to open the document, it says that this
document is currently used by another user (IUSR_*). After I manually kill
the EXCEL.EXE in task manager, file can be opened with no problem...


Dan Bass said:
Denis,

I've seen this before... What happens is the Excel quit command seems to
close the document down, but leave a blank instance of Excel open. The catch
is this blank instance appears to lock up.

try the shutdown method in this
http://www.thescarms.com/dotNet/ExcelObject.asp

Thanks
Daniel.


Denis Brkljacic said:
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
 
D

Dan Bass

Agreed. (!!!)

Alvin Bruney said:
its one of the reasons to stay away from server-side automation of excel.
You may want to consider using the Office Web Components which has a
well-behaved Excel object included.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
----------------------------------------------------------


Denis Brkljacic said:
Thanks!

But, I wouldn't say that only Excel is up (and doc itself closed),
because
after I quit the Excel, then try to open the document, it says that this
document is currently used by another user (IUSR_*). After I manually
kill
the EXCEL.EXE in task manager, file can be opened with no problem...


"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in
message
Denis,

I've seen this before... What happens is the Excel quit command seems to
close the document down, but leave a blank instance of Excel open. The catch
is this blank instance appears to lock up.

try the shutdown method in this
http://www.thescarms.com/dotNet/ExcelObject.asp

Thanks
Daniel.


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
 
Top