Regolazione dell altezza della riga e della larghezza della colonna
Lavorare con le righe
Regolazione dell’altezza della riga
Aspose.Cells fornisce una classe, Workbook, che rappresenta un file Microsoft Excel. La classe Workbook contiene una WorksheetCollection 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 di Cells che rappresenta tutte le celle nel foglio di lavoro.
La raccolta Cells fornisce diversi metodi per gestire righe o colonne in un foglio di lavoro. Alcuni di questi verranno discussi più dettagliatamente di seguito.
Impostazione dell’altezza della riga
È possibile impostare l’altezza di una singola riga chiamando il metodo setRowHeight della raccolta Cells. Il metodo setRowHeight richiede i seguenti parametri:
- Indice di riga, l’indice della riga a cui si sta modificando l’altezza.
- Altezza della riga, l’altezza della riga da applicare alla 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(SettingHeightOfRow.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); | |
Cells cells = worksheet.getCells(); | |
// Setting the height of the second row to 13 | |
cells.setRowHeight(1, 13); | |
// Saving the modified Excel file in default (that is Excel 2003) format | |
workbook.save(dataDir + "SettingHeightOfRow_out.xls"); |
Impostare l’altezza di tutte le righe
Per impostare la stessa altezza di riga per tutte le righe in un foglio di lavoro, utilizzare il metodo setStandardHeight della raccolta Cells.
// 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(SettingHeightAllRows.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); | |
Cells cells = worksheet.getCells(); | |
// Setting the height of all rows in the worksheet to 15 | |
worksheet.getCells().setStandardHeight(15f); | |
// Saving the modified Excel file in default (that is Excel 2003) format | |
workbook.save(dataDir + "SettingHeightAllRows_out.xls"); |
Lavorare con colonne
Impostazione della larghezza di una colonna
Impostare la larghezza di una colonna chiamando il metodo setColumnWidth della raccolta Cells. Il metodo setColumnWidth richiede i seguenti parametri:
- Indice di colonna, l’indice della colonna a cui si sta modificando la larghezza.
- Larghezza di colonna, la larghezza desiderata della 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(SettingWidthOfColumn.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); | |
Cells cells = worksheet.getCells(); | |
// Setting the width of the second column to 17.5 | |
cells.setColumnWidth(1, 17.5); | |
// Saving the modified Excel file in default (that is Excel 2003) format | |
workbook.save(dataDir + "SettingWidthOfColumn_out.xls"); |
Impostare la larghezza di tutte le colonne
Per impostare la stessa larghezza di colonna per tutte le colonne in un foglio di lavoro, utilizzare il metodo setStandardWidth della raccolta Cells.
// 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(SettingWidthOfAllColumns.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); | |
Cells cells = worksheet.getCells(); | |
// Setting the width of all columns in the worksheet to 20.5 | |
worksheet.getCells().setStandardWidth(20.5f); | |
// Saving the modified Excel file in default (that is Excel 2003) format | |
workbook.save(dataDir + "SettingWidthOfAllColumns_out.xls"); |