Articles in this section
Category / Section

How to hide subtotals in a pivot table using C#, VB.NET?

3 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.

This article explains how to hide subtotals in a pivot table using C# and VB.NET.

Include the following namespace in the Program.cs file.

C#

using Syncfusion.XlsIO;

VB.NET

Imports Syncfusion.XlsIO

Use the following code snippet for hiding subtotals in a pivot table .

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
    IWorkbook workbook = application.Workbooks.Open(inputStream);
    IWorksheet worksheet = workbook.Worksheets[0];
    IWorksheet pivotSheet = workbook.Worksheets[1];

    //Create Pivot cache with the given data range
    IPivotCache cache = workbook.PivotCaches.Add(worksheet["A1:C9"]);

    //Create PivotTable with the cache at the specified location
    IPivotTable pivotTable = pivotSheet.PivotTables.Add("PivotTable1", pivotSheet["A1"], cache);

    //Add Pivot table field
    IPivotField regionField = pivotTable.Fields["Region"];
    regionField.Axis = PivotAxisTypes.Row;

    //Hide subtotals
    regionField.Subtotals = PivotSubtotalTypes.None;

    //Add Pivot table field
    IPivotField categoryField = pivotTable.Fields["Category"];
    categoryField.Axis = PivotAxisTypes.Row;

    //Hide subtotals
    categoryField.Subtotals = PivotSubtotalTypes.None;

    //Add data field
    IPivotField dataField = pivotTable.Fields["Sales"];
    pivotTable.DataFields.Add(dataField, "Total Sales", PivotSubtotalTypes.Sum);

    #region Save
    //Saving the workbook
    FileStream outputStream = new FileStream(Path.GetFullPath("Output.xlsx"), FileMode.Create, FileAccess.Write);
    workbook.SaveAs(outputStream);
    #endregion

    //Dispose streams
    outputStream.Dispose();
    inputStream.Dispose();
}

VB.NET

Using excelEngine As New ExcelEngine()
    Dim application As IApplication = excelEngine.Excel
    application.DefaultVersion = ExcelVersion.Xlsx
    Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")
    Dim worksheet As IWorksheet = workbook.Worksheets(0)
    Dim pivotSheet As IWorksheet = workbook.Worksheets(1)

    'Create Pivot cache with the given data range
    Dim cache As IPivotCache = workbook.PivotCaches.Add(worksheet("A1:C9"))

    'Create PivotTable with the cache at the specified location
    Dim pivotTable As IPivotTable = pivotSheet.PivotTables.Add("PivotTable1", pivotSheet("A1"), cache)

    'Add Pivot table field
    Dim regionField As IPivotField = pivotTable.Fields("Region")
    regionField.Axis = PivotAxisTypes.Row

    'Hide subtotals
    regionField.Subtotals = PivotSubtotalTypes.None

    'Add Pivot table field
    Dim categoryField As IPivotField = pivotTable.Fields("Category")
    categoryField.Axis = PivotAxisTypes.Row

    'Hide subtotals
    categoryField.Subtotals = PivotSubtotalTypes.None

    'Add Data Field
    Dim salesField As IPivotField = pivotTable.Fields("Sales")
    pivotTable.DataFields.Add(salesField, "Total Sales", PivotSubtotalTypes.Sum)

    'Saving the workbook
    workbook.SaveAs("Output.xlsx")
End Using  

You can get the complete sample for hiding subtotals in a pivot table from here.

I hope you enjoyed learning about how to hide subtotals in a PivotTable using C# and VB.NET in XlsIO.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!

Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied