Paramètres d alignement
Configuration des paramètres d’alignement
Paramètres d’alignement dans Microsoft Excel
Toute personne ayant utilisé Microsoft Excel pour formater des cellules sera familière avec les paramètres d’alignement dans Microsoft Excel.
Comme vous pouvez le voir sur la figure ci-dessus, il existe différents types d’options d’alignement :
- Alignement du texte (horizontal et vertical)
- Retrait.
- Orientation.
- Contrôle du texte.
- Direction du texte.
Tous ces paramètres d’alignement sont entièrement pris en charge par Aspose.Cells et sont discutés plus en détail ci-dessous.
Paramètres d’alignement dans Aspose.Cells
Aspose.Cells fournit les méthodes GetStyle et SetStyle pour la classe Cell qui sont utilisées pour obtenir et définir le formatage d’une cellule. La classe Style fournit des propriétés utiles pour configurer les paramètres d’alignement.
Sélectionnez n’importe quel type d’alignement de texte en utilisant l’énumération TextAlignmentType. Les types d’alignement de texte prédéfinis dans l’énumération TextAlignmentType sont:
Types d’alignement de texte | Description |
---|---|
Bottom | Représente un alignement de texte en bas |
Center | Représente un alignement de texte au centre |
CenterAcross | Représente un alignement de texte centré sur plusieurs cellules |
Distributed | Représente un alignement de texte distribué |
Fill | Représente un alignement de texte en remplissage |
General | Représente un alignement de texte général |
Justify | Représente un alignement de texte justifié |
Left | Représente un alignement de texte à gauche |
Right | Représente un alignement de texte à droite |
Top | Représente un alignement de texte en haut |
JustifiedLow | Aligne le texte avec une longueur de kashida ajustée pour le texte arabe. |
ThaiDistributed | Distribue le texte thaïlandais en particulier, car chaque caractère est traité comme un mot. |
Alignement horizontal, vertical et indentation
Utilisez la propriété HorizontalAlignment pour aligner le texte horizontalement et la propriété VerticalAlignment pour aligner le texte verticalement. Il est possible de définir le niveau d’indentation du texte dans une cellule avec la propriété IndentLevel et cela ne s’applique qu’en cas d’alignement horizontal à gauche ou à droite.
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Visit Aspose!"); | |
// Setting the horizontal alignment of the text in the "A1" cell | |
Style style = cell.getStyle(); | |
//Set text left horizontal alignment | |
style.setHorizontalAlignment(TextAlignmentType.RIGHT); | |
//Set indent | |
style.setIndentLevel(4); | |
//Set text top vertical alignment | |
style.setVerticalAlignment(TextAlignmentType.TOP); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("book1.xlsx"); |
Orientation
Définissez l’orientation (rotation) du texte dans une cellule avec la propriété RotationAngle
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Visit Aspose!"); | |
// Setting the horizontal alignment of the text in the "A1" cell | |
Style style = cell.getStyle(); | |
// Setting the rotation of the text (inside the cell) to 25 | |
style.setRotationAngle(25); | |
cell.setStyle(style); | |
//Accessing the "A2" cell from the worksheet | |
cell = worksheet.getCells().get("A2"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Visit Aspose!"); | |
// Setting the horizontal alignment of the text in the "A2" cell | |
style = cell.getStyle(); | |
// Setting the orientation of the text from top to bottom | |
style.setRotationAngle(255); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("book1.xlsx"); |
Contrôle du texte
La section suivante aborde comment contrôler le texte en définissant le retour à la ligne, le rétrécissement pour s’adapter et d’autres options de mise en forme.
Retour à la ligne du texte
Le renvoi à la ligne du texte dans une cellule facilite la lecture : la hauteur de la cellule s’ajuste pour contenir tout le texte, au lieu de le couper ou le faire déborder sur les cellules adjacentes. Définissez le renvoi à la ligne du texte sur ou hors avec la propriété IsTextWrapped
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("I am using the latest version of Aspose.Cells to test this functionality."); | |
// Gets style | |
Style style = cell.getStyle(); | |
// Wrap Cell's Text wrap | |
style.setTextWrapped( true); | |
//Set style. | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("book1.xlsx"); |
Rétrécissement pour s’adapter
Une option pour le renvoi à la ligne du texte dans un champ est de réduire la taille du texte pour l’adapter aux dimensions de la cellule. Cela se fait en définissant la propriété ShrinkToFit à true.
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("I am using the latest version of Aspose.Cells to test this functionality."); | |
// Gets style in the "A1" cell | |
Style style = cell.getStyle(); | |
// Shrinking the text to fit according to the dimensions of the cell | |
style.setShrinkToFit(true); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("book1.xlsx"); | |
Fusion de cellules
Comme Microsoft Excel, Aspose.Cells prend en charge la fusion de plusieurs cellules en une seule. Aspose.Cells propose deux approches pour cette tâche. Une manière est d’appeler la méthode Merge. La méthode prend les paramètres suivants pour fusionner les cellules:
- Première rangée : la première rangée à partir de laquelle commencer la fusion.
- Première colonne : la première colonne à partir de laquelle commencer la fusion.
- Nombre de rangées : le nombre de rangées à fusionner.
- Nombre de colonnes : le nombre de colonnes à fusionner.
// Create a Cells object ot fetch all the cells. | |
Cells cells = worksheet.getCells(); | |
// Merge some Cells (C6:E7) into a single C6 Cell. | |
cells.merge(5, 2, 2, 3); | |
// Input data into C6 Cell. | |
worksheet.getCells().get(5, 2).putValue("This is my value"); | |
// Create a Style object to fetch the Style of C6 Cell. | |
Style style = worksheet.getCells().get(5, 2).getStyle(); | |
// Create a Font object | |
Font font = style.getFont(); | |
// Set the name. | |
font.setName("Times New Roman"); | |
// Set the font size. | |
font.setSize(18); | |
// Set the font color | |
font.setColor(Color.getBlue()); | |
// Bold the text | |
font.setBold(true); | |
// Make it italic | |
font.setItalic(true); | |
// Set the backgrond color of C6 Cell to Red | |
style.setForegroundColor(Color.getRed()); | |
style.setPattern(BackgroundType.SOLID); | |
// Apply the Style to C6 Cell. | |
cells.get(5, 2).setStyle(style); | |
Direction du texte
Il est possible de définir l’ordre de lecture du texte dans les cellules. L’ordre de lecture est l’ordre visuel dans lequel les caractères, les mots, etc. sont affichés. Par exemple, l’anglais est une langue de gauche à droite tandis que l’arabe est une langue de droite à gauche.
L’ordre de lecture est défini avec la propriété TextDirection. Aspose.Cells fournit des types de direction de texte prédéfinis dans l’énumération TextDirectionType.
Types de direction du texte | Description |
---|---|
Context | L’ordre de lecture en accord avec la langue du premier caractère saisi |
LeftToRight | Ordre de lecture de gauche à droite |
RightToLeft | Ordre de lecture de droite à gauche |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("I am using the latest version of Aspose.Cells to test this functionality."); | |
// Gets style in the "A1" cell | |
Style style = cell.getStyle(); | |
// Shrinking the text to fit according to the dimensions of the cell | |
style.setTextDirection(TextDirectionType.LEFT_TO_RIGHT); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("book1.xlsx"); |