Ajustement de la hauteur des lignes et de la largeur des colonnes

Travailler avec des lignes

Réglage de la hauteur de rangée

Aspose.Cells fournit une classe,Cahier , qui représente un fichier Excel Microsoft. LeCahier classe contient unWorksheetCollectionqui permet d’accéder à chaque feuille de calcul dans le fichier Excel. Une feuille de calcul est représentée par leFeuille de travail classe. LeFeuille de travail la classe offre uneCellscollection qui représente toutes les cellules de la feuille de calcul.

LeCellscollection fournit plusieurs méthodes pour gérer les lignes ou les colonnes dans une feuille de calcul. Certains d’entre eux sont discutés ci-dessous plus en détail.

Définition de la hauteur de ligne

Il est possible de définir la hauteur d’une seule ligne en appelant leCells de la collectionsetRowHeight méthode. LesetRowHeight prend les paramètres suivants :

  • Indice de ligne, l’index de la ligne dont vous modifiez la hauteur.
  • Hauteur de ligne, la hauteur de ligne à appliquer sur la ligne.
// 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");

Définition de la hauteur de toutes les lignes

Pour définir la même hauteur de ligne pour toutes les lignes d’une feuille de calcul, utilisez laCells de la collectionsetStandardHeightméthode.

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

Travailler avec des colonnes

Définition de la largeur d’une colonne

Définissez la largeur d’une colonne en appelant laCells de la collectionsetColumnWidth méthode. LesetColumnWidth prend les paramètres suivants :

  • Indice de colonne, l’index de la colonne dont vous modifiez la largeur.
  • Largeur de colonne, la largeur de colonne souhaitée.
// 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");

Définition de la largeur de toutes les colonnes

Pour définir la même largeur de colonne pour toutes les colonnes d’une feuille de calcul, utilisez laCells de la collectiondéfinirLargeurStandardméthode.

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