行和列的分组和取消分组
介绍
在 Microsoft Excel 文件中,您可以创建一个数据大纲,以便通过单击鼠标来显示和隐藏不同级别的细节。
单击大纲符号1、2、3、+和- 快速显示工作表中仅提供摘要或标题部分的行或列,或者您可使用符号查看摘要或标题下的详细信息,如下图所示:
分组行和列 |
---|
![]() |
行和列的分组管理
Aspose.Cells for Python via .NET提供了一个代表Microsoft Excel文件的类Workbook。Workbook类包含一个WorksheetCollection,允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供了一个Cells集合,代表工作表中的所有单元格。
Cells集合提供了几种管理工作表行或列的方法,以下将更详细地讨论其中的一些。
如何对行和列进行分组
通过调用Cells集合的group_rows和group_columns方法,可以对行或列进行分组。这两种方法都带有以下参数:
- 第一个行/列索引,即组中的第一行或列。
- 最后一个行/列索引,即组中的最后一行或列。
- 是否隐藏,一个布尔参数,指定是否在分组后隐藏行/列。
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") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Grouping first six rows (from 0 to 5) and making them hidden by passing true | |
worksheet.cells.group_rows(0, 5, True) | |
# Grouping first three columns (from 0 to 2) and making them hidden by passing true | |
worksheet.cells.group_columns(0, 2, True) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
分组设置
Microsoft Excel 允许您配置用于显示的分组设置:
- 详细信息下面的摘要行。
- 详细信息右侧的摘要列。
开发者可以使用Worksheet类的outline属性配置这些组设置。
如何将汇总行放置在细节下方
可以通过将Outline类的summary_row_below属性设置为true或false来控制是否在详细信息下方显示摘要行。
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(".") | |
workbook = Workbook(dataDir + "sample.xlsx") | |
worksheet = workbook.worksheets[0] | |
# Grouping first six rows and first three columns | |
worksheet.cells.group_rows(0, 5, True) | |
worksheet.cells.group_columns(0, 2, True) | |
# Setting SummaryRowBelow property to false | |
worksheet.outline.summary_row_below = False | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xls") |
如何将汇总列放置在细节右侧
开发者还可以通过将Outline类的summary_column_right属性设置为true或false来控制是否在详细信息右侧显示摘要列。
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(".") | |
workbook = Workbook(dataDir + "sample.xlsx") | |
worksheet = workbook.worksheets[0] | |
# Grouping first six rows and first three columns | |
worksheet.cells.group_rows(0, 5, True) | |
worksheet.cells.group_columns(0, 2, True) | |
worksheet.outline.summary_column_right = True | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xls") |
如何取消分组行和列
要取消任何已分组的行或列,调用Cells集合的ungroup_rows和ungroup_columns方法。这两个方法都带有两个参数:
- 第一个行或列索引,即要取消分组的第一行/列。
- 最后一个行或列索引,即要取消分组的最后一行/列。
ungroup_rows有一个额外的重载,它带有一个布尔型第三个参数。将其设置为true会移除所有分组信息。否则,只有外部的组信息会被移除。
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) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Ungrouping first six rows (from 0 to 5) | |
worksheet.cells.ungroup_rows(0, 5) | |
# Ungrouping first three columns (from 0 to 2) | |
worksheet.cells.ungroup_columns(0, 2) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |