行と列のグループ化および展開
紹介
Microsoft Excelファイルでは、データの概要を作成して、1回のマウスクリックで詳細のレベルを表示したり非表示にしたりできます。
アウトライン記号の1、2、3、+、および-をクリックして、ワークシートのセクションの要約または見出しを迅速に表示したり、個々の要約または見出しの詳細を表示する際に使用できます。下の図で示されているように、個々の要約または見出しの詳細を表示するためにシンボルを使用できます。
行と列のグループ化
行と列のグループ管理
Aspose.Cellsは、Microsoft Excelファイルを表すWorkbook クラスを提供しています。Workbook クラスには、Excelファイル内の各ワークシートにアクセスする機能を提供するWorksheets コレクションが含まれています。ワークシートはWorksheet クラスによって表されます。Worksheet クラスは、ワークシート内のすべてのセルを表すCells コレクションを提供します。
Cells コレクションには、ワークシート内の行または列を管理するための複数のメソッドが提供されています。これについての詳細は、以下でさらに説明します。
行と列のグループ化
Cells コレクションを使用して、groupRows およびgroupColumns メソッドを呼び出すことで、行または列をグループ化することが可能です。両メソッドは以下のパラメータを取ります:
- 最初の行/列インデックス、グループ内の最初の行または列
- グループ内の最後の行/列のインデックス、最後の行または列。
- 非表示かどうか、グループ化後に行または列を非表示にするかどうかを指定するブールパラメータ。
// 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プロパティを使用して、これらのグループ設定を構成することが可能です。
詳細の下の要約行
開発者は、OutlineクラスのSummaryRowBelowメソッドを使用して、詳細の下に要約行を表示するかを制御することができます。
// 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"); |
詳細の右側にサマリー列
開発者は、OutlineクラスのSummaryColumnRightメソッドを使用して、要約列が詳細の右側に表示されるかどうかを制御することができます。
// 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コレクションのUngroupRowsおよびUngroupColumnsメソッドを呼び出すことで解除することができます。両方のメソッドは同じパラメータを使用します:
- 最初の行または列のインデックス、グループ化を解除する最初の行/列。
- 最後の行または列のインデックス、グループ化を解除する最後の行/列。
// 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."); |