Group Rows and Columns of Worksheet

Possible Usage Scenarios

Aspose.Cells allows you to group rows and columns in a worksheet using Cells.GroupRows() and Cells.GroupColumns() methods.

Group Rows and Columns of Worksheet

The following sample code shows how to group rows and columns. It groups the rows and columns up to the 3rd level. Please check the output excel file generated with this code. It has two worksheets, the first one contains the grouping of rows and the second one contains the grouping of columns. Please also see the screenshot showing the grouping of rows in its first worksheet.

todo:image_alt_text

Sample Code

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
Aspose::Cells::Startup();
//Output directory path
U16String outPath(u"..\\Data\\Output\\");
//Path of output excel file
U16String outputGroupRowsAndColumnsOfWorksheet = outPath + u"outputGroupRowsAndColumnsOfWorksheet.xlsx";
//Create an empty workbook
Workbook wb;
//Add worksheet for grouping rows
Worksheet grpRows = wb.GetWorksheets().Get(0);
grpRows.SetName(u"GroupRows");
//Add worksheet for grouping columns
int idx = wb.GetWorksheets().Add();
Worksheet grpCols = wb.GetWorksheets().Get(idx);
grpCols.SetName(u"GroupColumns");
//Add sample values in both worksheets
for (int i = 0; i < 50; i++)
{
U16String str(u"Text");
grpRows.GetCells().Get(i, 0).PutValue(str);
grpCols.GetCells().Get(0, i).PutValue(str);
}
//Grouping rows at first level
grpRows.GetCells().GroupRows(0, 10);
grpRows.GetCells().GroupRows(12, 22);
grpRows.GetCells().GroupRows(24, 34);
//Grouping rows at second level
grpRows.GetCells().GroupRows(2, 8);
grpRows.GetCells().GroupRows(14, 20);
grpRows.GetCells().GroupRows(28, 30);
//Grouping rows at third level
grpRows.GetCells().GroupRows(5, 7);
//Grouping columns at first level
grpCols.GetCells().GroupColumns(0, 10);
grpCols.GetCells().GroupColumns(12, 22);
grpCols.GetCells().GroupColumns(24, 34);
//Grouping columns at second level
grpCols.GetCells().GroupColumns(2, 8);
grpCols.GetCells().GroupColumns(14, 20);
grpCols.GetCells().GroupColumns(28, 30);
//Grouping columns at third level
grpCols.GetCells().GroupColumns(5, 7);
//Save the output excel file
wb.Save(outputGroupRowsAndColumnsOfWorksheet);
Aspose::Cells::Cleanup();