管理Excel文件的数据

如何向单元格添加数据

Aspose.Cells for Python via .NET提供了一个表示Microsoft Excel文件的类,WorkbookWorkbook类包含一个worksheets集合,用于访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供了cells集合。cells集合中的每个项目表示Cell类的对象。

Aspose.Cells for Python via .NET允许开发人员通过调用Cell类的put_value方法向工作表中的单元格添加数据。Aspose.Cells for Python via .NET提供了put_value方法的重载版本,让开发人员可以向单元格添加不同类型的数据。使用put_value方法的这些重载版本,可以向单元格添加布尔值、字符串、双精度、整数或日期/时间等值。

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")

如何提高效率

如果您使用put_value方法向工作表添加大量数据,应先按行然后按列添加值到单元格。这种方法极大地提高了应用程序的效率。

如何从单元格中检索数据

Aspose.Cells for Python via .NET提供了一个表示Microsoft Excel文件的类,Workbook,该类包含一个worksheets集合,允许访问文件中的工作表。工作表由Worksheet类表示。Worksheet类提供了一个cells集合。该集合中的每个项目表示Cell类的对象。

Cell类提供了几个属性,允许开发人员根据其数据类型从单元格中检索值。这些属性包括:

当字段未填写时,具有double_value 或float_value 的单元格会引发异常。

还可以通过使用Cell 类的type 属性来检查单元格中包含的数据类型。 实际上,Cell 类的type 属性基于CellValueType 枚举,其预定义值如下:

单元格值类型 描述
IS_BOOL 指定单元格值为布尔值。
IS_DATE_TIME 指定单元格值为日期/时间。
IS_NULL 表示空白单元格。
IS_NUMERIC 指定单元格值为数字。
IS_STRING 指定单元格值为字符串。
IS_ERROR 指定单元格值为错误值。
IS_UNKNOWN 指定单元格值为未知。

您还可以使用上述预定义的单元格值类型与每个单元格中存在的数据类型进行比较。

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

高级主题