Adatta automaticamente righe e colonne

Montaggio automatico

Aspose.Cells offre un corso,Cartella di lavoro , che rappresenta un file Excel Microsoft. IlCartella di lavoro la classe contiene unFogli di lavororaccolta che consente l’accesso a ciascun foglio di lavoro nel file Excel.

Un foglio di lavoro è rappresentato daFoglio di lavoro classe. IlFoglio di lavoro fornisce un’ampia gamma di proprietà e metodi per la gestione di un foglio di lavoro. Questo articolo esamina l’utilizzo diFoglio di lavoroclass per adattare automaticamente righe o colonne.

Riga AutoFit - Semplice

L’approccio più diretto per ridimensionare automaticamente la larghezza e l’altezza di una riga consiste nel chiamare il metodoFoglio di lavoro classe'autoFitRow metodo. IlautoFitRow accetta 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.");

AutoFit Row in un intervallo di Cells

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 diautoFitRow metodo. Richiede i seguenti parametri:

  • Indice di riga, l’indice della riga che sta per essere adattata automaticamente.
  • Indice della prima colonna, l’indice della prima colonna della riga.
  • Indice dell’ultima colonna, l’indice dell’ultima colonna della riga.

IlautoFitRow 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.");

Colonna AutoFit - Semplice

Il modo più semplice per ridimensionare automaticamente la larghezza e l’altezza di una colonna consiste nel chiamare il metodoFoglio di lavoro classe'autoFitColumn metodo. Il[autoFitColumn](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#autoFitColumn(int)accetta l’indice della colonna (della colonna che sta per essere ridimensionata) 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.");

Colonna AutoFit in un intervallo di Cells

Una colonna è composta da molte righe. È possibile adattare automaticamente una colonna in base al contenuto in un intervallo di celle nella colonna chiamando una versione sovraccaricata diautoFitColumn metodo che accetta i seguenti parametri:

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

IlautoFitColumn controlla il contenuto 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.");

Adatta righe per unione Cells

Con Aspose.Cells è possibile adattare automaticamente le righe anche per le celle che sono state unite utilizzando ilAutoFitterOptions API. AutoFitterOptionsla classe fornisceAutoFitMergedCellsTypeproprietà che può essere utilizzata per adattare automaticamente le righe per le celle unite.AutoFitMergedCellsTypeaccettaAutoFitMergedCellsTypeenumerabile che ha i seguenti membri.

// 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");

Puoi anche utilizzare le versioni sovraccaricate diautoFitRows & autoFitColumns metodi che accettano un intervallo di righe/colonne e un’istanza diAutoFitterOptions per adattare automaticamente le righe/colonne selezionate con il desideratoAutoFitterOptionsdi conseguenza.

Le firme dei suddetti metodi sono le seguenti:

  1. autoFitRows(int startRow, int endRow,AutoFitterOptionsopzioni)
  2. autoFitColumns(int primaColumn, int ultimaColumn,AutoFitterOptionsopzioni)

Importante da sapere