I've got this set of code, now, I know you have to dispose com references and run the garbage collector after accessing excel, but my particular code doesn't write excel references to variables... how do I dispose the excel app properly in this case?
Here it is:
try
{
OleDbcon =
new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + openFileDialog.FileName +
";Extended Properties='Excel 8.0; HDR = Yes; IMEX = 1'");
OleDbcon.Open();
System.Data.DataTable dt = OleDbcon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
OleDbcon.Close();
CMBsheets.Items.Clear();
for (int i = 0; i < dt.Rows.Count; i++)
{
String sheetName = dt.Rows[i]["TABLE_NAME"].ToString();
sheetName = sheetName.Substring(0, sheetName.Length - 1);
CMBsheets.Items.Add(sheetName);
}
}
catch (Exception ex)
{
MessageBox.Show("Write Excel: " + ex.Message);
}
This works but EXCEL doesn't close! so I cannot save data from another codeblock. (Btw, the code adds the excel book into a combobox where the sheets are individually searchable, on selection, the data is displayed into a datagridview)
Thanks in advance!