Modificare l'allineamento Cells e mantenere la formattazione esistente

Possibili scenari di utilizzo

A volte, vuoi cambiare l’allineamento di più celle ma vuoi anche mantenere la formattazione esistente. Aspose.Cells ti permette di farlo utilizzando ilStyleFlag.Alignments proprietà. Se lo imposteraiVERO , i cambiamenti nell’allineamento avranno luogo altrimenti no. Notare che,StyleFlag oggetto viene passato come parametro aRange.applyStyle() metodo che applica effettivamente la formattazione all’intervallo di celle.

Modificare l’allineamento Cells e mantenere la formattazione esistente

Il codice di esempio seguente carica il fileesempio di file Excel, crea l’intervallo e il centro lo allinea orizzontalmente e verticalmente e mantiene intatta la formattazione esistente. Lo screenshot seguente confronta il file Excel di esempio efile Excel di outpute mostra che tutta la formattazione esistente delle celle è la stessa, tranne per il fatto che le celle ora sono allineate al centro orizzontalmente e verticalmente.

cose da fare:immagine_alt_testo

Codice d’esempio

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