在C#中读取Excel文件总是在系统.__ ComObject的结果?
这是我用来读取xls文件的代码:
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
不知道发生了什么,我非常感谢任何帮助,谢谢!
使用范围
Microsoft.Office.Interop.Excel.Range range =(Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[1,1]; string cellValue =range.Value.ToString();
在你的例子中,excelSheet.Cells [1,1]不是一个值,而是一个对象(Range)。 您需要获取对象的Value属性。
MessageBox.Show(excelSheet.Cells[1, 1].Value.ToString());
在我的项目中,我使用Value2:
MessageBox.Show(((Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[1, 1]).Value2.ToString());