Raggruppamento e Sgrovigliamento di Righe e Colonne

Introduzione

In un file Microsoft Excel, è possibile creare un’outline per i dati che consente di mostrare e nascondere livelli di dettaglio con un singolo clic del mouse.

Fare clic sui Simboli di Riepilogo, 1,2,3, + e - per visualizzare rapidamente solo le righe o colonne che forniscono riepiloghi o intestazioni per sezioni in un foglio di lavoro, oppure è possibile utilizzare i simboli per vedere i dettagli sotto un riepilogo o intestazione individuale come mostrato di seguito nella figura:

Raggruppamento di righe e colonne

todo:image_alt_text

Gestione raggruppata di righe e colonne

Aspose.Cells fornisce una classe, Workbook che rappresenta un file Microsoft Excel. La classe Workbook contiene una raccolta Worksheets che consente di accedere a ciascun foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una raccolta Cells che rappresenta tutte le celle nel foglio di lavoro.

La raccolta Cells fornisce diversi metodi per gestire le righe o colonne in un foglio di lavoro, alcuni di questi sono discussi di seguito in modo più dettagliato.

Raggruppamento di righe e colonne

È possibile raggruppare righe o colonne chiamando i metodi groupRows e groupColumns della raccolta Cells. Entrambi i metodi richiedono i seguenti parametri:

  • Indice della prima riga/colonna, la prima riga o colonna nel gruppo.
  • Indice dell’ultima riga/colonna, l’ultima riga o colonna nel gruppo.
  • È nascosto, un parametro booleano che specifica se nascondere o meno righe/colonne dopo il raggruppamento.
// 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");

Impostazioni di raggruppamento

Anche Microsoft Excel consente di configurare le impostazioni di raggruppamento per la visualizzazione:

  • Le righe di riassunto sotto il dettaglio.
  • Colonnesommario a destra dei dettagli.

Impostazioni di raggruppamento

todo:image_alt_text

È possibile configurare queste impostazioni di raggruppamento utilizzando la proprietà Outline della classe Worksheet.

Righe di riepilogo sotto i dettagli

Gli sviluppatori possono controllare la visualizzazione delle righe di riepilogo sotto i dettagli utilizzando il metodo SummaryRowBelow della classe 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");

Colonne sommario a destra dei dettagli

È possibile controllare se le colonne riepilogo sono visualizzate a destra dei dettagli con il metodo SummaryColumnRight della classe 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(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");

Sganciare Righe e Colonne

Annullare il raggruppamento delle righe o colonne raggruppate chiamando i metodi UngroupRows e UngroupColumns della raccolta Cells. Entrambi i metodi richiedono gli stessi parametri:

  • Indice della prima riga o colonna, la prima riga/colonna da sraggruppare.
  • Indice dell’ultima riga o colonna, l’ultima riga/colonna da sraggruppare.
// 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.");