Reading Excel files in C # is always on .__ ComObject?

This is the code I use to read the 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());

As a result, a window appears with the message:

System.__ComObject

Not sure what is going on, I would really appreciate any help, thanks!

+3
source share
3 answers

Use range

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

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());
+3
source

In my project, I am using Value2:

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

Source: https://habr.com/ru/post/1784119/


All Articles