Configuraciones de Alineación
Configurando Ajustes de Alineación
Configuraciones de Alineación en Microsoft Excel
Cualquiera que haya usado Microsoft Excel para formatear celdas estará familiarizado con las configuraciones de alineación en Microsoft Excel.
Como puedes ver en la figura anterior, hay diferentes tipos de opciones de alineación:
- Alineación de texto (horizontal y vertical)
- Sangría
- Orientación.
- Control de texto.
- Dirección del texto.
Todos estos ajustes de alineación son completamente compatibles con Aspose.Cells y se discuten con más detalle a continuación.
Ajustes de alineación en Aspose.Cells
Aspose.Cells proporciona métodos GetStyle y SetStyle para la clase Cell que se utilizan para obtener y establecer el formato de una celda. La clase Style proporciona propiedades útiles para configurar los ajustes de alineación.
Seleccione cualquier tipo de alineación de texto utilizando la enumeración TextAlignmentType. Los tipos de alineación de texto predefinidos en la enumeración TextAlignmentType son:
Tipos de Alineación de Texto | Descripción |
---|---|
Bottom | Representa la alineación del texto inferior |
Center | Representa la alineación del texto centrado |
CenterAcross | Representa la alineación del texto centrado a través |
Distributed | Representa la alineación del texto distribuido |
Fill | Representa la alineación del texto de relleno |
General | Representa la alineación del texto general |
Justify | Representa la alineación del texto justificado |
Left | Representa la alineación del texto a la izquierda |
Right | Representa la alineación del texto a la derecha |
Top | Representa la alineación del texto superior |
JustifiedLow | Alinea el texto con una longitud de kashida ajustada para el texto árabe. |
ThaiDistributed | Distribuye texto en tailandés especialmente, porque cada carácter se trata como una palabra. |
Alineación horizontal, vertical e indentación
Utilice la propiedad HorizontalAlignment para alinear el texto horizontalmente y la propiedad VerticalAlignment para alinear el texto verticalmente. Es posible establecer el nivel de sangría del texto en una celda con la propiedad IndentLevel y solo afecta cuando la alineación horizontal es izquierda o derecha.
// 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"); |
Orientación
Establezca la orientación (rotación) del texto en una celda con la propiedad 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"); |
Control de texto
La siguiente sección explica cómo controlar el texto mediante el ajuste del ajuste de texto, el ajuste al tamaño y otras opciones de formato.
Envolver texto
Envolver texto en una celda facilita su lectura: la altura de la celda se ajusta para que quepa todo el texto, en lugar de cortarlo o derramarse en celdas adyacentes. Activar o desactivar el ajuste de texto con la propiedad 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"); |
Reducir para ajustar
Una opción para envolver texto en un campo es reducir el tamaño del texto para que se ajuste a las dimensiones de la celda. Esto se hace configurando la propiedad ShrinkToFit como 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"); | |
Combinar celdas
Al igual que Microsoft Excel, Aspose.Cells admite fusionar varias celdas en una. Aspose.Cells proporciona dos enfoques para esta tarea. Una forma es llamar al método Merge. El método toma los siguientes parámetros para fusionar las celdas:
- Primera fila: la primera fila desde donde comenzar a combinar.
- Primera columna: la primera columna desde donde comenzar a combinar.
- Número de filas: el número de filas a fusionar.
- Número de columnas: el número de columnas a fusionar.
// 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); | |
Dirección del texto
Es posible establecer el orden de lectura del texto en las celdas. El orden de lectura es el orden visual en el que se muestran los caracteres, palabras, etc. Por ejemplo, el inglés es un idioma de izquierda a derecha, mientras que el árabe es un idioma de derecha a izquierda.
El orden de lectura se establece con la propiedad TextDirection. Aspose.Cells proporciona tipos de dirección de texto predefinidos en la enumeración TextDirectionType.
Tipos de dirección de texto | Descripción |
---|---|
Context | El orden de lectura es coherente con el idioma del primer carácter introducido |
LeftToRight | Orden de lectura de izquierda a derecha |
RightToLeft | Orden de lectura de derecha a izquierda |
// 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"); |