Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
数据透视表是一种交互式汇总报表:无需任何公式,即可将大量逐行排列的原始数据压缩为紧凑的矩阵。您可以决定哪些列用作行标签、哪些列用作列标签,以及哪些列作为值进行聚合——分组和计算均由数据透视表完成:
Aspose.Cells for Python via .NET 在 aspose.cells.pivot 命名空间中提供了一组专门用于创建和控制数据透视表的类。这些类用于创建和配置 PivotTable 对象,这些对象是构成数据透视表的基本单元。具体包括:
创建数据透视表包括三个步骤:将源数据写入工作表单元格、在目标位置添加数据透视表,以及将数据透视字段放入行区域、列区域和数据区域。
add(source_data, row, column)。source_data 是源区域(例如,“A1:C8”);row 和 column 是数据透视表左上角目标单元格的坐标,均从 0 开始计数(单元格 E3 表示行索引 2、列索引 4)。该方法直接返回新的 PivotTable 对象。add_field_to_area 方法将数据透视字段添加到相应区域:字段名称进入行区域(PivotFieldType.ROW)、列区域(PivotFieldType.COLUMN)或数据区域(PivotFieldType.DATA)。import aspose.cells as ac
from aspose.cells.pivot import PivotFieldType
# 创建一个新工作簿并获取第一个工作表
workbook = ac.Workbook()
sheet = workbook.worksheets[0]
# 写入源数据:按运动和季度统计的销售额
sheet.cells.get("A1").put_value("Sport")
sheet.cells.get("B1").put_value("Quarter")
sheet.cells.get("C1").put_value("Sales")
sheet.cells.get("A2").put_value("Golf")
sheet.cells.get("B2").put_value("Qtr3")
sheet.cells.get("C2").put_value(1500)
sheet.cells.get("A3").put_value("Golf")
sheet.cells.get("B3").put_value("Qtr4")
sheet.cells.get("C3").put_value(2000)
sheet.cells.get("A4").put_value("Tennis")
sheet.cells.get("B4").put_value("Qtr3")
sheet.cells.get("C4").put_value(600)
sheet.cells.get("A5").put_value("Tennis")
sheet.cells.get("B5").put_value("Qtr4")
sheet.cells.get("C5").put_value(1500)
sheet.cells.get("A6").put_value("Tennis")
sheet.cells.get("B6").put_value("Qtr3")
sheet.cells.get("C6").put_value(4070)
sheet.cells.get("A7").put_value("Tennis")
sheet.cells.get("B7").put_value("Qtr4")
sheet.cells.get("C7").put_value(5000)
sheet.cells.get("A8").put_value("Golf")
sheet.cells.get("B8").put_value("Qtr3")
sheet.cells.get("C8").put_value(6430)
# 在目标单元格 E3 添加数据透视表,源数据为 A1:C8
pivot_table = sheet.pivot_tables.add("A1:C8", 2, 4)
# 分配透视字段:将 Sport 放入行区域,将 Quarter 放入列区域,将 Sales 放入数据区域
pivot_table.add_field_to_area(PivotFieldType.ROW, "Sport")
pivot_table.add_field_to_area(PivotFieldType.COLUMN, "Quarter")
pivot_table.add_field_to_area(PivotFieldType.DATA, "Sales")
# 保存工作簿
workbook.save("pivotTable_test_out.xlsx")
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.