分组工作表的行和列
Contents
[
Hide
]
可能的使用场景
Aspose.Cells允许您使用Cells.GroupRows()和Cells.GroupColumns()方法对工作表中的行和列进行分组。
分组工作表的行和列
以下示例代码演示了如何分组行和列。它将行和列分组到第三级。请查看使用此代码生成的输出Excel文件。它有两个工作表,第一个包含行的分组,第二个包含列的分组。还请参阅显示第一个工作表中行分组的截图。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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(); |