This short tutorial guides on how to add subtotal in Excel using C#. You will get details to set the IDE, a list of steps, and a sample code for creating subtotals in Excel using C#. All the details will be shared to customize the Excel file for outlining the grouped data before saving the Excel file.
Steps to Create Automatic Subtotals using C#
- Establish the environment to use Aspose.Cells for .NET to add a subtotal
- Set the license to use the library without the evaluation limits
- Create the workbook object and access the first sheet in it
- Add labels for the headers in the first row
- Set data in the rows using the array data
- Define the range of data by excluding the header row
- Use the Subtotal() method to group data by Region and sum Amount
- Iterate through all the outline levels to collapse the grouped rows
- Save the file on the disk with data and subtotals in it
The above steps summarize how to make subtotal in Excel using C#. Instantiate a Workbook, access the first sheet, add headers in it, insert some sample data, define the range of data, and apply a subtotal by grouping it by Region on the Amount column. Set the online behavior, add the summary row, collapse the data, and auto-fit the columns before saving the Excel file.
Code to Create Automatic Subtotals in Excel using C#
This code demonstrates how to create subtotals in Excel using C#. You can customize the output by sorting the group columns before calling the Subtotal function, add multiple subtotal functions such as Sum and Average, and use SubtotalSetting to display the summary below or to the right of the data. Styles can also be set for subtotal rows like setting it bold or filling the color.
This article has taught us the process to add a subtotal in the Excel file. For connecting a slicer to multiple pivot tables, refer to the article Connect slicer to multiple pivot tables using C#.