Manage data of Excel files

How to Add Data to Cells

Aspose.Cells for Python via .NET provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains a worksheets collection that allows access to each worksheet in the Excel 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.

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

from aspose.cells import Workbook
from datetime import datetime
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first worksheet
worksheet = workbook.worksheets[0]
# Adding a string value to the cell
worksheet.cells.get("A1").put_value("Hello World")
# Adding a double value to the cell
worksheet.cells.get("A2").put_value(20.5)
# Adding an integer value to the cell
worksheet.cells.get("A3").put_value(15)
# Adding a boolean value to the cell
worksheet.cells.get("A4").put_value(True)
# Adding a date/time value to the cell
worksheet.cells.get("A5").put_value(datetime.now())
# Setting the display format of the date
style = worksheet.cells.get("A5").get_style()
style.number = 15
worksheet.cells.get("A5").set_style(style)
# Saving the Excel file
workbook.save(dataDir + "output.out.xls")

How to Improve Efficiency

If you use put_value method to put a large amount of data to 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.

How to Retrieve Data from Cells

Aspose.Cells for Python via .NET provides a class, Workbook that represents a Microsoft Excel file. The Workbook class contains a 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 properties that allow developers to retrieve values from the cells according to their data types. These properties include:

When a field is not filled, cells with double_value or float_value throws an exception.

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

Cell Value Types Description
IS_BOOL Specifies that cell value is Boolean.
IS_DATE_TIME Specifies that cell value is date/time.
IS_NULL Represents a blank cell.
IS_NUMERIC Specifies that cell value is numeric.
IS_STRING Specifies that cell value is a string.
IS_ERROR Specifies that cell value is an error value.
IS_UNKNOWN Specifies that cell value is unknown.

You can also use the above pre-defined cell value types to compare with the Type of data present in each cell.

from aspose.cells import CellValueType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Opening an existing workbook
workbook = Workbook(dataDir + "book1.xls")
# Accessing first worksheet
worksheet = workbook.worksheets[0]
for cell1 in worksheet.cells:
# Passing the type of the data contained in the cell for evaluation
if cell1.type == CellValueType.IS_STRING:
stringValue = cell1.string_value
print("String Value: " + stringValue)
elif cell1.type == CellValueType.IS_NUMERIC:
doubleValue = cell1.double_value
print("Double Value: " + str(doubleValue))
elif cell1.type == CellValueType.IS_BOOL:
boolValue = cell1.bool_value
print("Bool Value: " + str(boolValue))
elif cell1.type == CellValueType.IS_DATE_TIME:
dateTimeValue = cell1.date_time_value
print("DateTime Value: " + str(dateTimeValue))
elif cell1.type == CellValueType.IS_UNKNOWN:
stringValue = cell1.string_value
print("Unknown Value: " + stringValue)
elif cell1.type == CellValueType.IS_NULL:
pass

Advance topics