Formattazione condizionale
La formattazione condizionale è una funzione avanzata di Microsoft Excel che consente di applicare formati a una cella o a un intervallo di celle e di far sì che tale formattazione cambi a seconda del valore della cella o del valore di una formula. Ad esempio, è possibile far sì che una cella appaia in grassetto solo quando il valore della cella è superiore a 500. Quando il valore della cella soddisfa la condizione, il formato specificato viene applicato alla cella. Se il valore della cella non soddisfa la condizione, viene utilizzata la formattazione predefinita. In Microsoft Excel, selezionare Formato, quindi Formattazione condizionale per aprire il riquadro della formattazione condizionale.
Formattazione condizionale in Microsoft Excel
Aspose.Cells supporta l’applicazione della formattazione condizionale alle celle in fase di esecuzione. In questo articolo viene spiegato come.
Applicare la formattazione condizionale
Aspose.Cells supporta la formattazione condizionale in due modi:
- Utilizzo di un foglio di calcolo del designer.
- Creazione della formattazione condizionale in fase di esecuzione.
Utilizzo del foglio di calcolo del designer
Gli sviluppatori possono creare un foglio di calcolo del designer che contiene la formattazione condizionale in Microsoft Excel e quindi aprire quel foglio di calcolo con Aspose.Cells. Aspose.Cells carica e salva il foglio di calcolo del designer, mantenendo qualsiasi impostazione di formattazione condizionale. Per saperne di più sui fogli di calcolo del designer, leggere Cos’è un foglio di calcolo del designer.
Applicare la formattazione condizionale in fase di esecuzione
Aspose.Cells supporta l’applicazione della formattazione condizionale in fase di esecuzione.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Path to source file | |
String dataDir = Utils.getSharedDataDir(ConditionalFormattingatRuntime.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
ConditionalFormattingCollection cfs = sheet.getConditionalFormattings(); | |
// The first method:adds an empty conditional formatting | |
int index = cfs.add(); | |
FormatConditionCollection fcs = cfs.get(index); | |
// Sets the conditional format range. | |
CellArea ca1 = new CellArea(); | |
ca1.StartRow = 0; | |
ca1.StartColumn = 0; | |
ca1.EndRow = 0; | |
ca1.EndColumn = 0; | |
CellArea ca2 = new CellArea(); | |
ca2.StartRow = 0; | |
ca2.StartColumn = 0; | |
ca2.EndRow = 0; | |
ca2.EndColumn = 0; | |
CellArea ca3 = new CellArea(); | |
ca3.StartRow = 0; | |
ca3.StartColumn = 0; | |
ca3.EndRow = 0; | |
ca3.EndColumn = 0; | |
fcs.addArea(ca1); | |
fcs.addArea(ca2); | |
fcs.addArea(ca3); | |
// Sets condition formulas. | |
int conditionIndex = fcs.addCondition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "=A2", "100"); | |
FormatCondition fc = fcs.get(conditionIndex); | |
int conditionIndex2 = fcs.addCondition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100"); | |
// Saving the Excel file | |
workbook.save(dataDir + "CFAtRuntime_out.xls"); |
Imposta Carattere
Impostazione dei caratteri in Microsoft Excel
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Path to source file | |
String dataDir = Utils.getSharedDataDir(SettingFontStyle.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
FormatCondition fc = null; | |
Style style = fc.getStyle(); | |
Font font = style.getFont(); | |
font.setItalic(true); | |
font.setBold(true); | |
font.setStrikeout(true); | |
font.setUnderline(FontUnderlineType.DOUBLE); | |
font.setColor(Color.getBlack()); | |
fc.setStyle(style); | |
// Saving the Excel file | |
workbook.save(dataDir + "SFontStyle_out.xls"); |
Imposta Bordo
Impostazione dei bordi in Microsoft Excel
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Path to source file | |
String dataDir = Utils.getSharedDataDir(SetBorder.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
// Adds an empty conditional formatting | |
int index = sheet.getConditionalFormattings().add(); | |
FormatConditionCollection fcs = sheet.getConditionalFormattings().get(index); | |
// Sets the conditional format range. | |
CellArea ca = new CellArea(); | |
ca.StartRow = 0; | |
ca.EndRow = 5; | |
ca.StartColumn = 0; | |
ca.EndColumn = 3; | |
fcs.addArea(ca); | |
// Adds condition. | |
int conditionIndex = fcs.addCondition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100"); | |
// Sets the background color. | |
FormatCondition fc = fcs.get(conditionIndex); | |
Style style = fc.getStyle(); | |
style.setBorder(BorderType.LEFT_BORDER, CellBorderType.DASHED, Color.fromArgb(0, 255, 255)); | |
style.setBorder(BorderType.TOP_BORDER, CellBorderType.DASHED, Color.fromArgb(0, 255, 255)); | |
style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.DASHED, Color.fromArgb(0, 255, 255)); | |
style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.DASHED, Color.fromArgb(255, 255, 0)); | |
fc.setStyle(style); |
Imposta Schema
Impostazione di un modello di cella in Microsoft Excel
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Path to source file | |
String dataDir = Utils.getSharedDataDir(SetPattern.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
// Adds an empty conditional formatting | |
int index = sheet.getConditionalFormattings().add(); | |
FormatConditionCollection fcs = sheet.getConditionalFormattings().get(index); | |
// Sets the conditional format range. | |
CellArea ca = new CellArea(); | |
ca.StartRow = 0; | |
ca.EndRow = 5; | |
ca.StartColumn = 0; | |
ca.EndColumn = 3; | |
fcs.addArea(ca); | |
// Adds condition. | |
int conditionIndex = fcs.addCondition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100"); | |
// Sets the background color. | |
FormatCondition fc = fcs.get(conditionIndex); | |
Style style = fc.getStyle(); | |
style.setPattern(BackgroundType.REVERSE_DIAGONAL_STRIPE); | |
style.setForegroundColor(Color.fromArgb(255, 255, 0)); | |
style.setBackgroundColor(Color.fromArgb(0, 255, 255)); | |
fc.setStyle(style); |