0
votes

I am trying to read excel file through this way but getting error:

Microsoft.Office.Interop.Excel.Application appExcel;
Microsoft.Office.Interop.Excel.Workbook workbook;
Microsoft.Office.Interop.Excel.Range range;
Microsoft.Office.Interop.Excel._Worksheet worksheet;

appExcel = new Microsoft.Office.Interop.Excel.Application();
workbook = appExcel.Workbooks.Open(path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
// getting error on the below line
worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets;

Error is below..

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel._Worksheet'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D8-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

have any idea about this exception?

1

1 Answers

0
votes

Apparently, workbook.Sheets is not a type of Worksheet.

worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets;

UPDATE:

Sheets is a collection of Worksheet, so instead add an index:

Microsoft.Office.Interop.Excel._Worksheet worksheet;

worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets[0];

Hope it helps!