分组和取消分组行和列

介绍

在 Microsoft Excel 文件中,您可以为数据创建一个大纲,以便通过单击鼠标来显示和隐藏详细信息级别。

点击大纲符号、1、2、3、+ 和 - 快速仅显示工作表中为各部分提供摘要或标题的行或列,或者您可以使用符号查看单个摘要或标题下的详细信息,如下图所示:

行和列的分组

待办事项:图片_替代_文本

行列分组管理

Aspose.Cells提供了一个类,工作簿表示 Microsoft Excel 文件。这工作簿类包含一个工作表允许访问 Excel 文件中每个工作表的集合。工作表由工作表班级。这工作表类提供了Cells代表工作表中所有单元格的集合。

Cellscollection 提供了多种方法来管理工作表中的行或列,下面将详细讨论其中的一些方法。

分组行和列

可以通过调用[组行](https://reference.aspose.com/cells/java/com.aspose.cells/cells#groupRows(int,%20int,%20boolean)) 和[组列](https://reference.aspose.com/cells/java/com.aspose.cells/cells#groupColumns(int,%20int,%20boolean) 的方法Cells收藏。这两种方法都采用以下参数:

  • 第一行/列索引,组中的第一行或第一列。
  • 最后一行/列索引,组中的最后一行或最后一列。
  • is hidden,布尔型参数,指定分组后是否隐藏行/列。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(GroupingRowsandColumns.class) + "RowsAndColumns/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Grouping first six rows (from 0 to 5) and making them hidden by
// passing true
cells.groupRows(0, 5, true);
// Grouping first three columns (from 0 to 2) and making them hidden by
// passing true
cells.groupColumns(0, 2, true);
// Setting SummaryRowBelow property to false
worksheet.getOutline().setSummaryRowBelow(true);
// Setting SummaryColumnRight property to false
worksheet.getOutline().setSummaryColumnRight(true);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "GroupingRowsandColumns_out.xlsx");

群组设置

Microsoft Excel 还允许配置用于显示的组设置:

  • 详细信息下方的汇总行。
  • 详细信息右侧的摘要列。

群组设置

待办事项:图片_替代_文本

可以使用 Worksheet 类的 Outline 属性配置这些组设置。

详细信息下方的汇总行

开发人员可以控制在详细信息下方显示摘要行,方法是使用大纲班级'汇总行下方方法。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SummaryRowBelow.class) + "RowsAndColumns/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Grouping first six rows (from 0 to 5) and making them hidden by passing true
cells.groupRows(0, 5, true);
// Grouping first three columns (from 0 to 2) and making them hidden by passing true
cells.groupColumns(0, 2, true);
// Setting SummaryRowBelow property to false
worksheet.getOutline().setSummaryRowBelow(false);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "SummaryRowBelow_out.xls");

详细信息右侧的摘要列

可以控制摘要列是否显示在详细信息的右侧大纲班级'摘要列右方法。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SummaryRowRight.class) + "RowsAndColumns/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "BookStyles.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Grouping first six rows (from 0 to 5) and making them hidden by passing true
cells.ungroupRows(0, 5);
// Grouping first three columns (from 0 to 2) and making them hidden by passing true
cells.ungroupColumns(0, 2);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "SummaryRowRight_out.xls");

取消分组行和列

通过调用Cells收藏的[解组行](https://reference.aspose.com/cells/java/com.aspose.cells/cells#ungroupRows(int,%20int)) 和[取消组合列](https://reference.aspose.com/cells/java/com.aspose.cells/cells#ungroupColumns(int,%20int)) 方法。两种方法都采用相同的参数:

  • 第一行或第一列索引,要取消分组的第一行/列。
  • 最后一行或最后一列索引,要取消分组的最后一行/列。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(UngroupingRowsandColumns.class) + "rows_cloumns/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "BookStyles.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Grouping first six rows (from 0 to 5) and making them hidden by
// passing true
cells.ungroupRows(0, 5);
// Grouping first three columns (from 0 to 2) and making them hidden by
// passing true
cells.ungroupColumns(0, 2);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "UngroupingRowsandColumns_out.xls");
// Print message
System.out.println("Rows and Columns ungrouped successfully.");