I wrote some code to read cell values from existing excel file.
When I debug, the program throw a "System.Runtime.InteropServices.COMException". I know that we should never use 2 dots with COM objects, but I don't know how to fix my code.
Please help me to clean up COM object when opening excel file.
private void button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlBook;
Excel.Worksheet xlSheet;
xlBook = xlApp.Workbooks.Open(@"C:\Users\trump45\Desktop\trump45.xls");
xlApp.Visible = false;
xlSheet = xlBook.Sheets["Sheet4"];
//Exception rising here
string d = xlSheet.UsedRange.Cells[3, "B"].Value.ToString();
MessageBox.Show(d);
xlBook.Close(false, Missing.Value, Missing.Value);
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
}