Agregar un conjunto de iconos condicionales con el texto de la celda
Contents
[
Hide
]
A veces, deseas agregar iconos condicionales junto al texto en una celda para hacer que los datos sean más significativos para los lectores. Quieres usar algunos de los tipos de iconos de formato condicional pero sin aplicar formato condicional a las celdas. Aspose.Cells admite esta función.
A continuación se muestra un ejemplo que crea un archivo XLSX desde cero, añadiendo iconos condicionales a las celdas con texto sin aplicar formato condicional.
Cuando se ejecuta el código, se añaden imágenes del conjunto de iconos condicionales en el área de celdas “B2:C4” como se muestra a continuación.
Archivo de salida
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 | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(AddConditionalIconsSet.class); | |
// Instantiate an instance of Workbook | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet (default worksheet) in the workbook | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Get the cells | |
Cells cells = worksheet.getCells(); | |
// Set the columns widths (A, B and C) | |
worksheet.getCells().setColumnWidth(0, 24); | |
worksheet.getCells().setColumnWidth(1, 24); | |
worksheet.getCells().setColumnWidth(2, 24); | |
// Input date into the cells | |
cells.get("A1").setValue("KPIs"); | |
cells.get("A2").setValue("Total Turnover (Sales at List)"); | |
cells.get("A3").setValue("Total Gross Margin %"); | |
cells.get("A4").setValue("Total Net Margin %"); | |
cells.get("B1").setValue("UA Contract Size Group 4"); | |
cells.get("B2").setValue(19551794); | |
cells.get("B3").setValue(11.8070745566204); | |
cells.get("B4").setValue(11.858589818569); | |
cells.get("C1").setValue("UA Contract Size Group 3"); | |
cells.get("C2").setValue(8150131.66666667); | |
cells.get("C3").setValue(10.3168384396244); | |
cells.get("C4").setValue(11.3326931937091); | |
// Get the conditional icon's image data | |
byte[] imagedata = ConditionalFormattingIcon.getIconImageData(IconSetType.TRAFFIC_LIGHTS_31, 0); | |
// Create a stream based on the image data | |
ByteArrayInputStream stream = new ByteArrayInputStream(imagedata); | |
// Add the picture to the cell based on the stream | |
worksheet.getPictures().add(1, 1, stream); | |
// Get the conditional icon's image data | |
byte[] imagedata1 = ConditionalFormattingIcon.getIconImageData(IconSetType.ARROWS_3, 2); | |
// Create a stream based on the image data | |
ByteArrayInputStream stream1 = new ByteArrayInputStream(imagedata1); | |
// Add the picture to the cell based on the stream | |
worksheet.getPictures().add(1, 2, stream1); | |
// Get the conditional icon's image data | |
byte[] imagedata2 = ConditionalFormattingIcon.getIconImageData(IconSetType.SYMBOLS_3, 0); | |
// Create a stream based on the image data | |
ByteArrayInputStream stream2 = new ByteArrayInputStream(imagedata2); | |
// Add the picture to the cell based on the stream | |
worksheet.getPictures().add(2, 1, stream2); | |
// Get the conditional icon's image data | |
byte[] imagedata3 = ConditionalFormattingIcon.getIconImageData(IconSetType.STARS_3, 0); | |
// Create a stream based on the image data | |
ByteArrayInputStream stream3 = new ByteArrayInputStream(imagedata3); | |
// Add the picture to the cell based on the stream | |
worksheet.getPictures().add(2, 2, stream3); | |
// Get the conditional icon's image data | |
byte[] imagedata4 = ConditionalFormattingIcon.getIconImageData(IconSetType.BOXES_5, 1); | |
// Create a stream based on the image data | |
ByteArrayInputStream stream4 = new ByteArrayInputStream(imagedata4); | |
// Add the picture to the cell based on the stream | |
worksheet.getPictures().add(3, 1, stream4); | |
// Get the conditional icon's image data | |
byte[] imagedata5 = ConditionalFormattingIcon.getIconImageData(IconSetType.FLAGS_3, 1); | |
// Create a stream based on the image data | |
ByteArrayInputStream stream5 = new ByteArrayInputStream(imagedata5); | |
// Add the picture to the cell based on the stream | |
worksheet.getPictures().add(3, 2, stream5); | |
// Save the Excel file | |
workbook.save(dataDir + "outfile_cond_icons1.xlsx"); |