Impostazioni di Allineamento
Configurazione delle impostazioni di allineamento
Impostazioni di allineamento in Microsoft Excel
Chiunque abbia usato Microsoft Excel per formattare le celle sarà familiare con le impostazioni di allineamento in Microsoft Excel.
Come si può vedere dalla figura sopra, ci sono diversi tipi di opzioni di allineamento:
- Allineamento del testo (orizzontale e verticale)
- Rientro.
- Orientamento.
- Controllo del testo.
- Direzione del testo.
Tutte queste impostazioni di allineamento sono completamente supportate da Aspose.Cells e sono discusse in modo più dettagliato di seguito.
Impostazioni di allineamento in Aspose.Cells
Aspose.Cells fornisce i metodi GetStyle e SetStyle per la classe Cell che vengono utilizzati per ottenere e impostare la formattazione di una cella. La classe Style fornisce proprietà utili per la configurazione delle impostazioni di allineamento.
Seleziona qualsiasi tipo di allineamento del testo utilizzando l’enumerazione TextAlignmentType. I tipi di allineamento del testo predefiniti nell’enumerazione TextAlignmentType sono:
Tipi di Allineamento del Testo | Descrizione |
---|---|
Bottom | Rappresenta l’allineamento del testo in basso |
Center | Rappresenta l’allineamento del testo al centro |
CenterAcross | Rappresenta l’allineamento del testo al centro tra le celle |
Distributed | Rappresenta l’allineamento distribuito del testo |
Fill | Rappresenta l’allineamento di riempimento del testo |
General | Rappresenta l’allineamento del testo generale |
Justify | Rappresenta l’allineamento del testo giustificato |
Left | Rappresenta l’allineamento del testo a sinistra |
Right | Rappresenta l’allineamento del testo a destra |
Top | Rappresenta l’allineamento del testo in alto |
JustifiedLow | Allinea il testo con una lunghezza kashida regolata per il testo in arabo. |
ThaiDistributed | Distribuisce il testo thailandese in particolare, poiché ciascun carattere è trattato come una parola. |
Allineamento orizzontale, verticale e rientro
Usa la proprietà HorizontalAlignment per allineare il testo in orizzontale e la proprietà VerticalAlignment per allineare il testo in verticale. È possibile impostare il livello di rientro del testo in una cella con la proprietà IndentLevel. e influisce solo quando l’allineamento orizzontale è a sinistra o a destra.
// 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"); |
Orientamento
Imposta l’orientamento (rotazione) del testo in una cella con la proprietà 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"); |
Controllo del Testo
La seguente sezione discute come controllare il testo impostando il rientro del testo, adattamento alla cella e altre opzioni di formattazione.
Testo a Capo
Il ritorno a capo del testo in una cella rende più facile la lettura: l’altezza della cella si adatta per contenere tutto il testo anziché tagliarlo o farlo traboccare nelle celle adiacenti. Imposta il ritorno a capo del testo su on o off con la proprietà 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"); |
Ridimensionamento per adattarsi
Una opzione per il ritorno a capo del testo in un campo è ridurre le dimensioni del testo per adattarlo alle dimensioni di una cella. Questo viene fatto impostando la proprietà ShrinkToFit su 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"); | |
Unione di celle
Come Microsoft Excel, Aspose.Cells supporta l’unione di diverse celle in una. Aspose.Cells fornisce due approcci per questo compito. Un modo è chiamare il metodo Merge. Il metodo richiede i seguenti parametri per unire le celle:
- Prima riga: la prima riga da cui iniziare a unire.
- Prima colonna: la prima colonna da cui iniziare a unire.
- Numero di righe: il numero di righe da unire.
- Numero di colonne: il numero di colonne da unire.
// 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); | |
Direzione del testo
È possibile impostare l’ordine di lettura del testo nelle celle. L’ordine di lettura è l’ordine visivo in cui vengono visualizzati i caratteri, le parole, ecc. Ad esempio, l’inglese è una lingua da sinistra a destra mentre l’arabo è una lingua da destra a sinistra.
L’ordine di lettura è impostato con la proprietà TextDirection. Aspose.Cells fornisce tipi di direzione del testo predefiniti nell’enumerazione TextDirectionType.
Tipi di direzione del testo | Descrizione |
---|---|
Context | L’ordine di lettura coerente con la lingua del primo carattere inserito |
LeftToRight | Ordine di lettura da sinistra a destra |
RightToLeft | Ordine di lettura da destra a sinistra |
// 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"); |