Documents for Excel, .NET Edition Documentation
Manage Data / Use Pivot Table / Pivot Table Settings
In This Topic
    Pivot Table Settings
    In This Topic

    You can modify the setting of the pivot table created in a spreadsheet by performing the following tasks:

    Configure Pivot Table Fields

    You can configure the fields of your pivot table using the properties and methods of the IPivotCaches interface and IPivotTables interface.

    Refer to the following example code to configure the pivot table fields in a worksheet.

    C#
    Copy Code
    // Configuring pivot table fields
    var field_Category = pivottable.PivotFields["Category"];
    field_Category.Orientation = PivotFieldOrientation.RowField;
    
    var field_Product = pivottable.PivotFields["Product"];
    field_Product.Orientation = PivotFieldOrientation.ColumnField;
    
    var field_Amount = pivottable.PivotFields["Amount"];
    field_Amount.Orientation = PivotFieldOrientation.DataField;
    
    var field_Country = pivottable.PivotFields["Country"];
    field_Country.Orientation = PivotFieldOrientation.PageField;

    Add Field Function

    Refer to the following example code to add field function in a pivot table.

    C#
    Copy Code
    //Set field amount function
    field_Amount.Function = ConsolidationFunction.Average;

    Filter Pivot Table

    Refer to the following example code to filter a pivot table.

    C#
    Copy Code
    var field_product = pivottable.PivotFields[1];
    field_product.Orientation = PivotFieldOrientation.RowField;
    
    var field_Amount = pivottable.PivotFields[3];
    field_Amount.Orientation = PivotFieldOrientation.DataField;
    
    var field_Country = pivottable.PivotFields[5];
    field_Country.Orientation = PivotFieldOrientation.PageField;
    
    //row field filter.
    field_product.PivotItems["Apple"].Visible = false;
    field_product.PivotItems["Beans"].Visible = false;
    field_product.PivotItems["Orange"].Visible = false;
    
    //page filter.
    field_Country.PivotItems["United States"].Visible = false;
    field_Country.PivotItems["Canada"].Visible = false;

    Manage Pivot Field Level

    Refer to the following example code to manage the field level of a pivot table.

    C#
    Copy Code
    //product in level 1.
    var field_product = pivottable.PivotFields["Product"];
    field_product.Orientation = PivotFieldOrientation.RowField;
    
    //category in level 2.
    var field_category = pivottable.PivotFields["Category"];
    field_category.Orientation = PivotFieldOrientation.RowField;
    
    var field_Amount = pivottable.PivotFields[3];
    field_Amount.Orientation = PivotFieldOrientation.DataField;
    
    //category will in level 1 and product in level 2.
    field_product.Position = 1;
    field_category.Position = 0;

    Manage Grand Total Visibility Settings

    The Grand total in pivot table helps in analyzing the total sum of the data in the pivot table. You can display or hide the grand total for the row or column field by setting the visibility of ColumnGrand and RowGrand properties of the IPivotTable interface. These properties take boolean values and are set to true by default. For example, if you want to display the grand total only for rows, then set the RowGrand property to true and ColumnGrand to false.

    Refer to the following example code to manage the visibility settings of the grand total field.

    C#
    Copy Code
    // Set the PivotTable report to show grand totals for columns & rows
    pivottable.ColumnGrand = true;
    pivottable.RowGrand = true;

    Change row axis layout

    The display of pivot table can be changed to any desired layout using the LayoutRowType enumeration. The following options are provided by this enumeration:

    Note: The SubtotalLocationType enumeration can only be set to Bottom if the LayoutRowType is set to TabularRow.

    Refer to the following example code to set the row axis layout of the pivot table to TabularRow.

    C#
    Copy Code
    // Set the PivotTable LayoutRowType to Tabular Row
    pivottable.SetRowAxisLayout(LayoutRowType.TabularRow);
    

    Rename pivot table fields

    Sometimes, the pivot table fields are not easily comprehendible and hence can be renamed to meaningful and easily understandable names.

    Refer to the following example code to rename the pivot table fields.

    C#
    Copy Code
    //config pivot table's fields
    var field_Date = pivottable.PivotFields["Date"];
    field_Date.Orientation = PivotFieldOrientation.PageField;
    
    // Renaming PivotField "Category" to "Type of Category"
    var field_Category = pivottable.PivotFields["Category"];
    field_Category.Name = "Type of Category";
    field_Category.Orientation = PivotFieldOrientation.RowField;
    
    var field_Product = pivottable.PivotFields["Product"];
    field_Product.Orientation = PivotFieldOrientation.ColumnField;
    
    var field_Amount = pivottable.PivotFields["Amount"];
    field_Amount.Orientation = PivotFieldOrientation.DataField;
         
    
    var field_Country = pivottable.PivotFields["Country"];
    field_Country.Orientation = PivotFieldOrientation.RowField;
    
    // Renaming DataField "Sum of Amount" to "Amount Total"
    pivottable.DataFields[0].Name = "Amount Total";
    
    

    Refresh pivot table

    Refer to the following example code to refresh a pivot table.

    C#
    Copy Code
    var field_product = pivottable.PivotFields["Product"];
    field_product.Orientation = PivotFieldOrientation.RowField;
    
    var field_Amount = pivottable.PivotFields[3];
    field_Amount.Orientation = PivotFieldOrientation.DataField;
    
    //change pivot cache's source data.
    worksheet.Range["D8"].Value = 3000;
    
    //sync cache's data to pivot table.
    worksheet.PivotTables[0].Refresh();