Создание сводной таблицы
Contents
[
Hide
]
Создание сводной таблицы
Aspose.Cells для Python via Java предоставляет возможность создания сводных таблиц. Для создания сводной таблицы с помощью Aspose.Cells следуйте следующим шагам:
- Добавьте некоторые данные в ячейки листа, используя свойство setValue объекта Cell. Эти данные будут использоваться в качестве источника данных для сводной таблицы.
- Добавьте сводную таблицу на лист, вызвав метод add у PivotTableCollection, инкапсулированного в объекте Worksheet.
- Получите объект PivotTable из PivotTableCollection, передав индекс PivotTable.
- Используйте любой из объектов сводной таблицы (объяснено выше), инкапсулированных в PivotTableCollection, для управления сводной таблицей.
Приведенный ниже фрагмент кода демонстрирует создание сводной таблицы с помощью Aspose.Cells API.
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
output_directory = "Examples/SampleFiles/OutputDirectory/" | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Obtaining the reference of the newly added worksheet | |
sheetIndex = workbook.getWorksheets().add() | |
worksheet = workbook.getWorksheets().get(sheetIndex) | |
cells = worksheet.getCells() | |
# Setting the value to the cells | |
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) | |
pivotTables = worksheet.getPivotTables() | |
# Adding a PivotTable to the worksheet | |
index = pivotTables.add("=A1:C8", "E3", "PivotTable2") | |
# Accessing the instance of the newly added PivotTable | |
pivotTable = pivotTables.get(index) | |
# Unshowing grand totals for rows | |
pivotTable.setRowGrand(False) | |
# Dragging the first field to the row area | |
pivotTable.addFieldToArea(PivotFieldType.ROW, 0) | |
# Dragging the second field to the column area | |
pivotTable.addFieldToArea(PivotFieldType.COLUMN, 1) | |
# Dragging the third field to the data area | |
pivotTable.addFieldToArea(PivotFieldType.DATA, 2) | |
# Saving the Excel file | |
workbook.save(output_directory + "CreatePivotTable_out.xlsx") |