插入数据透视表
Contents
[
Hide
]
创建数据透视表
可以使用Aspose.Cells for Node.js via C++以编程方式在电子表格中添加数据透视表。
数据透视表对象模型
Aspose.Cells for Node.js via C++提供了一套特殊的类,用于创建和控制数据透视表。这些类用于创建和设置PivotTable对象,作为数据透视表的基本构建块。对象包括:
- PivotField 代表 PivotTable 中的一个字段。
- PivotFieldCollection 代表 PivotTable 中的所有 PivotField 对象的集合。
- PivotTable 代表工作表上的数据透视表。
- PivotTableCollection 代表工作表上的所有 PivotTable 对象的集合。
使用 Aspose.Cells 创建一个简单的数据透视表
- 使用 Cell 对象的 putValue 方法向工作表添加数据。 这些数据将被用作数据透视表的数据源。
- 通过调用 PivotTables 集合的 add 方法(封装在工作表对象中)向工作表添加一个数据透视表。
- 通过传递数据透视表索引从 PivotTables 集合中访问新的 PivotTable 对象。
- 使用上面解释的任何 PivotTable 对象来管理数据透视表。
执行示例代码后,数据透视表将被添加到工作表中。
This file contains hidden or 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
const AsposeCells = require("aspose.cells.node"); | |
//Instantiating a Workbook object | |
var workbook = new AsposeCells.Workbook(); | |
//Obtaining the reference of the newly added worksheet | |
var sheetIndex = workbook.getWorksheets().add(); | |
var sheet = workbook.getWorksheets().get(sheetIndex); | |
var cells = sheet.getCells(); | |
//Setting the value to the cells | |
var cell = cells.get("A1"); | |
cell.setValue("Sport"); | |
cell = cells.get("B1"); | |
cell.setValue("Quarter"); | |
cell = cells.get("C1"); | |
cell.setValue("Sales"); | |
cell = cells.get("A2"); | |
cell.setValue("Golf"); | |
cell = cells.get("A3"); | |
cell.setValue("Golf"); | |
cell = cells.get("A4"); | |
cell.setValue("Tennis"); | |
cell = cells.get("A5"); | |
cell.setValue("Tennis"); | |
cell = cells.get("A6"); | |
cell.setValue("Tennis"); | |
cell = cells.get("A7"); | |
cell.setValue("Tennis"); | |
cell = cells.get("A8"); | |
cell.setValue("Golf"); | |
cell = cells.get("B2"); | |
cell.setValue("Qtr3"); | |
cell = cells.get("B3"); | |
cell.setValue("Qtr4"); | |
cell = cells.get("B4"); | |
cell.setValue("Qtr3"); | |
cell = cells.get("B5"); | |
cell.setValue("Qtr4"); | |
cell = cells.get("B6"); | |
cell.setValue("Qtr3"); | |
cell = cells.get("B7"); | |
cell.setValue("Qtr4"); | |
cell = cells.get("B8"); | |
cell.setValue("Qtr3"); | |
cell = cells.get("C2"); | |
cell.setValue(1500); | |
cell = cells.get("C3"); | |
cell.setValue(2000); | |
cell = cells.get("C4"); | |
cell.setValue(600); | |
cell = cells.get("C5"); | |
cell.setValue(1500); | |
cell = cells.get("C6"); | |
cell.setValue(4070); | |
cell = cells.get("C7"); | |
cell.setValue(5000); | |
cell = cells.get("C8"); | |
cell.setValue(6430); | |
var pivotTables = sheet.getPivotTables(); | |
//Adding a PivotTable to the worksheet | |
var index = pivotTables.add("=A1:C8", "E3", "PivotTable2"); | |
//Accessing the instance of the newly added PivotTable | |
var pivotTable = pivotTables.get(index); | |
//Unshowing grand totals for rows. | |
pivotTable.setRowGrand(false); | |
//Dragging the first field to the row area. | |
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Row, 0); | |
//Dragging the second field to the column area. | |
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Column, 1); | |
//Dragging the third field to the data area. | |
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Data, 2); | |
//Saving the Excel file | |
workbook.save("CreatePivotTable_out.xlsx"); |
当将一系列单元格指定为数据源时,该范围必须从左上到右下。例如,“A1:C3”是有效的,但“C3:A1”是无效的。