如何从C#中的Excel文件中读取数据的types
我正在尝试从Excel文件中读取数据。 有没有办法检查每个单元格中的数据types?
我试过.GetType(); 但结果是一个非常复杂的课,没有帮助。
这里是我的代码的一部分:
Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; Excel.Range range; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open(FilePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); range = xlWorkSheet.UsedRange; var a = range.Cells[1/*row*/, 1/*column*/] as Excel.Range; var type = a.GetType();
更新:例如,通过runnig这一行:
string MobileNumber = (string)(range.Cells[1, 7] as Excel.Range).Value2;
我得到这个错误:
无法将types“double”转换为“string”
但是这个问题解决了下面的代码:
double tempValue = (range.Cells[1, 7] as Excel.Range).Value2; string MobileNumber = tempValue.ToString();