Add and Retrieve Data
Adding Data to Cells
Aspose.Cells provides a class IWorkbook that represents a Microsoft Excel file. The IWorkbook class contains an IWorksheets collection that allows access to each worksheet in the Excel file. A worksheet is represented by the IWorksheet class. The IWorksheet class provides an ICells collection. Each item in the ICells collection represents an object of the ICell class.
Aspose.Cells allows developers to add data to the cells in worksheets by calling the ICell 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.
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 IWorkbook that represents a Microsoft Excel file. The IWorkbook class contains an IWorksheets collection that allows access to worksheets in the file. A worksheet is represented by the IWorksheet class. The IWorksheet class provides a ICells collection. Each item in the ICells collection represents an object of the ICell class.
The ICell class provides several methods that allow developers to retrieve values from the cells according to their data types. These methods include:
- GetStringValue, returns the string value of the cell.
- GetDoubleValue, returns the double value of the cell.
- GetBoolValue, returns the boolean value of the cell.
- GetDateTimeValue, returns the date/time value of the cell.
- GetFloatValue, returns the float value of the cell.
- GetIntValue, returns the integer value of the cell.
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 ICell class GetType method. In fact, the ICell 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. |