You can hide or show the Grand Totals at Pivot Table level whereas the Sub Totals are managed at Pivot Field level so you need to use the following snippet for your requirements.
Java
Workbook book = new Workbook(dir + “Example.xlsx”); PivotTable table = book.getWorksheets().get(“Pivot Table”).getPivotTables().get(0); table.setRowGrand(false); //Set true if you wish to show the grand row total table.setColumnGrand(false); //Set true if you wish to show the grand column total for(int i = 0; i< table.getRowFields().getCount(); i++) { table.getRowFields().get(i).setSubtotals(PivotFieldSubtotalType.NONE,true); } book.save(dir + “output.xlsx”);
Moreover, it still shows the sub-totals rows even after using the above code snippet!
Can you please check it again at your end.
I have tested the code before pasting in my response. Moreover, you can check the resultant spreadsheet (attached here) that it does not show the sub-totals for the Pivot Table's row fields. Please note, I have tested the scenario while using the latest version of Aspose.Cells for Java 8.8.0.3 (attached), therefore if you are using any older revision of the API, please give the latest version a try.
It gives the below error if I set sub-totals for the data fields.
com.aspose.cells.CellsException: Subtotals are only valid for nondata fields
Yes, that is correct. You cannot add a sub-total for the Data Field while using Aspose.Cells APIs. If you can achieve this with Excel application then please modify your source spreadsheet with required representation and provide the spreadsheet here along with details of the manual steps you took to achieve the goal.
That said, Data Fields are actually calculated fields (Sum of Quantity or Average of Discount) therefore if you are looking to change the function of Data Field, that can be done using the PivotField.setFunction method.
The second Boolean type parameter to the PivotField.setSubtotals method allows you to control the visibility of the sub totals for the RowFields or ColumnFields. If you pass false, the sub totals will be hidden and vice versa.
Let me explain why you are not able to hide the sub totals for the row fields in your provided sample spreadsheet. Please note, the sub totals for the row fields are set to Automatic therefore you have to reset the type to AUTOMATIC while passing false as second parameter to setSubTotal method in order to hide them. As some times we are not aware of the sub total type therefore we can use the sub total type as NONE while passing true as second parameter to setSubTotal method to achieve the same result.
Please try following piece of code for verification.
Java
Workbook book = new Workbook(dir + “Example.xlsx”); PivotTable table = book.getWorksheets().get(“Pivot Table”).getPivotTables().get(0); for(int i = 0; i< table.getRowFields().getCount(); i++) { table.getRowFields().get(i).setSubtotals(PivotFieldSubtotalType.AUTOMATIC, false); } book.save(dir + “output.xlsx”);
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.