Free Support Forum -

Accessing a cell and checking its datatype


My question is if i want to access an excel spread sheet and then verify the datatypes in the cells how do i do this. My thought is a double while loop. the first loop iterates through the rows and the second loop iterates through the columns. Within the second one though how do i access each Cell? i thought arraycellchecker = worksheet.cells[count,i]; where count is from the first while ( while count < rows) and the i is from the second while ( while i <= columns) what am i doing wrong?

This message was posted using Page2Forum from Accessing Cells of a Worksheet - Aspose.Cells for .NET


Well, you may use Cell.Type API to check the type of the data a cell has.
May the following sample code help you for your requirement. You may change the code or add to the code for your need.

Sample code:

Workbook workbook = new Workbook();
//Get the first worksheet.
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;

//Variables to store values of different data types
string stringValue;
double doubleValue;
bool boolValue;
DateTime dateTimeValue;

//Iterate through all the cells in the sheet.
for (int row = 0; row <= cells.MaxDataRow; row++)

for (int col = 0; col <= cells.MaxDataColumn; col++)
//Get the cell.
Aspose.Cells.Cell cell = cells[row, col];

//Passing the type of the data contained in the cell for evaluation
switch (cell.Type)

//Evaluating the data type of the cell data for string value
case CellValueType.IsString:
stringValue = cell.StringValue;

//Evaluating the data type of the cell data for double value
case CellValueType.IsNumeric:
doubleValue = cell.DoubleValue;

//Evaluating the data type of the cell data for boolean value
case CellValueType.IsBool:
boolValue = cell.BoolValue;

//Evaluating the data type of the cell data for date/time value
case CellValueType.IsDateTime:
dateTimeValue = cell.DateTimeValue;

//Evaluating the unknown data type of the cell data
case CellValueType.IsUnknown:
stringValue = cell.StringValue;

//Terminating the type checking of type of the cell data is null
case CellValueType.IsNull:





Thank you.