Creating Subtotals in Excel using Java

This short tutorial describes how to add subtotal in Excel using Java. You will get information to set the development environment, a list of programming steps, and a sample code for creating subtotals in Excel. Further details will be discussed to enhance the code and create custom output while adding subtotal to an Excel file.

Steps to Create Automatic Subtotals using Java

  1. Customize the IDE for using Aspose.Cells for Java to create a subtotal in Excel
  2. Create a Workbook object and get a reference to the first Worksheet for adding data in it
  3. Add the header labels in the first row of the worksheet
  4. Set the sample data in a 2D array grouped by the region, and fill the worksheet with the 2D data
  5. Set the data range, call the subtotal API for grouping, and calculate the sum
  6. Set the option for the outline to display the subtotal rows below the related detail rows
  7. For displaying the subtotal rows only, collapse the grouped detail rows for all the worksheet rows
  8. Auto-fit the columns and save the Workbook on the disk

These steps describe how to create subtotal in Excel using Java. Create a workbook object, access the first worksheet, add sample headers, create sample grouped data, and insert the data into the sheet below the respective headers. Define the range and the header row, apply a subtotal, set the subtotal display below the detail rows, collapse the outline levels to customize the resultant Excel file, and save the workbook on the disk.

Code to Create Automatic Subtotals in Excel using Java

This code has demonstrated how to make subtotal in Excel using Java. You can sort the data by group column before the subtotal, set the dynamic range, and opt for various subtotal options such as using the overload with replace, pageBreaks, and summary below data control the subtotal behavior. Multiple subtotal columns can also be added in once cell, and different consolidation function may be utilized say, COUNT, AVERAGE, and MAX, wherever required.

This article has guided us to work with the subtotals in an Excel file. To link a slicer with multiple pivot tables, refer to the article Link slicer to multiple pivot tables using Java.