管理Microsoft Excel文件的工作表

Aspose.Cells提供了一个表示Excel文件的类,Workbook类包含一个worksheets集合,允许访问Excel文件中的每个工作表。

工作表由Worksheet类表示。Worksheet类提供了广泛的属性和方法来管理工作表。

如何将工作表添加到新的Excel文件

要通过程序创建新的Excel文件:

  1. 创建Workbook类的对象。
  2. 调用 WorksheetCollection 类的 add 方法。自动向 Excel 文件中添加一个空工作表。可以通过将新工作表的索引传递给 worksheets 集合来引用它。
  3. 获取工作表引用。
  4. 对工作表进行操作。
  5. 调用 Workbook 类的 save 方法,保存带有新工作表的新 Excel 文件。
from aspose.cells import Workbook
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()
# Adding a new worksheet to the Workbook object
i = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Setting the name of the newly added worksheet
worksheet.name = "My Worksheet"
# Saving the Excel file
workbook.save(dataDir + "output.out.xls")

如何将工作表添加到设计师中的电子制表文档

向设计电子表格添加工作表的过程与添加新工作表的过程相同,只是 Excel 文件已经存在,所以在添加工作表之前应该打开它。可以通过 Workbook 类打开设计电子表格。

from aspose.cells import 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(".")
InputPath = dataDir + "book1.xlsx"
# Creating a file stream containing the Excel file to be opened
fstream = open(InputPath, "rb")
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Adding a new worksheet to the Workbook object
i = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Setting the name of the newly added worksheet
worksheet.name = "My Worksheet"
# Saving the Excel file
workbook.save(dataDir + "output.xlsx")

如何使用工作表名访问工作表

通过指定名称或索引来访问任何工作表。

from aspose.cells import 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(".")
InputPath = dataDir + "book1.xlsx"
# Creating a file stream containing the Excel file to be opened
fstream = open(InputPath, "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing a worksheet using its sheet name
worksheet = workbook.worksheets.get("Sheet1")
cell = worksheet.cells.get("A1")
print(cell.value)

如何使用工作表名删除工作表

要从文件中删除工作表,调用 WorksheetCollection 类的 remove_by_name 方法。将工作表的名称传递给 remove_by_name 方法以删除特定工作表。

from aspose.cells import 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(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Removing a worksheet using its sheet name
workbook.worksheets.remove_by_name("Sheet1")
# Save workbook
workbook.save(dataDir + "output.out.xls")

如何使用工作表索引删除工作表

当知道工作表的名称时,通过名称删除工作表的方法效果很好。如果不知道工作表的名称,请使用remove_by_index方法,该方法使用工作表的索引而不是工作表的名称。

from aspose.cells import 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(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Removing a worksheet using its sheet index
workbook.worksheets.remove_by_index(0)
# Save workbook
workbook.save(dataDir + "output.out.xls")

如何激活工作表并使工作表单元格活动

有时,您需要让特定工作表在用户在 Excel 中打开 Microsoft Excel 文件时处于活动状态并显示。同样,您可能希望激活特定单元格并设置滚动条以显示活动单元格。 Aspose.Cells 能够执行所有这些任务。

活动工作表 是您正在处理的工作表:标签上的活动工作表名称默认为粗体。

活动单元格 是所选单元格,也就是在开始输入数据时输入数据的单元格。一次只有一个单元格处于活动状态。活动单元格由粗边框突出显示。

如何激活工作表并激活单元格

Aspose.Cells 提供特定的 API 调用来激活工作表和单元格。例如,Aspose.Cells.WorksheetCollection.active_sheet_index 属性可用于在工作簿中设置活动工作表。 类似地,Aspose.Cells.Worksheet.active_cell 属性用于设置和获取工作表中的活动单元格。

要确保水平或垂直滚动条位于要显示特定数据的行和列索引位置,请使用 Aspose.Cells.Worksheet.first_visible_rowAspose.Cells.Worksheet.first_visible_column 属性。

以下示例显示了如何激活工作表并将其中一个单元格设为活动单元格。在生成的输出中,滚动条将滚动以使第二行和第二列成为它们的第一个可见行和列。

from aspose.cells import 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(".")
# Instantiate a new Workbook.
workbook = Workbook()
# Get the first worksheet in the workbook.
worksheet1 = workbook.worksheets[0]
# Get the cells in the worksheet.
cells = worksheet1.cells
# Input data into B2 cell.
cells.get(1, 1).put_value("Hello World!")
# Set the first sheet as an active sheet.
workbook.worksheets.active_sheet_index = 0
# Set B2 cell as an active cell in the worksheet.
worksheet1.active_cell = "B2"
# Set the B column as the first visible column in the worksheet.
worksheet1.first_visible_column = 1
# Set the 2nd row as the first visible row in the worksheet.
worksheet1.first_visible_row = 1
# Save the excel file.
workbook.save(dataDir + "output.xls")