Excel 中的数据透视表和数据透视图

数据透视表是一种整合 Microsoft Excel 中包含的大量数据的工具。它们让用户能够更快地从收集的数据中得出结论。数据透视表由列、行、页和数据字段组成,它们有助于扩展、隔离、汇总和分组特定数据。此外,数据透视表还允许您获取大量数据并以仅需查看少量数据字段的方式对其进行处理。数据透视表的其他一些好处包括

  • 数据透视表使数据分析更容易
  • 数据透视表可以创建即时数据
  • 数据透视表易于使用
  • 数据透视表轻松汇总数据
  • 数据透视表有助于查找数据模式
  • 数据透视表可以更快地创建准确的报告
  • 数据透视表可以帮助更快地做出决策

将数据透视表添加到 Excel 文件

Aspose.Cells Cloud API 提供了一组类,可让您将数据透视表添加到 Excel 工作表。PutWorksheetPivotTable 方法允许您在 Excel 工作表中添加新的数据透视表。为了添加表,我们需要提供以下参数

  • name(字符串) - 我们需要添加数据透视表的 Excel 文件的名称。
  • sheetName(string) - 您想要添加数据透视表的工作表名称。
  • sourceData(字符串) - 数据透视表的数据源。
  • destCellName(字符串) - 数据透视表目标范围左上角的单元格。
  • tableName(字符串) - 新数据透视表的名称。
  • useSameSource(布尔值) - 表示当另一个现有的数据透视表已使用此数据源时是否使用相同的数据源。

cURL 命令

curl -X PUT "https://api.aspose.cloud/v3.0/cells/Family%20Budget1.xlsx/worksheets/Sheet1/pivottables?sourceData='Current%20Month'!%24B%2427%3A%24E%2446&destCellName=B14&tableName=Pivot1&useSameSource=true" -H  "accept: application/json" -H  "authorization: Bearer <JWT Token>"

请求 URL

https://api.aspose.cloud/v3.0/cells/Family%20Budget1.xlsx/worksheets/Sheet1/pivottables?sourceData='Current%20Month'!%24B%2427%3A%24E%2446&destCellName=B14&tableName=Pivot1&useSameSource=true

C#.NET

string MyAppKey = "xxxxxxxx";    // Get AppKey from https://dashboard.aspose.cloud/
string MyAppSid = "xxxxxxxxx";   // Get AppSID from https://dashboard.aspose.cloud/

try
{
    Aspose.Cells.Cloud.SDK.Api.CellsApi cellsApi = new Aspose.Cells.Cloud.SDK.Api.CellsApi(MyAppSid, MyAppKey);
    String fileName = "Family Budget1.xlsx";
    string sheetName = "Sheet1";

    Aspose.Cells.Cloud.SDK.Model.CreatePivotTableRequest createPivotTableRequest = 
        new Aspose.Cells.Cloud.SDK.Model.CreatePivotTableRequest();
    createPivotTableRequest.Name = "NewPivot";
    createPivotTableRequest.SourceData = "'Current Month'!$B$21:$C$24";
    createPivotTableRequest.DestCellName = "G22";
    createPivotTableRequest.UseSameSource = true;
    createPivotTableRequest.PivotFieldColumns = new System.Collections.Generic.List<int?> { 1 };
    createPivotTableRequest.PivotFieldRows = new System.Collections.Generic.List<int?> { 1 };
    createPivotTableRequest.PivotFieldData = new System.Collections.Generic.List<int?> { 1 };

    Aspose.Cells.Cloud.SDK.Model.PivotTableResponse pivotTableResponse = 
        cellsApi.CellsPivotTablesPutWorksheetPivotTable(fileName, sheetName, createPivotTableRequest);

    if (pivotTableResponse != null && pivotTableResponse.Status.Equals("OK"))
    {
        Console.WriteLine("Add a Pivot Table in a Worksheet, Done!");
        Console.ReadKey();
    }
}
catch (Exception ex)
{
    System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}