Wednesday 15 August 2012

c# - Getting Excel to shutdown after interop -



c# - Getting Excel to shutdown after interop -

i have app sucks info out of excel spreadsheet. can't clean nicely. excel.exe continues run. i've read other posts need marshal.releasecomobject() on com objects. believe doing correctly.

to create things bit cleaner, i've removed error checking code (none of excel related), code:

public override marketpriceitem[] getmarketprices() { string[] files = directory.getfiles(configurationmanager.appsettings["xxspreadsheets"]); excel.application app = new excel.application { visible = false }; list<marketpriceitem> prices = new list<marketpriceitem>(); excel.workbooks workbooks = app.workbooks; foreach (string filename in files) { excel.workbook wb = null; wb = workbooks.open(filename); excel.worksheet sheet = wb.sheets[1]; cell = sheet.cells[1, 4]; string datestr = cell.text; marshal.releasecomobject(cell); datetime fri = datetime.parse(datestr); datetime today = datetime.today; int days = 5 - (friday - today).days; int todaycell = (days * 2) + 1; int rows = sheet.usedrange.rows.count; foreach (int key in _codetodescription.keys) { (int index = 4; index < rows; index++) { cell = sheet.cells[index, 1]; string desc = cell.text; marshal.releasecomobject(cell); if (desc.trim() == _codetodescription[key].trim()) { cell = sheet.cells[index, todaycell]; if (cell == null) { marshal.releasecomobject(cell); continue; } var valval = cell.value; marshal.releasecomobject(cell); if (valval == null) { continue; } prices.add(new marketpriceitem() { marketcode = key.tostring(), marketdate = today, markettypecode = "xx", cost = (decimal) valval }); } } } marshal.releasecomobject(sheet); marshal.releasecomobject(wb); } marshal.releasecomobject(workbooks); marshal.releasecomobject(app); homecoming prices.toarray(); }

what missing?

at end of code, have tried

app.quit();

that should quit instance of excel running.

c# excel com interop

No comments:

Post a Comment