行と列のグループ化および展開
紹介
Microsoft Excelファイルでは、データの概要を作成して、1回のマウスクリックで詳細のレベルを表示したり非表示にしたりできます。
アウトライン記号の1、2、3、+、および-をクリックして、ワークシートのセクションの要約または見出しを迅速に表示したり、個々の要約または見出しの詳細を表示する際に使用できます。下の図で示されているように、個々の要約または見出しの詳細を表示するためにシンボルを使用できます。
行と列のグループ化 |
---|
![]() |
行と列のグループ管理
Aspose.CellsはMicrosoft Excelファイルを表すWorkbookクラスを提供しており、WorkbookクラスにはExcelファイル内の各ワークシートにアクセスするためのWorksheetCollectionが含まれています。ワークシートはWorksheetクラスによって表されます。Worksheetクラスはワークシート内のすべてのセルを表すCellsコレクションを提供します。
Cellsコレクションには、ワークシート内の行や列を管理するためのいくつかのメソッドが提供されており、そのうちいくつかについて以下で詳しく説明します。
行と列のグループ化
行と列をグループ化するには、CellsコレクションのGroupRowsおよびGroupColumnsメソッドを呼び出すことができます。両方のメソッドは以下のパラメーターを受け取ります:
- 最初の行/列インデックス、グループ内の最初の行または列
- グループ内の最後の行/列のインデックス、最後の行または列。
- 非表示かどうか、グループ化後に行または列を非表示にするかどうかを指定するブールパラメータ。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Grouping first six rows (from 0 to 5) and making them hidden by passing true | |
worksheet.Cells.GroupRows(0, 5, true); | |
// Grouping first three columns (from 0 to 2) and making them hidden by passing true | |
worksheet.Cells.GroupColumns(0, 2, true); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
グループ設定
Microsoft Excelでは、以下を表示するためのグループ設定を構成できます:
- 詳細の下の要約行。
- 詳細の右側の要約列。
開発者は、WorksheetクラスのOutlineプロパティを使用して、これらのグループ設定を構成できます。
詳細の下にサマリー行
サマリー行が詳細の下に表示されるかどうかは、OutlineクラスのSummaryRowBelowプロパティをtrueまたはfalseに設定することで制御できます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
Workbook workbook = new Workbook(dataDir + "sample.xlsx"); | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Grouping first six rows and first three columns | |
worksheet.Cells.GroupRows(0, 5, true); | |
worksheet.Cells.GroupColumns(0, 2, true); | |
// Setting SummaryRowBelow property to false | |
worksheet.Outline.SummaryRowBelow = false; | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xls"); |
詳細の右側にサマリー列
開発者は、OutlineクラスのSummaryColumnRightプロパティをtrueまたはfalseに設定することで、詳細の右側にサマリー列を表示することもできます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
Workbook workbook = new Workbook(dataDir + "sample.xlsx"); | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Grouping first six rows and first three columns | |
worksheet.Cells.GroupRows(0, 5, true); | |
worksheet.Cells.GroupColumns(0, 2, true); | |
worksheet.Outline.SummaryColumnRight = true; | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xls"); |
行と列のグループ解除
グループ化された行または列を解除するには、{1}のコレクションの{2}および{3}メソッドを呼び出します。どちらのメソッドも2つのパラメーターを取ります。
- 最初の行または列のインデックス、グループ化を解除する最初の行/列。
- 最後の行または列のインデックス、グループ化を解除する最後の行/列。
UngroupRowsには、ブール値の第三パラメーターを取るオーバーロードがあります。これをtrueに設定すると、グループ化された情報がすべて削除されます。それ以外の場合は、外部グループ化情報のみが削除されます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Ungrouping first six rows (from 0 to 5) | |
worksheet.Cells.UngroupRows(0, 5); | |
// Ungrouping first three columns (from 0 to 2) | |
worksheet.Cells.UngroupColumns(0, 2); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |