Utilisation des couleurs dans Apache POI et Aspose.Cells

Aspose.Cells - Travailler avec les couleurs

Aspose.Cells fournit une classe,Cahier, qui représente un fichier Excel Microsoft. La classe Workbook contient une WorksheetCollection qui permet d’accéder à chaque feuille de calcul du fichier Excel. Une feuille de calcul est représentée par leFeuille de travailclasse. La classe Worksheet fournit une Cellscollection. Chaque pièce de la collection Cells représente un objet de laCellclasse.

Aspose.Cells fournit la méthode setStyle dans la classe Cell qui est utilisée pour définir la mise en forme d’une cellule. En outre, l’objet Style de la classe Style peut être utilisé pour configurer les paramètres de police.

Java

 //Accessing cell from the worksheet

Cell cell = cells.get("B2");

Style style = cell.getStyle();

//Setting the foreground color to yellow

style.setBackgroundColor(Color.getYellow());

//Setting the background pattern to vertical stripe

style.setPattern(BackgroundType.VERTICAL_STRIPE);

//Saving the modified style to the "A1" cell.

cell.setStyle(style);

// === Setting Foreground ===

//Adding custom color to the palette at 55th index

Color color = Color.fromArgb(212,213,0);

workbook.changePalette(color,55);

//Accessing cell from the worksheet

cell = cells.get("B3");

//Adding some value to the cell

cell.setValue("Hello Aspose!");

//Setting the custom color to the font

style = cell.getStyle();

Font font = style.getFont();

font.setColor(color);

cell.setStyle(style);

Apache POI SS - HSSF XSSF - Travailler avec les couleurs

La classe CellStyle est disponible pour définir les paramètres d’arrière-plan et de motif de remplissage.

Java

 // Aqua background

CellStyle style = wb.createCellStyle();

style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());

style.setFillPattern(CellStyle.BIG_SPOTS);

Cell cell = row.createCell((short) 1);

cell.setCellValue("X");

cell.setCellStyle(style);

// Orange "foreground", foreground being the fill foreground not the font color.

style = wb.createCellStyle();

style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());

style.setFillPattern(CellStyle.SOLID_FOREGROUND);

cell = row.createCell((short) 2);

cell.setCellValue("X");

cell.setCellStyle(style);

Télécharger le code d’exécution

Télécharger l’exemple de code