Adatta automaticamente righe e colonne

Adattamento automatico

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 vasta gamma di proprietà e metodi per la gestione di un foglio di lavoro. Questo articolo esamina l’uso della classe Worksheet per adattare automaticamente righe o colonne.

Adatta automaticamente la riga - Semplice

Il metodo più semplice per adattare automaticamente la larghezza e l’altezza di una riga è chiamare il metodo autoFitRow della classe Worksheet. Il metodo autoFitRow richiede un indice di riga (della riga da ridimensionare) come parametro.

// 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(AutoFitRowsandColumns.class) + "rows_cloumns/";
// 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);
// Auto-fitting the 2nd row of the worksheet
worksheet.autoFitRow(1);
// Auto-fitting the 1st column of the worksheet
worksheet.autoFitColumn(0);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "AutoFitRowsandColumns_out.xls");
// Print message
System.out.println("Row and Column auto fit successfully.");

Adatta automaticamente la riga in un intervallo di celle

Una riga è composta da molte colonne. Aspose.Cells consente agli sviluppatori di adattare automaticamente una riga in base al contenuto in un intervallo di celle all’interno della riga chiamando una versione sovraccaricata del metodo autoFitRow. Richiede i seguenti parametri:

  • Indice riga, l’indice della riga da adattare automaticamente.
  • Primo indice colonna, l’indice della prima colonna della riga.
  • Ultimo indice colonna, l’indice dell’ultima colonna della riga.

Il metodo autoFitRow controlla il contenuto di tutte le colonne nella riga e quindi adatta automaticamente la riga.

// 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(AutoFitRowsinaRangeofCells.class) + "rows_cloumns/";
// 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);
// Auto-fitting the row of the worksheet
worksheet.autoFitRow(1, 0, 5);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "AutoFitRowsinaRangeofCells_out.xls");
// Print message
System.out.println("Row auto fit successfully.");

Adatta automaticamente la colonna - Semplice

Il modo più semplice per ridimensionare automaticamente la larghezza e l’altezza di una colonna è chiamare il metodo autoFitColumn della classe Worksheet. Il metodo autoFitColumn richiede l’indice di colonna (della colonna da ridimensionare) come parametro.

// 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(AutoFitRowsandColumns.class) + "rows_cloumns/";
// 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);
// Auto-fitting the 2nd row of the worksheet
worksheet.autoFitRow(1);
// Auto-fitting the 1st column of the worksheet
worksheet.autoFitColumn(0);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "AutoFitRowsandColumns_out.xls");
// Print message
System.out.println("Row and Column auto fit successfully.");

Adatta automaticamente la colonna in un intervallo di celle

Una colonna è composta da molte righe. È possibile adattare automaticamente una colonna in base ai contenuti in un intervallo di celle nella colonna chiamando una versione sovraccaricata del metodo autoFitColumn che richiede i seguenti parametri:

  • Indice della colonna, rappresenta l’indice della colonna i cui contenuti devono essere adattati automaticamente
  • Indice della prima riga, rappresenta l’indice della prima riga della colonna
  • Indice dell’ultima riga, rappresenta l’indice dell’ultima riga della colonna

Il metodo autoFitColumn controlla i contenuti di tutte le righe nella colonna e quindi adatta automaticamente la colonna.

// 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(AutoFitColumnsinaRangeofCells.class) + "rows_cloumns/";
// 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);
// Auto-fitting the Column of the worksheet
worksheet.autoFitColumn(4, 4, 6);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "AutoFitColumnsinaRangeofCells_out.xls");
// Print message
System.out.println("Columns auto fit successfully.");

Adattare automaticamente le righe per le celle unite

Con Aspose.Cells è possibile adattare automaticamente le righe anche per le celle che sono state unite utilizzando l’API AutoFitterOptions. La classe AutoFitterOptions fornisce la proprietà AutoFitMergedCellsType che può essere utilizzata per adattare automaticamente le righe per le celle unite. AutoFitMergedCellsType accetta un’enumerazione AutoFitMergedCellsType che ha i seguenti membri.

  • NESSUNO: Ignora le celle unite.
  • PRIMA_RIGA: Espande solo l’altezza della prima riga.
  • ULTIMA_RIGA: Espande solo l’altezza dell’ultima riga.
  • OGNI_RIGA: Espande solo l’altezza di ogni riga.
// 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(AutofitRowsforMergedCells.class) + "RowsAndColumns/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Create a range A1:B1
Range range = worksheet.getCells().createRange(0, 0, 1, 2);
// Merge the cells
range.merge();
// Insert value to the merged cell A1
worksheet.getCells().get(0, 0).setValue("A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end");
// Create a style object
Style style = worksheet.getCells().get(0, 0).getStyle();
// Set wrapping text on
style.setTextWrapped(true);
// Apply the style to the cell
worksheet.getCells().get(0, 0).setStyle(style);
// Create an object for AutoFitterOptions
AutoFitterOptions options = new AutoFitterOptions();
// Set auto-fit for merged cells
options.setAutoFitMergedCellsType(AutoFitMergedCellsType.EACH_LINE);
// Autofit rows in the sheet(including the merged cells)
worksheet.autoFitRows(options);
// Save the Excel file
workbook.save(dataDir + "AutofitRowsforMergedCells_out.xlsx");

È anche possibile utilizzare le versioni sovraccaricate di autoFitRows e autoFitColumns che accettano un intervallo di righe/colonne e un’istanza di AutoFitterOptions per adattare automaticamente le righe/colonne selezionate con le AutoFitterOptions desiderate di conseguenza.

Le firme dei suddetti metodi sono le seguenti:

  1. autoFitRows(int startRow, int endRow, AutoFitterOptions options)
  2. autoFitColumns(int firstColumn, int lastColumn, AutoFitterOptions options)

Importante sapere