//creating a custom palette for the workbook HSSFPalette palette = wb.getCustomPalette(); //replacing the standard red with freebsd.org red palette.setColorAtIndex(HSSFColor.RED.index, (byte) 153, //RGB red (0-255) (byte) 0, //RGB green (byte) 0 //RGB blue ); // or creating a new Color HSSFColor myColor = palette.addColor((byte) 153, (byte) 0, (byte) 0); HSSFCellStyle style = wb.createCellStyle(); style.setFillForegroundColor(myColor);
private static short getOrAddColorIndex(short[] rgb, HSSFWorkbook wb) { HSSFPalette palette = wb.getCustomPalette(); HSSFColor color = palette.findColor((byte) rgb[0], (byte) rgb[1], (byte) rgb[2]); if (color == null) { try { color = palette.addColor((byte) rgb[0], (byte) rgb[1], (byte) rgb[2]); } catch (RuntimeException e) { // Could not find free color index color = palette.findSimilarColor(rgb[0], rgb[1], rgb[2]); } } return color.getIndex(); }
static private short getColorIndex(short[] rgbColor, Workbook workbook) { if (rgbColor != null) { HSSFWorkbook hssfWorkbook = (HSSFWorkbook) workbook; HSSFPalette palette = hssfWorkbook.getCustomPalette(); HSSFColor hssfColor = palette.findColor((byte) rgbColor[0], (byte) rgbColor[1], (byte) rgbColor[2]); if (hssfColor == null ) { try { hssfColor = palette.addColor((byte) rgbColor[0], (byte) rgbColor[1], (byte) rgbColor[2]); } catch (Exception e) { HSSFColor similarColor = palette.findSimilarColor((int)rgbColor[0], (int)rgbColor[1], (int)rgbColor[2]); palette.setColorAtIndex(similarColor.getIndex(), (byte) rgbColor[0], (byte) rgbColor[1], (byte) rgbColor[2]); hssfColor = palette.getColor(similarColor.getIndex()); } } return hssfColor.getIndex(); } return HSSFColor.WHITE.index; }
static private short getColorIndex(short[] rgbColor, Workbook workbook) { if (rgbColor != null) { HSSFWorkbook hssfWorkbook = (HSSFWorkbook) workbook; HSSFPalette palette = hssfWorkbook.getCustomPalette(); HSSFColor hssfColor = palette.findColor((byte) rgbColor[0], (byte) rgbColor[1], (byte) rgbColor[2]); if (hssfColor == null ) { try { hssfColor = palette.addColor((byte) rgbColor[0], (byte) rgbColor[1], (byte) rgbColor[2]); } catch (Exception e) { HSSFColor similarColor = palette.findSimilarColor((int)rgbColor[0], (int)rgbColor[1], (int)rgbColor[2]); palette.setColorAtIndex(similarColor.getIndex(), (byte) rgbColor[0], (byte) rgbColor[1], (byte) rgbColor[2]); hssfColor = palette.getColor(similarColor.getIndex()); } } return hssfColor.getIndex(); } return HSSFColor.HSSFColorPredefined.WHITE.getIndex(); }