创建小计

使用 Microsoft Excel

要在 Microsoft Excel 中创建小计:

  1. 在工作簿的第一个工作表中创建一个简单的数据列表(如下图所示),并将文件保存为Book1.xls。

  2. 选择列表中的任何单元格。

  3. 来自数据菜单,选择小计. 显示小计对话框。定义要使用的函数以及放置小计的位置。

    选择数据范围以添加小计

待办事项:图片_替代_文本

小计对话框

待办事项:图片_替代_文本

使用 Aspose.Cells API

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

工作表由工作表班级。该类提供了广泛的属性和方法来管理工作表和其他对象。每个工作表由一个Cells收藏。要在工作表中创建小计,请使用Cells类的小计法。为方法的参数提供适当的值以获得您想要的结果。

下面的示例显示如何使用 Aspose.Cells API 在模板文件 (Book1.xls) 的第一个工作表中创建小计。

执行代码时,将创建一个包含小计的工作表。

应用小计

待办事项:图片_替代_文本

// 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");