应用小计并更改大纲摘要行的方向,而不是详细信息下面的行
Contents
[
Hide
]
本文将解释如何对数据应用小计并更改大纲摘要行下面的方向。
你可以使用Worksheet.Cells.subtotal()方法对数据应用小计。它接受以下参数。
- 单元格区域 - 应用小计的范围
- 按组 - 按零为基础的整数偏移量分组
- 功能 - 小计功能。
- TotalList - 一个从零开始的字段偏移量数组,指示要添加小计的字段。
- Replace - 指示是否替换当前的小计
- PageBreaks - 指示是否在组之间添加分页符
- SummaryBelowData - 指示是否在数据下方添加摘要。
此外,您可以使用Worksheet.getOutline().SummaryRowBelow属性来控制大纲详细下方的摘要行的方向,如下面的屏幕截图所示。您可以在 Microsoft Excel 中使用数据 > 大纲 > 设置打开此设置。
示例
源文件和输出文件的屏幕截图对比
下图显示了示例代码中使用的源Excel文件,其中包含列A和B中的一些数据。
下面的屏幕截图显示了样例代码生成的输出 Excel 文件。如您所见,小计已应用于范围A2:B11,大纲的方向为详细下方的摘要行。
Java 代码应用小计并更改大纲摘要行的方向
以下是实现上述输出的示例代码。
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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
//directories | |
String sourceDir = Utils.Get_SourceDirectory(); | |
String outputDir = Utils.Get_OutputDirectory(); | |
// Create workbook from source Excel file | |
Workbook workbook = new Workbook(sourceDir + "SampleSubtotal.xlsx"); | |
// Access the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Get the Cells collection in the first worksheet | |
Cells cells = worksheet.getCells(); | |
// Create a cellarea i.e.., A2:B11 | |
CellArea ca = CellArea.createCellArea("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, new int[] { 1 }, true, false, true); | |
// Set the direction of outline summary | |
worksheet.getOutline().setSummaryRowBelow(true); | |
// Save the excel file | |
workbook.save(outputDir + "ASubtotal_out.xlsx"); |