Add and Retrieve Data

Adding Data to Cells

Aspose.Cells provides a class Workbook that represents a Microsoft Excel file. The Workbook class contains an Worksheets collection that allows access to each worksheet in the Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides an Cells collection. Each item in the Cells collection represents an object of the Cell class.

Aspose.Cells allows developers to add data to the cells in worksheets by calling the Cell class PutValue method. Aspose.Cells provides overloaded versions of the PutValue method that lets developers add different kinds of data to cells. Using these overloaded versions of the PutValue method, it is possible to add a Boolean, string, double, integer or date/time, etc. values to the cell.

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleData = dirPath + u"sampleData.xlsx";
//Path of output excel file
U16String outputData = outPath + u"outputData.xlsx";
//Read input excel file
Workbook workbook(sampleData);
//Accessing the second worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(1);
//Adding a string value to the cell
worksheet.GetCells().Get(u"A1").PutValue(u"Hello World");
//Adding a double value to the cell
worksheet.GetCells().Get(u"A2").PutValue(20.5);
//Adding an integer value to the cell
worksheet.GetCells().Get(u"A3").PutValue(15);
//Adding a boolean value to the cell
worksheet.GetCells().Get(u"A4").PutValue(true);
//Setting the display format of the date
Cell cell = worksheet.GetCells().Get(u"A5");
Style style = cell.GetStyle();
style.SetNumber(15);
cell.SetStyle(style);
//Save the workbook
workbook.Save(outputData);
Aspose::Cells::Cleanup();

Improving Efficiency

If you use PutValue method to put a large amount of data into a worksheet, you should add values to the cells, first by rows and then by columns. This approach greatly improves the efficiency of your applications.

Retrieving Data from Cells

Aspose.Cells provides a class Workbook that represents a Microsoft Excel file. The Workbook class contains an Worksheets collection that allows access to worksheets in the file. A worksheet is represented by the Worksheet class. The Worksheet class provides a Cells collection. Each item in the Cells collection represents an object of the Cell class.

The Cell class provides several methods that allow developers to retrieve values from the cells according to their data types. These methods include:

When a field is not filled, cells with GetDoubleValue or GetFloatValue throws an exception.

The type of data contained in a cell can also be checked by using the Cell class GetType method. In fact, the Cell class GetType method is based on the CellValueType enumeration whose pre-defined values are listed below:

Cell Value Types Description
CellValueType_IsBool Specifies that cell value is Boolean.
CellValueType_IsDateTime Specifies that cell value is date/time.
CellValueType_IsNull Represents a blank cell.
CellValueType_IsNumeric Specifies that cell value is numeric.
CellValueType_IsString Specifies that cell value is string.
CellValueType_IsUnknown Specifies that cell value is unknown.
You can also use the above pre-defined cell value types to compare with the Type of the data present in each cell.
Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleData = dirPath + u"sampleData.xlsx";
//Read input excel file
Workbook workbook(sampleData);
//Accessing the third worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(2);
//Get cells from sheet
Cells cells = worksheet.GetCells();
//Variable declarations
U16String strVal;
Date dateVal;
double dblVal;
bool boolVal;
Enumerator<Cell> enCell = cells.GetEnumerator();
while (enCell.MoveNext())
{
Cell cell = enCell.GetCurrent();
switch (cell.GetType())
{
//Evaluating the data type of the cell data for string value
case CellValueType::IsString:
std::cout <<"Cell Value Type Is String." << std::endl;
strVal = cell.GetStringValue();
break;
//Evaluating the data type of the cell data for double value
case CellValueType::IsNumeric:
std::cout <<"Cell Value Type Is Numeric." << std::endl;
dblVal = cell.GetDoubleValue();
break;
//Evaluating the data type of the cell data for boolean value
case CellValueType::IsBool:
std::cout <<"Cell Value Type Is Bool." << std::endl;
boolVal = cell.GetBoolValue();
break;
//Evaluating the data type of the cell data for date/time value
case CellValueType::IsDateTime:
std::cout <<"Cell Value Type Is DateTime." << std::endl;
dateVal = cell.GetDateTimeValue();
break;
//Evaluating the unknown data type of the cell data
case CellValueType::IsUnknown:
cell.GetStringValue();
break;
default:
break;
}
}
Aspose::Cells::Cleanup();