Paramètres d'alignement
Configuration des paramètres d’alignement
Paramètres d’alignement dans Microsoft Excel
Quiconque a utilisé Microsoft Excel pour formater des cellules connaîtra 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)
- Échancrure.
- Orientation.
- Contrôle du texte.
- Sens 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 fournitObtenirStyle etDéfinirStyle méthodes pour laCell class qui sont utilisées pour obtenir et définir la mise en forme d’une cellule. LeStylefournit des propriétés utiles pour configurer les paramètres d’alignement.
Sélectionnez n’importe quel type d’alignement de texte à l’aide de laType d’alignement de texte énumération. Les types d’alignement de texte prédéfinis dans leType d’alignement de textel’énumération sont :
Types d’alignement de texte | Description |
---|---|
Fond | Représente l’alignement du texte inférieur |
Centre | Représente l’alignement du texte au centre |
CenterAcross | Représente le centre sur l’alignement du texte |
Distribué | Représente l’alignement de texte distribué |
Remplir | Représente l’alignement du texte de remplissage |
Général | Représente l’alignement général du texte |
Justifier | Représente l’alignement du texte justifié |
Gauche | Représente l’alignement du texte à gauche |
Droit | Représente l’alignement du texte à droite |
Haut | Représente l’alignement supérieur du texte |
JustifiéBas | Aligne le texte avec une longueur kashida ajustée pour le texte arabe. |
ThaïDistribué | Distribue le texte thaï en particulier, car chaque caractère est traité comme un mot. |
Alignement horizontal, vertical et indentation
Utilisez leAlignementHorizontal propriété pour aligner le texte horizontalement etAlignement verticalpropriété pour aligner le texte verticalement. Il est possible de définir le niveau d’indentation du texte dans une cellule avec laNiveau d’indentation la propriété et tt n’a d’effet que lorsque l’alignement horizontal est à 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 laAngle de rotationla propriété.
// 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 explique comment contrôler le texte en définissant l’habillage du texte, le rétrécissement et d’autres options de formatage.
Habillage du texte
L’habillage du texte dans une cellule facilite la lecture : la hauteur de la cellule s’ajuste pour s’adapter à tout le texte, au lieu de le couper ou de déborder dans les cellules adjacentes. Activez ou désactivez l’habillage du texte avec leEstEnveloppéTextela propriété.
// 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écir pour s’adapter
Une option d’habillage du texte dans un champ consiste à réduire la taille du texte pour l’adapter aux dimensions d’une cellule. Cela se fait en réglant leRéduire pour s’adapter la propriété. àvrai.
// 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 Cells
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 façon consiste à appeler leFusionner méthode. La méthode prend les paramètres suivants pour fusionner les cellules :
- Première ligne : la première ligne à partir de laquelle commencer la fusion.
- Première colonne : la première colonne à partir de laquelle commencer la fusion.
- Nombre de lignes : le nombre de lignes à 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); | |
Sens 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 leDirection du texte la propriété. Aspose.Cells fournit des types de direction de texte prédéfinis dans leTextDirectionTypeénumération.
Types d’orientation du texte | Description |
---|---|
Contexte | L’ordre de lecture cohérent avec la langue du premier caractère saisi |
De gauche à droite | Ordre de lecture de gauche à droite |
De droite à gauche | 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"); |