Free Support Forum - aspose.com

Accessing a cell and checking its datatype

Hello,

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

Hi,

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();
workbook.Open(“e:\test\MyBook1.xlsx”);
//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;
break;

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

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

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

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

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

}



}

}

workbook.Save(“d:\test\out_Book1.xlsx”);


Thank you.