对工作表的行和列进行分组
Contents
[
Hide
]
可能的使用场景
Aspose.Cells 允许您使用以下方法对工作表中的行和列进行分组ICells.GroupRows()和ICells.GroupColumns()方法。
对工作表的行和列进行分组
以下示例代码显示了如何对行和列进行分组。它将行和列分组到第 3 级。请检查输出excel文件使用此代码生成。它有两个工作表,第一个包含行分组,第二个包含列分组。另请参阅显示第一个工作表中行分组的屏幕截图。
示例代码
This file contains hidden or 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 | |
//Output directory path | |
StringPtr outPath = new String("..\\Data\\Output\\"); | |
//Path of output excel file | |
StringPtr outputGroupRowsAndColumnsOfWorksheet = outPath->StringAppend(new String("outputGroupRowsAndColumnsOfWorksheet.xlsx")); | |
//Create an empty workbook | |
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(); | |
//Add worksheet for grouping rows | |
intrusive_ptr<IWorksheet> grpRows = wb->GetIWorksheets()->GetObjectByIndex(0); | |
grpRows->SetName(new String("GroupRows")); | |
//Add worksheet for grouping columns | |
int idx = wb->GetIWorksheets()->Add(); | |
intrusive_ptr<IWorksheet> grpCols = wb->GetIWorksheets()->GetObjectByIndex(idx); | |
grpCols->SetName(new String("GroupColumns")); | |
//Add sample values in both worksheets | |
for (int i = 0; i<50; i++) | |
{ | |
intrusive_ptr<String> str = new String("Text"); | |
grpRows->GetICells()->GetObjectByIndex(i, 0)->PutValue(str); | |
grpCols->GetICells()->GetObjectByIndex(0, i)->PutValue(str); | |
} | |
//Grouping rows at first level | |
grpRows->GetICells()->GroupRows(0, 10); | |
grpRows->GetICells()->GroupRows(12, 22); | |
grpRows->GetICells()->GroupRows(24, 34); | |
//Grouping rows at second level | |
grpRows->GetICells()->GroupRows(2, 8); | |
grpRows->GetICells()->GroupRows(14, 20); | |
grpRows->GetICells()->GroupRows(28, 30); | |
//Grouping rows at third level | |
grpRows->GetICells()->GroupRows(5, 7); | |
//Grouping columns at first level | |
grpCols->GetICells()->GroupColumns(0, 10); | |
grpCols->GetICells()->GroupColumns(12, 22); | |
grpCols->GetICells()->GroupColumns(24, 34); | |
//Grouping columns at second level | |
grpCols->GetICells()->GroupColumns(2, 8); | |
grpCols->GetICells()->GroupColumns(14, 20); | |
grpCols->GetICells()->GroupColumns(28, 30); | |
//Grouping columns at third level | |
grpCols->GetICells()->GroupColumns(5, 7); | |
//Save the output excel file | |
wb->Save(outputGroupRowsAndColumnsOfWorksheet); |