I try to read my Exel file from code and received System.InvalidCastException:
Additional information: Unable to cast COM object of type \'System.__Co
Workbook.ActiveSheet Property might not be the best choice for programmatically opened Excel files as it can actually return a non-Worksheet object
You might want to consider checking sheets count and using indexes:
int count = newWorkbook.Worksheets.Count;
if (count > 0)
{
objsheet = (Worksheet) newWorkbook.Worksheets[1];
}
And try not to break the 2-dot rule - you'll need to release all you COM's to properly close your app and Excel.
Edited:
You could be mixing Microsoft.Office.Interop.Excel with Microsoft.Office.Tools.Excel namespaces.
Try to declare and assign as follows:
private static Microsoft.Office.Interop.Excel.Worksheet objsheet = null;
...
objsheet = (Microsoft.Office.Interop.Excel.Worksheet) newWorkbook.Worksheets[1];