Configuración de alineación

Configuración de ajustes de alineación

Configuración de alineación en Microsoft Excel

Cualquiera que haya usado Microsoft Excel para formatear celdas estará familiarizado con la configuración de alineación en Microsoft Excel.

Como puede 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.

Todas estas configuraciones de alineación son totalmente compatibles con Aspose.Cells y se analizan con más detalle a continuación.

Ajustes de alineación en Aspose.Cells

Aspose.Cells proporcionaObtenerEstilo yEstablecerEstilo métodos para elCell clase que se utilizan para obtener y establecer el formato de una celda. ÉlEstiloLa clase proporciona propiedades útiles para configurar los ajustes de alineación.

Seleccione cualquier tipo de alineación de texto usando elTextAlignmentType enumeración. Los tipos de alineación de texto predefinidos en elTextAlignmentTypeenumeración son:

Tipos de alineación de texto Descripción
Abajo Representa la alineación del texto inferior
Centro Representa la alineación del texto central
CenterAcross Representa el centro a lo largo de la alineación del texto
Repartido Representa la alineación de texto distribuida
Llenar Representa la alineación del texto de relleno
General Representa la alineación general del texto.
Justificar Representa justificar la alineación del texto
Izquierda Representa la alineación del texto a la izquierda
Derecha Representa la alineación correcta del texto.
Parte superior Representa la alineación del texto superior
JustificadoBajo Alinea el texto con una longitud de kashida ajustada para el texto en árabe.
TailandésDistribuido Distribuye especialmente el texto en tailandés, porque cada carácter se trata como una palabra.

Alineación horizontal, vertical y sangría

Utilizar elAlineación horizontal propiedad para alinear el texto horizontalmente yAlineamiento verticalpropiedad para alinear el texto verticalmente. Es posible establecer el nivel de sangría del texto en una celda con elnivel de sangría propiedad y tt 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

Establecer la orientación (rotación) del texto en una celda con elÁngulo de rotaciónpropiedad.

// 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 configurando el ajuste de texto, reducir para ajustar y otras opciones de formato.

Texto de ajuste

Envolver texto en una celda hace que sea más fácil de leer: la altura de la celda se ajusta para ajustarse a todo el texto, en lugar de cortarlo o extenderse a las celdas adyacentes. Active o desactive el ajuste de texto con elEsTextoEnvueltopropiedad.

// 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");

Encogimiento para encajar

Una opción para ajustar texto en un campo es reducir el tamaño del texto para que se ajuste a las dimensiones de una celda. Esto se hace configurando elShrinkToFit propiedad. averdadero.

// 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");

Fusionando Cells

Al igual que Microsoft Excel, Aspose.Cells admite la combinación de varias celdas en una sola. Aspose.Cells proporciona dos enfoques para esta tarea. Una forma es llamar alUnir método. El método toma los siguientes parámetros para fusionar las celdas:

  • Primera fila: la primera fila desde donde comenzar a fusionar.
  • Primera columna: la primera columna desde donde comenzar a fusionar.
  • 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 elDirección del texto propiedad. Aspose.Cells proporciona tipos de dirección de texto predefinidos en elTextDirectionTypeenumeración.

Tipos de dirección de texto Descripción
Contexto El orden de lectura consistente con el idioma del primer carácter ingresado
De izquierda a derecha Orden de lectura de izquierda a derecha
De derecha a izquierda 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");

Temas avanzados