sooprise
sooprise

Reputation: 23187

Reading Excel Files In C# Always Results In System.__ComObject?

This is the code I'm using to read an xls file:

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(filePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(1);

MessageBox.Show(excelSheet.Cells[1,1].ToString());

Which results in a message box with:

System.__ComObject

Not sure what's going on, I'd really appreciate any help, thanks!

Upvotes: 3

Views: 11317

Answers (3)

Bolu
Bolu

Reputation: 8786

In my project I use Value2:

 MessageBox.Show(((Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[1, 1]).Value2.ToString());

Upvotes: 0

Mike Ohlsen
Mike Ohlsen

Reputation: 1900

In your example, excelSheet.Cells[1,1] is not a value but an object (Range). You need to get the Value property of the object.

MessageBox.Show(excelSheet.Cells[1, 1].Value.ToString());

Upvotes: 3

Ta01
Ta01

Reputation: 31630

Use Range

Microsoft.Office.Interop.Excel.Range range =(Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[1,1];
string cellValue =range.Value.ToString();

Upvotes: 4

Related Questions