Excel Group is used to classify data information with categories. If information in a workbook has been divided into several parts, we can form each part as a group. For example, each product information will be set as a group in a sales report.
Generally speaking, group is created by rows or columns. Therefore, it is very important to confirm data range before creating group. Also, data in one group can be hide or display. After setting, we can find a “-” symbol and the group data is presented. Click it. It will become “+” and the data is hidden.
In this post, I will show the method about how to create Excel group according to data information by using C#.
In my example, I have an Excel file which saves personal information of vip customers. I will separate information from two groups based on rows and columns. The following steps show details.
And, Spire.XLS, a component to operate Excel for .Net, so I add its dll file as reference in project.
- Load file which I want to create group.
- Initialize worksheet. In order to make the appearance be more wonderful, set the grid lines as invisible.
- Create Groups. There are two methods, GroupByRows, GroupByColumns. For each method, we need to pass three parameters: first row/column, last row/column and a bool value which defines if the group will be folded.
- Save and Launch file.
static void Main(string args)
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets;
sheet.GridLinesVisible = false;
//Set Group by Rows and Columns
sheet.GroupByRows(1, 10, false);
sheet.GroupByColumns(1, 5, false);
//Save and Launch File
Result Shown by Following:
Click Here to LEARN MORE about Spire.XLS
Click Here to DOWNLOAD Spire.XLS