应用小计并更改大纲摘要行的方向,而不是详细信息下面的行
Contents
[
Hide
]
本文将解释如何对数据应用小计并更改大纲摘要行下面的方向。
你可以使用Worksheet.cells.subtotal()方法对数据应用小计。它接受以下参数。
- ca - 应用小计的范围
- group_by - 以从零开始的整数偏移量作为分组依据的字段
- function - 小计函数
- total_list - 一个从零开始的字段偏移量数组,指示添加小计的字段
- replace - 指示是否替换当前小计
- page_breaks - 指示是否在分组之间添加分页符 -summary_below_data- 表示是否将摘要添加在数据下方。
此外,您可以使用 Worksheet.Outline.SummaryRowBelow 属性,在下图所示的 Microsoft Excel 中通过“数据 > 大纲 > 设置”来控制大纲摘要行在细节下方的方向。
源文件和输出文件的图片
下图显示了示例代码中使用的源Excel文件,其中包含列A和B中的一些数据。
下图显示了示例代码生成的输出Excel文件。如您所见,对范围A2:B11应用了小计,并且大纲的方向是细节下方的摘要行。
Python代码应用小计并改变大纲摘要行的方向
以下是实现上述输出的示例代码。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import CellArea, ConsolidationFunction, 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(".") | |
# Create workbook from source Excel file | |
workbook = Workbook(dataDir + "Book1.xlsx") | |
# Access the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Get the Cells collection in the first worksheet | |
cells = worksheet.cells | |
# Create a cellarea i.e.., A2:B11 | |
ca = CellArea.create_cell_area("A2", "B11") | |
# Apply subtotal, the consolidation function is Sum and it will applied to Second column (B) in the list | |
cells.subtotal(ca, 0, ConsolidationFunction.SUM, [1 ], True, False, True) | |
# Set the direction of outline summary | |
worksheet.outline.summary_row_below = True | |
# Save the excel file | |
workbook.save(dataDir + "output_out.xlsx") |