创建分类汇总
使用Microsoft Excel
在 Microsoft Excel 中创建分类汇总:
-
在工作簿的第一个工作表中创建一个简单的数据列表(如下图所示),并将文件保存为 Book1.xls。
-
选择列表中的任何单元格。
-
从“数据”菜单中选择“分类汇总”选项。 分类汇总对话框将显示出来。定义要使用的函数以及放置分类汇总的位置。
选择要添加分类汇总的数据范围
分类汇总对话框
使用 Aspose.Cells API
Aspose.Cells 提供了一个代表 Microsoft Excel 文件的类,Workbook,该类包含一个 Workbook,允许访问 Excel 文件中的每个工作表。
工作表由 Worksheet 类表示。此类提供了广泛的属性和方法用于管理工作表和其他对象。每个工作表都由一个 Cells 集合组成。要在工作表中创建分类汇总,使用 Cells 类的 subtotal 方法。为方法的参数提供适当的值,以获得所需的结果。
下面的示例显示了如何在模板文件(Book1.xls)的第一个工作表中使用 Aspose.Cells API 创建分类汇总。
执行代码后,将创建包含分类汇总的工作表。
应用分类汇总
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(CreatingSubtotals.class) + "data/"; | |
// Instantiate a new workbook | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Get the Cells collection in the first worksheet | |
Cells cells = workbook.getWorksheets().get(0).getCells(); | |
// Create a cellarea i.e.., B3:C19 | |
CellArea ca = new CellArea(); | |
ca.StartRow = 2; | |
ca.StartColumn = 1; | |
ca.EndRow = 18; | |
ca.EndColumn = 2; | |
// Apply subtotal, the consolidation function is Sum and it will applied | |
// to | |
// Second column (C) in the list | |
cells.subtotal(ca, 0, ConsolidationFunction.SUM, new int[] { 1 }); | |
// Save the excel file | |
workbook.save(dataDir + "CreatingSubtotals_out.xls"); | |
// Print message | |
System.out.println("Process completed successfully"); |