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 una classe, Workbook, che rappresenta un file Excel. La classe Workbook contiene una collezione Worksheets che permette di accedere a ogni foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una collezione getCells(). Ogni elemento nella collezione Cells rappresenta un oggetto della classe Cell.
Aspose.Cells fornisce i metodi getStyle e setStyle per la classe Cell che vengono usati per ottenere e impostare il formato di una cella. La classe Style fornisce proprietà utili per configurare le impostazioni di allineamento.
Seleziona qualsiasi tipo di allineamento del testo usando 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
Usa il metodo setHorizontalAlignment dell’oggetto Style per allineare orizzontalmente il testo.
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
if (!fs.existsSync(dataDir)) { | |
fs.mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Obtaining the reference of the worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
const 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 | |
const style = cell.getStyle(); | |
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |
Allineamento Verticale
Simile all’allineamento orizzontale, utilizza il metodo setVerticalAlignment dell’oggetto Style per allineare verticalmente il testo.
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
if (!require("fs").existsSync(dataDir)) { | |
require("fs").mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Clearing all the worksheets | |
workbook.getWorksheets().clear(); | |
// Adding a new worksheet to the Excel object | |
const i = workbook.getWorksheets().add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
const worksheet = workbook.getWorksheets().get(i); | |
// Accessing the "A1" cell from the worksheet | |
const 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 | |
const style = cell.getStyle(); | |
// Setting the vertical alignment of the text in a cell | |
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |
Rientro
È possibile impostare il livello di indentazione del testo in una cella con il metodo setIndentLevel dell’oggetto Style.
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
if (!require("fs").existsSync(dataDir)) { | |
require("fs").mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Obtaining the reference of the worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
const 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 | |
const style = cell.getStyle(); | |
// Setting the indentation level of the text (inside the cell) to 2 | |
style.setIndentLevel(2); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |
Orientamento
Imposta l’orientamento (rotazione) del testo in una cella con il metodo setRotationAngle dell’oggetto Style.
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
if (!require("fs").existsSync(dataDir)) { | |
require("fs").mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Obtaining the reference of the worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
const 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 | |
const style = cell.getStyle(); | |
// Setting the rotation of the text (inside the cell) to 25 | |
style.setRotationAngle(25); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |
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
L’andamento del testo in una cella facilita la lettura: l’altezza della cella si adatta per contenere tutto il testo, invece di tagliarlo o fuoriuscire nelle celle adiacenti. Attiva o disattiva l’impacchettamento del testo con il metodo setIsTextWrapped(boolean) dell’oggetto Style.
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create Workbook Object | |
const wb = new AsposeCells.Workbook(); | |
// Open first Worksheet in the workbook | |
const ws = wb.getWorksheets().get(0); | |
// Get Worksheet Cells Collection | |
const cell = ws.getCells(); | |
// Increase the width of First Column Width | |
cell.setColumnWidth(0, 35); | |
// Increase the height of first row | |
cell.setRowHeight(0, 36); | |
// Add Text to the First Cell | |
cell.checkCell(0, 0).putValue("I am using the latest version of Aspose.Cells to test this functionality"); | |
// Make Cell's Text wrap | |
const style = cell.checkCell(0, 0).getStyle(); | |
style.setIsTextWrapped(true); | |
cell.checkCell(0, 0).setStyle(style); | |
// Save Excel File | |
wb.save(path.join(dataDir, "WrappingText.out.xlsx")); |
Ridimensionamento per adattarsi
Un’opzione per l’impacchettamento del testo in un campo è ridurre la dimensione del testo per adattarsi alle dimensioni della cella. Ciò si ottiene impostando il metodo setShrinkToFit(boolean) dell’oggetto Style su true.
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
if (!require("fs").existsSync(dataDir)) { | |
require("fs").mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Obtaining the reference of the worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
const 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 | |
const 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(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |
Unione di celle
Come Microsoft Excel, Aspose.Cells supporta l’unione di più celle in una sola. Aspose.Cells offre due approcci per questo compito. Un modo è chiamare il metodo merge della raccolta Cells. Il metodo merge accetta 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.
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
if (!require("fs").existsSync(dataDir)) { | |
require("fs").mkdirSync(dataDir); | |
} | |
// Create a Workbook. | |
const wbk = new AsposeCells.Workbook(); | |
// Create a Worksheet and get the first sheet. | |
const worksheet = wbk.getWorksheets().get(0); | |
// Create a Cells object to fetch all the cells. | |
const cells = worksheet.getCells(); | |
// Merge some Cells (C6:E7) into a single C6 Cell. | |
cells.merge(5, 2, 2, 3); | |
// Input data into C6 Cell. | |
cells.get(5, 2).putValue("This is my value"); | |
// Create a Style object to fetch the Style of C6 Cell. | |
const style = cells.get(5, 2).getStyle(); | |
// Create a Font object | |
const font = style.getFont(); | |
// Set the name. | |
font.setName("Times New Roman"); | |
// Set the font size. | |
font.setSize(18); | |
// Set the font color | |
font.setColor(AsposeCells.Color.Blue); | |
// Bold the text | |
font.setIsBold(true); | |
// Make it italic | |
font.setIsItalic(true); | |
// Set the background color of C6 Cell to Red | |
style.setForegroundColor(AsposeCells.Color.Red); | |
style.setPattern(AsposeCells.BackgroundType.Solid); | |
// Apply the Style to C6 Cell. | |
cells.get(5, 2).setStyle(style); | |
// Save the Workbook. | |
wbk.save(path.join(dataDir, "mergingcells.out.xls")); |
L’altro modo è chiamare prima il metodo createRange della raccolta Cells per creare un intervallo di celle da unire. Il metodo createRange prende gli stessi parametri del sopra menzionato metodo merge e restituisce un oggetto Range. L’oggetto Range fornisce anche un metodo merge che unisce l’intervallo specificato nell’oggetto Range.
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 si imposta con la proprietà TextDirection dell’oggetto Style. Aspose.Cells offre 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 |
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
const filePath = path.join(dataDir, "sample.xlsx"); | |
// Loads the workbook which contains hidden external links | |
const workbook = new AsposeCells.Workbook(filePath); | |
// Instantiating a Workbook object | |
// Obtaining the reference of first worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
const 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 | |
const style = cell.getStyle(); | |
// Shrinking the text to fit according to the dimensions of the cell | |
style.setTextDirection(AsposeCells.TextDirectionType.LeftToRight); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("book1.xlsx"); |