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
c # types file excel


source to share


No one has answered this question yet

Check out similar questions:

5116
How can I check if a file exists without exceptions?
4829
How do I include a JavaScript file in another JavaScript file?
3575
How to list an enumeration?
2112
How do I copy a file in Python?
2058
How do I get a consistent byte representation of strings in C # without manually specifying the encoding?
2028
How to read a file line by line in a list?
1786
How to create Excel file (.XLS and .XLSX) in C # without installing Microsoft Office?
1742
How to calculate a person's age in C #?
1658
Get int value from enum in c #
0
How to get datagridview1 to split both two and three in the current excel sheet



All Articles
Loading...
X
Show
Funny
Dev
Pics