创建分类汇总
Contents
[
Hide
]
您可以自动为电子表格中的任何重复值创建小计。Aspose.Cells提供了API功能,帮助您以编程方式向电子表格添加小计。
使用Microsoft Excel
在Microsoft Excel中添加小计:
- 在工作簿的第一个工作表中创建一个简单的数据列表(如下图所示),并将文件保存为 Book1.xls。
- 选择列表中的任何单元格。
- 从数据菜单中选择小计。将显示小计对话框。定义要使用的函数和放置小计的位置。
使用Aspose.Cells API
Aspose.Cells提供了一个代表Microsoft Excel文件的类,Workbook。Workbook类包含Worksheets集合,允许访问Excel文件中的每个工作表。
工作表由Worksheet类表示。该类提供了广泛的属性和方法来管理工作表和其他对象。每个工作表都包含一个Cells集合。要向工作表添加小计,请使用Cells类的Subtotal方法。向该方法提供参数值以指定如何计算和放置小计。
在下面的示例中,我们使用Aspose.Cells API向模板文件(Book1.xls)的第一个工作表添加了小计。执行代码时,将创建一个带有小计的工作表。
以下代码片段展示了如何使用 Aspose.Cells for .NET 向工作表添加小计。
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-.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"); |