[]
        
(Showing Draft Content)

Pivot Table Layout and Format

DsExcel .NET enables you to to switch between different pivot table layout forms (Compact, Outline, Tabular), control grand total visibility, and configure advanced display settings.

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:

  • CompactRow (default layout)

  • OutlineRow

  • TabularRow

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.

// Set the PivotTable LayoutRowType to Tabular Row
pivottable.SetRowAxisLayout(LayoutRowType.TabularRow);

Change Pivot Table Layout

The different layouts of a pivot table makes it more flexible and convenient to analyse its data. DsExcel supports the following pivot table layouts:

  • Compact form

  • Outline form

  • Tabular form

In addition to these, you can also choose to insert blank rows, set the position of subtotals, show all items or to repeat any item in the pivot table layouts.

Refer to the following example code to set the layout of pivot table and additional options.

//set pivot table layout
field_Category.LayoutForm = LayoutFormType.Tabular;
field_Category.LayoutBlankLine = true;

field_Country.LayoutForm = LayoutFormType.Outline;
field_Country.LayoutCompactRow = false;

//set subtotal location
field_Country.LayoutSubtotalLocation = SubtotalLocationType.Bottom;
field_Country.ShowAllItems = true;

Auto-Fit Column Width

DsExcel allows you to control whether to auto-fit column width using HasAutoFormat property of IPivotTable interface. Refer to the following example code to auto-fit column width:

/* Auto-fit the column width when refreshed.
   It is recommended to use it together with DeferLayoutUpdate
   due to the extensive text length measurements performed by autofit. */
pivottable.DeferLayoutUpdate = true;
pivottable.HasAutoFormat = true;

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.

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

Use Pivot Table Options

DsExcel supports the following layout and formatting options in a pivot table:

  • Merging cells with outer-row item, column item, subtotal and grand total labels

  • Indentation of Pivot table items when compact row layout form is set

  • Ordering page fields in pivot table layout. It can be either DownThenOver (default value) or OverThenDown.

  • Defining number of page fields in each column or row in the pivot table output

  • Displaying custom string in cells which contain errors

  • Displaying custom string in cells which contain null values

Refer to the following example code to set various layout and format options in a pivot table.

//set layout and format options
pivottable.PageFieldOrder = Order.OverThenDown;
pivottable.PageFieldWrapCount = 2;

pivottable.CompactRowIndent = 2;

pivottable.ErrorString = "Error";
pivottable.NullString = "Empty";

pivottable.DisplayErrorString = true;
pivottable.DisplayNullString = true;

Refresh Pivot Table

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

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();

DsExcel also allows you to control whether to automatically refresh the pivot cache when opening a file in Excel using RefreshOnFileOpen property of IPivotCache interface. Refer to the following example code to enable automatic refresh of the pivot cache:

// Automatically refresh the saved file when opening with Excel.
pivotcache.RefreshOnFileOpen = true;

Defer Layout Update

In case of huge amount of data, the performance of a pivot table might get affected while updating its layout by adding or moving fields in the different areas of a pivot table.

DsExcel provides DeferLayoutUpdate property which improves the performance of a pivot table by deferring its layout updates. When set to true, the pivot table is recalculated only after all the fields are added or moved instead of getting recalculated after each change. You can choose to update the pivot table output after making all the changes by calling the Update method.

Refer to the following example code to defer layout updates to a pivot table.

//defer layout update
pivottable.DeferLayoutUpdate = true;

//config pivot table's 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;

//must update the pivottable
pivottable.Update();

Disable Automatic Grouping of Date/Time Columns

The Date/Time columns in a pivot table are grouped together by default. DsExcel allows you to disable this grouping by setting AutomaticGroupDateTimeInPivotTable property to false before creating the pivot cache while creating a pivot table.

When AutomaticGroupDateTimeInPivotTable = False

When AutomaticGroupDateTimeInPivotTable = True (default)



Refer to the following example code to disable automatic grouping of date/time columns.

// Set false to group date/time fields in PivotTable automatically
workbook.Options.Data.AutomaticGroupDateTimeInPivotTable = false;

Show/Hide Pivot Table Field List

DsExcel allows you to control whether to show/hide PivotTable field list using ShowPivotTableFieldList property of IWorkbook interface. Refer to the following example code to hide the pivot field list when opening the file in Excel:

// Hide the Pivot Field List when opening the file in Excel.
workbook.ShowPivotTableFieldList = false;

Show/Hide Pivot Table Field Headers

You can control the visibility of field headers (row and column field captions) in a PivotTable by using the DisplayFieldCaptions property of the IPivotTable interface in DsExcel. By default, DisplayFieldCaptions is set to true.

When DisplayFieldCaptions is set to false:

  • If a row contains only row/column field labels, the entire row is removed and the pivot table range shrinks accordingly.

  • If a row contains both row/column field labels and other content (such as value labels), only the field label cells are hidden. The pivot table row count remains unchanged.

The table below shows the effect of changing the DisplayFieldCaptions property:

Row Type

DisplayFieldCaptions = true

DisplayFieldCaptions = false

Only field labels

image

image

Field labels and other data

image

image

Refer to the following example code to hide the field headers in a PivotTable:

pivottable.DisplayFieldCaptions = false;

Set Conditional Formatting

Refer to the following example code to set conditional formatting in last row of a pivot table report by setting cell color when the values are above average.

// set condional format to the last row
int rowCount = pivottable.DataBodyRange.RowCount;
IAboveAverage averageCondition = pivottable.DataBodyRange.Rows[rowCount - 1].FormatConditions.AddAboveAverage();
averageCondition.AboveBelow = AboveBelow.AboveAverage;
averageCondition.Interior.Color = Color.Pink;

Note: Conditional formatting applied to a pivot table is lost if the pivot table is changed in any way.