2011-01-07 57 views
3

这是我用来读取xls文件的代码:在C#中读取Excel文件总是在系统.__ ComObject中显示结果?

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()); 

它在一个消息框结果:

System.__ComObject 

不知道发生了什么事,我会很感激的任何帮助,谢谢!

回答

3

使用范围

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

谢谢!这正是我需要的。 – sooprise 2011-01-07 20:12:01

2

在您的例子,excelSheet.Cells [1,1]是不是一个值,而是一个对象(范围)。您需要获取对象的Value属性。

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

在项目中,我用值2:

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