Hey everyone,
I'm having an issue where my application will keep Excel running even after it closes. I think I'm closing everything properly, but is there a way to check on close?
Here's part of my code:
////////////// Part of ExcelWriter class
public void CreateSheet(Laptop lptp, string file, string sheet)
{
try
{
xlBook = xl.Workbooks.Open(file_path + file, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
xlSheet = xlBook.Worksheets.get_Item(1);
xl.Worksheets.Add(oMissing, oMissing, oMissing, oMissing);
xl.Worksheets[1].name = sheet;
}
catch (Exception ex)
{
MessageBox.Show("Create Error: " + ex.Message, "Could not create sheet", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
xlBook.Save();
xl.Application.Workbooks.Close();
xl.Workbooks.Close();
}
}
/////////////// Part of ExcelWriter class.
public void UpdateFile(Laptop lptp, string file)
{
try
{
xlBook = xl.Workbooks.Open(file_path + file, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
xlSheet = xlBook.Worksheets.get_Item(1);
xl.ActiveSheet.Name = "List";
int num_rows = xlSheet.Rows.Count;
xlSheet.Cells[1, num_rows+1] = "Test";
}
catch (Exception ex)
{
MessageBox.Show("Write Error: " + ex.Message, "Could not write to file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
xlBook.Save();
xl.Application.Workbooks.Close();
xl.Workbooks.Close();
}
}