Refine search
public HSSFColor setColor(HSSFWorkbook workbook, byte r,byte g, byte b){ HSSFPalette palette = workbook.getCustomPalette(); HSSFColor hssfColor = null; try { hssfColor= palette.findColor(r, g, b); if (hssfColor == null ){ palette.setColorAtIndex(HSSFColor.LAVENDER.index, r, g,b); hssfColor = palette.getColor(HSSFColor.LAVENDER.index); } } catch (Exception e) { logger.error(e); } return hssfColor; }
HSSFWorkbook hwb = new HSSFWorkbook(); HSSFPalette palette = hwb.getCustomPalette(); // get the color which most closely matches the color you want to use HSSFColor myColor = palette.findSimilarColor(255, 0, 0); // get the palette index of that color short palIndex = myColor.getIndex(); // code to get the style for the cell goes here style.setFillForegroundColor(palIndex);
@Override public HSSFColor getFillBackgroundColorColor() { HSSFPalette pallette = new HSSFPalette( _workbook.getCustomPalette() ); return pallette.getColor( getFillBackgroundColor() ); }
/** * Finds the closest matching color in the custom palette. The * method for finding the distance between the colors is fairly * primative. * * @param red The red component of the color to match. * @param green The green component of the color to match. * @param blue The blue component of the color to match. * @return The closest color or null if there are no custom * colors currently defined. */ public HSSFColor findSimilarColor(byte red, byte green, byte blue) { return findSimilarColor(unsignedInt(red), unsignedInt(green), unsignedInt(blue)); } /**
HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); HSSFRow row = sheet.createRow((short) 0); HSSFCell cell = row.createCell((short) 0); HSSFCellStyle style = wb.createCellStyle(); style.setFillForegroundColor(HSSFColor.LIME.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); palette.setColorAtIndex(HSSFColor.RED.index, (byte) 153, //RGB red (0-255) (byte) 0, //RGB green ); palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
wb = new HSSFWorkbook(); sheet = wb.createSheet(); wb.setActiveSheet(0); BufferedImage img = null; try { HSSFColor hssfColor = null; try { hssfColor= palette.findSimilarColor(r, g, b); if (hssfColor == null ){ palette.setColorAtIndex(HSSFColor.LAVENDER.index, r, g,b); hssfColor = palette.getColor(HSSFColor.LAVENDER.index);
int[][] data = handler.convertImageToRGB(); Workbook book = new HSSFWorkbook(); Sheet sheet = book.createSheet("drawing"); Map<String, CellStyle> colorMap = new HashMap<String, CellStyle>(); palette.setColorAtIndex(HSSFColor.LAVENDER.index, (byte)rgb[0], (byte)rgb[1], (byte)rgb[2]); HSSFColor color = palette.findSimilarColor(rgb[0], rgb[1], rgb[2]); return color.getIndex();
+ "</ul>"; HSSFWorkbook workBook = new HSSFWorkbook(); HSSFSheet sheet = workBook.createSheet("Html Text"); if (!isEmpty(fontValue)) { HSSFPalette palette = workBook.getCustomPalette(); HSSFColor myColor = palette.findSimilarColor( Integer.valueOf(fontValue.substring(2, 4), 16), Integer.valueOf(fontValue.substring(4, 6), 16),
@Override public HSSFColor getTopBorderColorColor() { return workbook.getCustomPalette().getColor( borderFormatting.getTopBorderColor() ); }
private HSSFFont matchFont( Font matchFont ) { HSSFColor hssfColor = workbook.getCustomPalette() .findColor((byte)foreground.getRed(), (byte)foreground.getGreen(), (byte)foreground.getBlue()); if (hssfColor == null) hssfColor = workbook.getCustomPalette().findSimilarColor((byte)foreground.getRed(), (byte)foreground.getGreen(), (byte)foreground.getBlue()); boolean bold = (matchFont.getStyle() & Font.BOLD) != 0; boolean italic = (matchFont.getStyle() & Font.ITALIC) != 0; HSSFFont hssfFont = workbook.findFont(bold, hssfColor.getIndex(), (short)(matchFont.getSize() * 20), matchFont.getName(), italic, false, (short)0, (byte)0); if (hssfFont == null) { hssfFont = workbook.createFont(); hssfFont.setBold(bold); hssfFont.setColor(hssfColor.getIndex()); hssfFont.setFontHeight((short)(matchFont.getSize() * 20)); hssfFont.setFontName(matchFont.getName()); hssfFont.setItalic(italic); hssfFont.setStrikeout(false); hssfFont.setTypeOffset((short) 0); hssfFont.setUnderline((byte) 0); } return hssfFont; }
response.setHeader("Content-Disposition", "Attachment;Filename=\""+"ExcelExport"+"."+"xls" + "\""); HSSFSheet sheet = workbook.createSheet("Software Contract Database"); HSSFCellStyle headerStyle = workbook.createCellStyle(); HSSFCellStyle dateStyle = workbook.createCellStyle(); HSSFCellStyle datePatternStyle = workbook.createCellStyle(); HSSFDataFormat format1 = workbook.createDataFormat(); HSSFFont font = workbook.createFont(); HSSFPalette palette = workbook.getCustomPalette(); palette.setColorAtIndex(HSSFColor.LAVENDER.index, (byte) 204, (byte) 204,
private HSSFCellStyle createHeaderStyle(HSSFWorkbook myWorkBook) { Font headerFont = myWorkBook.createFont(); headerFont.setColor(Font.COLOR_NORMAL); HSSFPalette palette = myWorkBook.getCustomPalette(); HSSFColor headerColor = palette.findSimilarColor((byte) 149, (byte) 179, (byte) 215); HSSFCellStyle headerStyle = myWorkBook.createCellStyle(); headerStyle.setFillForegroundColor(headerColor.getIndex()); headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); headerStyle.setFont(headerFont); headerStyle.setWrapText(true); return headerStyle; }
/** * parse color * @param workBook work book * @param color string color * @return HSSFColor */ public static HSSFColor parseColor(HSSFWorkbook workBook, String color) { HSSFColor poiColor = null; if (StringUtils.isNotBlank(color)) { Color awtColor = Color.decode(color); if (awtColor != null) { int r = awtColor.getRed(); int g = awtColor.getGreen(); int b = awtColor.getBlue(); HSSFPalette palette = workBook.getCustomPalette(); poiColor = palette.findColor((byte) r, (byte) g, (byte) b); if (poiColor == null) { poiColor = palette.findSimilarColor(r, g, b); } } } return poiColor; }
HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette(); Color color = null; if (facetBackground != null) { color = Color.decode(facetBackground); HSSFColor backgroundColor = palette.findSimilarColor(color.getRed(), color.getGreen(), color.getBlue()); ((HSSFCellStyle) facetStyle).setFillForegroundColor(backgroundColor.getIndex()); facetStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); if (facetFontColor != null) { color = Color.decode(facetFontColor); HSSFColor facetColor = palette.findSimilarColor(color.getRed(), color.getGreen(), color.getBlue()); ((HSSFFont) facetFont).setColor(facetColor.getIndex());
/** * Adds a new color into an empty color slot. * @param red The red component * @param green The green component * @param blue The blue component * * @return The new custom color. * * @throws RuntimeException if there are more more free color indexes. */ public HSSFColor addColor( byte red, byte green, byte blue ) { byte[] b = _palette.getColor(PaletteRecord.FIRST_COLOR_INDEX); short i; for (i = PaletteRecord.FIRST_COLOR_INDEX; i < PaletteRecord.STANDARD_PALETTE_SIZE + PaletteRecord.FIRST_COLOR_INDEX; b = _palette.getColor(++i)) { if (b == null) { setColorAtIndex( i, red, green, blue ); return getColor(i); } } throw new RuntimeException("Could not find free color index"); }
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(); }
/** * Retrieves the color at a given index * * @param index the palette index, between 0x8 to 0x40 inclusive * @return the color, or null if the index is not populated */ public HSSFColor getColor(int index) { return getColor((short)index); }
private void replaceColorsPallete(Map<HSSFColor, HSSFColor> colorsReplaceMap, Workbook wb) { if (! (wb instanceof HSSFWorkbook)) { return; } HSSFWorkbook hssfWb = (HSSFWorkbook) wb; final HSSFPalette customPalette = hssfWb.getCustomPalette(); for (Entry<HSSFColor, HSSFColor> e : colorsReplaceMap.entrySet()) { short[] rgb = e.getValue().getTriplet(); customPalette.setColorAtIndex(e.getKey().getIndex(), (byte)rgb[0], (byte)rgb[1], (byte)rgb[2] ); } }
/** * Finds the closest matching color in the custom palette. The * method for finding the distance between the colors is fairly * primative. * * @param red The red component of the color to match. * @param green The green component of the color to match. * @param blue The blue component of the color to match. * @return The closest color or null if there are no custom * colors currently defined. */ public HSSFColor findSimilarColor(int red, int green, int blue) { HSSFColor result = null; int minColorDistance = Integer.MAX_VALUE; byte[] b = _palette.getColor(PaletteRecord.FIRST_COLOR_INDEX); for (short i = PaletteRecord.FIRST_COLOR_INDEX; b != null; b = _palette.getColor(++i)) { int colorDistance = Math.abs(red - unsignedInt(b[0])) + Math.abs(green - unsignedInt(b[1])) + Math.abs(blue - unsignedInt(b[2])); if (colorDistance < minColorDistance) { minColorDistance = colorDistance; result = getColor(i); } } return result; }
//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);