Impostazioni di allineamento con C++
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 consente l’accesso 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 GetCells() rappresenta un oggetto della classe Cell.
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
Utilizzare la proprietà GetHorizontalAlignment() dell’oggetto Style per allineare il testo orizzontalmente.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create workbook
Workbook workbook;
// Obtain the reference of the first worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Access the "A1" cell from the worksheet
Cell cell = worksheet.GetCells().Get(u"A1");
// Add some value to the "A1" cell
cell.PutValue(u"Visit Aspose!");
// Set the horizontal alignment of the text in the "A1" cell
Style style = cell.GetStyle();
style.SetHorizontalAlignment(TextAlignmentType::Center);
cell.SetStyle(style);
// Save the Excel file
workbook.Save(outDir + u"book1.out.xls", SaveFormat::Excel97To2003);
std::cout << "Excel file saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Allineamento Verticale
Similmente all’allineamento orizzontale, utilizzare la proprietà GetVerticalAlignment() dell’oggetto Style per allineare il testo verticalmente.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create workbook
Workbook workbook;
// Clearing all the worksheets
workbook.GetWorksheets().Clear();
// Adding a new worksheet to the Excel object
int i = workbook.GetWorksheets().Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.GetWorksheets().Get(i);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.GetCells().Get(u"A1");
// Adding some value to the "A1" cell
cell.PutValue(u"Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
Style style = cell.GetStyle();
// Setting the vertical alignment of the text in a cell
style.SetVerticalAlignment(TextAlignmentType::Center);
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(outDir + u"book1.out.xls", SaveFormat::Excel97To2003);
std::cout << "Excel file saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Rientro
È possibile impostare il livello di rientro del testo in una cella con la proprietà GetIndentLevel() dell’oggetto Style.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create a new workbook
Workbook workbook;
// Get the first worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Access the "A1" cell
Cell cell = worksheet.GetCells().Get(u"A1");
// Set value in the cell
cell.PutValue(u"Visit Aspose!");
// Get the cell's style
Style style = cell.GetStyle();
// Set the indentation level
style.SetIndentLevel(2);
// Apply the style to the cell
cell.SetStyle(style);
// Save the workbook
workbook.Save(outDir + u"book1.out.xls", SaveFormat::Excel97To2003);
std::cout << "File saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Orientamento
Imposta l’orientamento (rotazione) del testo in una cella con la proprietà GetRotationAngle() dell’oggetto Style.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create a new workbook
Workbook workbook;
// Get the first worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Access the "A1" cell
Cell cell = worksheet.GetCells().Get(u"A1");
// Add value to the cell
cell.PutValue(u"Visit Aspose!");
// Get the cell's style
Style style = cell.GetStyle();
// Set the rotation angle of the text to 25 degrees
style.SetRotationAngle(25);
// Apply the style to the cell
cell.SetStyle(style);
// Save the workbook in Excel 97-2003 format
workbook.Save(outDir + u"book1.out.xls", SaveFormat::Excel97To2003);
std::cout << "Workbook saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
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 testo a capo in una cella rende più facile leggerlo: l’altezza della cella si adatta per contenere tutto il testo, invece di tagliarlo o farlo fuoriuscire nelle celle adiacenti. Imposta il testo a capo on o off con la proprietà IsTextWrapped dell’oggetto Style.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create Workbook Object
Workbook wb;
// Open first Worksheet in the workbook
Worksheet ws = wb.GetWorksheets().Get(0);
// Get Worksheet Cells Collection
Cells 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.Get(0, 0).PutValue(u"I am using the latest version of Aspose.Cells to test this functionality");
// Make Cell's Text wrap
Style style = cell.Get(0, 0).GetStyle();
style.SetIsTextWrapped(true);
cell.Get(0, 0).SetStyle(style);
// Save Excel File
wb.Save(outDir + u"WrappingText_out.xlsx");
std::cout << "Text wrapping applied successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Ridimensionamento per adattarsi
Un’opzione per avvolgere il testo in un campo è ridurre le dimensioni del testo per adattarlo alle dimensioni di una cella. Questo viene fatto impostando la proprietà IsTextWrapped dell’oggetto Style su true.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create a new workbook
Workbook workbook;
// Get the first worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Access the "A1" cell
Cell cell = worksheet.GetCells().Get(u"A1");
// Add value to the cell
cell.PutValue(u"Visit Aspose!");
// Get the cell's style
Style style = cell.GetStyle();
// Set shrink to fit
style.SetShrinkToFit(true);
// Apply the style to the cell
cell.SetStyle(style);
// Save the workbook
workbook.Save(outDir + u"book1.out.xls", SaveFormat::Excel97To2003);
std::cout << "File saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Unione di celle
Come Microsoft Excel, Aspose.Cells supporta l’unione di diverse celle in una. Aspose.Cells fornisce due approcci a questo compito. Un modo è chiamare il metodo Merge della raccolta GetCells(). Il metodo Merge 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.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create a Workbook
Workbook wbk;
// Create a Worksheet and get the first sheet
Worksheet worksheet = wbk.GetWorksheets().Get(0);
// Create a Cells object to 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(u"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(u"Times New Roman");
// Set the font size
font.SetSize(18);
// Set the font color
font.SetColor(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(Color::Red());
style.SetPattern(BackgroundType::Solid);
// Apply the Style to C6 Cell
worksheet.GetCells().Get(5, 2).SetStyle(style);
// Save the Workbook
wbk.Save(outDir + u"mergingcells.out.xls");
std::cout << "Workbook saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
L’altro modo è chiamare prima il metodo CreateRange della raccolta GetCells() per creare un intervallo di celle da unire. Il metodo CreateRange richiede lo stesso set di parametri di quello del metodo Merge discusso sopra 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 è impostato con la proprietà GetTextDirection() dell’oggetto Style. 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 |
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Create a new workbook
Workbook workbook;
// Get the first worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Access cell A1
Cell cell = worksheet.GetCells().Get(u"A1");
// Set value in cell A1
cell.PutValue(u"I am using the latest version of Aspose.Cells to test this functionality.");
// Get the style of cell A1
Style style = cell.GetStyle();
// Set text direction to left-to-right
style.SetTextDirection(TextDirectionType::LeftToRight);
// Apply the modified style to the cell
cell.SetStyle(style);
// Save the workbook
workbook.Save(u"book1.xlsx");
std::cout << "Workbook saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
return 0;
}