创建分类汇总

使用Microsoft Excel

在Microsoft Excel中添加小计:

  1. 在工作簿的第一个工作表中创建一个简单的数据列表(如下图所示),并将文件保存为 Book1.xls。
  2. 选择列表中的任何单元格。
  3. 数据菜单中选择小计。将显示小计对话框。定义要使用的函数和放置小计的位置。

使用Aspose.Cells API

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

工作表由Worksheet类表示。该类提供了广泛的属性和方法来管理工作表和其他对象。每个工作表都包含一个Cells集合。要向工作表添加小计,请使用Cells类的Subtotal方法。向该方法提供参数值以指定如何计算和放置小计。

在下面的示例中,我们使用Aspose.Cells API向模板文件(Book1.xls)的第一个工作表添加了小计。执行代码时,将创建一个带有小计的工作表。

以下代码片段展示了如何使用 Aspose.Cells for .NET 向工作表添加小计。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a new workbook
// Open the template file
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Get the Cells collection in the first worksheet
Cells cells = workbook.Worksheets[0].Cells;
// 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 + "output.out.xls");

高级主题