Cambiar la alineación Cells y mantener el formato existente
Posibles escenarios de uso
A veces, desea cambiar la alineación de varias celdas pero también desea mantener el formato existente. Aspose.Cells le permite hacerlo usando elStyleFlag.Alineaciones propiedad. si lo vas a configurarverdadero , se producirán cambios en la alineación, de lo contrario no. Tenga en cuenta,Bandera de estilo El objeto se pasa como un parámetro aRango.applyStyle() método que realmente aplica el formato al rango de celdas.
Cambiar la alineación Cells y mantener el formato existente
El siguiente código de ejemplo carga elejemplo de archivo de Excel, crea el rango y el centro lo alinea horizontal y verticalmente y mantiene intacto el formato existente. La siguiente captura de pantalla compara el archivo de ejemplo de Excel yarchivo de salida de Excely muestra que todo el formato existente de las celdas es el mismo excepto que las celdas ahora están alineadas al centro horizontal y verticalmente.
Código de muestra
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Load sample Excel file containing cells with formatting. | |
Workbook wb = new Workbook(srcDir + "sampleChangeCellsAlignmentAndKeepExistingFormatting.xlsx"); | |
// Access first worksheet. | |
Worksheet ws = wb.getWorksheets().get(0); | |
// Create cells range. | |
Range rng = ws.getCells().createRange("B2:D7"); | |
// Create style object. | |
Style st = wb.createStyle(); | |
// Set the horizontal and vertical alignment to center. | |
st.setHorizontalAlignment(TextAlignmentType.CENTER); | |
st.setVerticalAlignment(TextAlignmentType.CENTER); | |
// Create style flag object. | |
StyleFlag flag = new StyleFlag(); | |
// Set style flag alignments true. It is most crucial statement. | |
// Because if it will be false, no changes will take place. | |
flag.setAlignments(true); | |
// Apply style to range of cells. | |
rng.applyStyle(st, flag); | |
// Save the workbook in XLSX format. | |
wb.save(outDir + "outputChangeCellsAlignmentAndKeepExistingFormatting.xlsx", SaveFormat.XLSX); |