Agregar escalas de colores de 2 colores y 3 colores con formato condicional
Contents
[
Hide
]
Las escalas de colores de 2 colores y 3 colores con formato condicional se agregan de la misma manera, excepto que se diferencian por la propiedad FormatCondition.ColorScale.Is3ColorScale. Esta propiedad es false para escalas de colores de 2 colores y true para escalas de colores de 3 colores con formato condicional.
Agregar escalas de colores de 2 colores y 3 colores con formato condicional
El siguiente código de muestra añade formatos condicionales de escalas de colores de 2 y 3 colores. Genera el archivo de Excel de salida como se muestra a continuación.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getDataDir(AddingTwoAndThreeColorScale.class); | |
// Create workbook | |
Workbook workbook = new Workbook(); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Add some data in cells | |
worksheet.getCells().get("A1").putValue("2-Color Scale"); | |
worksheet.getCells().get("D1").putValue("3-Color Scale"); | |
for (int i = 2; i <= 15; i++) { | |
worksheet.getCells().get("A" + i).putValue(i); | |
worksheet.getCells().get("D" + i).putValue(i); | |
} | |
// Adding 2-Color Scale Conditional Formatting | |
CellArea ca = CellArea.createCellArea("A2", "A15"); | |
int idx = worksheet.getConditionalFormattings().add(); | |
FormatConditionCollection fcc = worksheet.getConditionalFormattings().get(idx); | |
fcc.addCondition(FormatConditionType.COLOR_SCALE); | |
fcc.addArea(ca); | |
FormatCondition fc = worksheet.getConditionalFormattings().get(idx).get(0); | |
fc.getColorScale().setIs3ColorScale(false); | |
fc.getColorScale().setMaxColor(Color.getLightBlue()); | |
fc.getColorScale().setMinColor(Color.getLightGreen()); | |
// Adding 3-Color Scale Conditional Formatting | |
ca = CellArea.createCellArea("D2", "D15"); | |
idx = worksheet.getConditionalFormattings().add(); | |
fcc = worksheet.getConditionalFormattings().get(idx); | |
fcc.addCondition(FormatConditionType.COLOR_SCALE); | |
fcc.addArea(ca); | |
fc = worksheet.getConditionalFormattings().get(idx).get(0); | |
fc.getColorScale().setIs3ColorScale(true); | |
fc.getColorScale().setMaxColor(Color.getLightBlue()); | |
fc.getColorScale().setMidColor(Color.getYellow()); | |
fc.getColorScale().setMinColor(Color.getLightGreen()); | |
// Save the workbook | |
workbook.save(dataDir + "output.xlsx"); |