How to get read type of data from excel file in C #

I am trying to read data from an Excel file. Is there a way to check the data type in each cell?

I tried .GetType (); but the result is a very tricky class that didn't help.

here is part of my code:

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

      

UPDATE: For example by running the following line:

string MobileNumber = (string)(range.Cells[1, 7] as Excel.Range).Value2;

      

I am getting this error:

Cannot convert type 'double' to 'string'

But this problem was solved with the following code:

double tempValue = (range.Cells[1, 7] as Excel.Range).Value2;
string MobileNumber = tempValue.ToString();

      

+3


source to share





All Articles