protected final Object getAttributeValue(Column column, Cell cell, A source, String key) { Map<String, AttributeSupplier<A>> map = getAttributeSupplierMap(); AttributeSupplier<A> supplier = map.get(key.toLowerCase()); if (supplier == null) { throw new UnsupportedOperationException(MessageFormat.format( "unsupported attribute name={0}, choose in {1}", key, new TreeSet<>(map.keySet()))); } Object value = supplier.get(column, cell, source); if (value instanceof Color) { int rgb = PoiExcelColorVisitor.getRGB((Color) value); if (rgb < 0) { return null; } if (column.getType() instanceof StringType) { value = String.format("%06x", rgb); } else { value = (long) rgb; } } return value; }
/** * Retrieve a <code>Font</code> from the cache with the given * properties. * @param fontBoldweight The font boldweight. * @param fontItalic Whether the font is italic. * @param fontColor The font color. * @param fontName The font name. * @param fontHeightInPoints The font height in points. * @param fontUnderline The font underline. * @param fontStrikeout Whether the font is in strikeout. * @param fontCharset The font charset. * @param fontTypeOffset The font type offset. * @return A <code>Font</code> that matches all given properties, or * <code>null</code> if it doesn't exist. */ public Font retrieveFont(short fontBoldweight, boolean fontItalic, Color fontColor, String fontName, short fontHeightInPoints, byte fontUnderline, boolean fontStrikeout, int fontCharset, short fontTypeOffset) { String representation = getRepresentation(fontBoldweight, fontItalic, fontColor, fontName, fontHeightInPoints, fontUnderline, fontStrikeout, fontCharset, fontTypeOffset ); Font f = myFontMap.get(representation); if (DEBUG) { if (f != null) System.err.println("FCache hit : " + representation); else System.err.println("FCache miss! : " + representation); } return f; }
/** * Gets the string representation of the given <code>Font</code>. * @param f A <code>Font</code>. * @return The string representation. */ private String getRepresentation(Font f) { // Colors that need an instanceof check Color fontColor; if (f instanceof HSSFFont) { HSSFFont hf = (HSSFFont) f; fontColor = hf.getHSSFColor((HSSFWorkbook) myWorkbook); } else if (f instanceof XSSFFont) { XSSFFont xf = (XSSFFont) f; fontColor = xf.getXSSFColor(); } else throw new IllegalArgumentException("Bad Font type: " + f.getClass().getName()); return getRepresentation(f.getBoldweight(), f.getItalic(), fontColor, f.getFontName(), f.getFontHeightInPoints(), f.getUnderline(), f.getStrikeout(), f.getCharSet(), f.getTypeOffset()); }
/** * Return the string representation of a <code>Font</code> with the * given properties. * @param fontBoldweight The font boldweight. * @param fontItalic Whether the font is italic. * @param fontColor The font color. * @param fontName The font name. * @param fontHeightInPoints The font height in points. * @param fontUnderline The font underline. * @param fontStrikeout Whether the font is in strikeout. * @param fontCharset The font charset. * @param fontTypeOffset The font type offset. * @return The string representation. */ private String getRepresentation(short fontBoldweight, boolean fontItalic, Color fontColor, String fontName, short fontHeightInPoints, byte fontUnderline, boolean fontStrikeout, int fontCharset, short fontTypeOffset) { StringBuilder buf = new StringBuilder(); buf.append(fontBoldweight).append(PROP_SEP); // Font italic buf.append(fontItalic).append(PROP_SEP); // Font color buf.append(SheetUtil.getColorHexString(fontColor)); // Font name buf.append(PROP_SEP).append(fontName); // Font height in points buf.append(PROP_SEP).append(fontHeightInPoints); // Font underline buf.append(PROP_SEP).append(fontUnderline); // Font strikeout buf.append(PROP_SEP).append(fontStrikeout); // Font charset buf.append(PROP_SEP).append(fontCharset); // Font type offset buf.append(PROP_SEP).append(fontTypeOffset); return buf.toString(); }
/** * Get the hex string that represents the <code>Color</code>. * @param color A POI <code>Color</code>. * @return The hex string that represents the <code>Color</code>. * @since 0.5.0 */ public static String getColorHexString(Color color) { if (color instanceof HSSFColor) { HSSFColor hssfColor = (HSSFColor) color; return getHSSFColorHexString(hssfColor); } else if (color == null) { return "null"; } else { throw new IllegalArgumentException("Unexpected type of Color: " + color.getClass().getName()); } }
/** * Creates a new <code>Font</code> for the given <code>Workbook</code>, * with the given attributes. Moved from <code>StyleTag</code> here for * 0.5.0. * @param workbook A <code>Workbook</code>. * @param fontBoldweight A <code>short</code> boldweight constant. * @param fontItalic Whether the text is italic. * @param fontColor A color <code>Color</code> opbject. * @param fontName A font name. * @param fontHeightInPoints A <code>short</code> font height in points. * @param fontUnderline A <code>byte</code> underline constant. * @param fontStrikeout Whether the font is strikeout. * @param fontCharset An <code>int</code> charset constant. * @param fontTypeOffset A <code>short</code> type offset constant. * @return A new <code>Font</code>. */ public static Font createFont(Workbook workbook, short fontBoldweight, boolean fontItalic, Color fontColor, String fontName, short fontHeightInPoints, byte fontUnderline, boolean fontStrikeout, int fontCharset, short fontTypeOffset) { if (DEBUG) { System.err.println("createFont: " + fontBoldweight + "," + fontItalic + "," + ((fontColor == null) ? "null" :fontColor.toString() // (fontColor instanceof HSSFColor) ? fontColor.toString() : // ((XSSFColor) fontColor).getCTColor().toString() ) + "," + fontName + "," + fontHeightInPoints + "," + fontUnderline + "," + fontStrikeout + "," + fontCharset + "," + fontTypeOffset); } Font f = workbook.createFont(); f.setBoldweight(fontBoldweight); f.setItalic(fontItalic); f.setFontName(fontName); f.setFontHeightInPoints(fontHeightInPoints); f.setUnderline(fontUnderline); f.setStrikeout(fontStrikeout); f.setCharSet(fontCharset); f.setTypeOffset(fontTypeOffset); // Color type check. if (fontColor instanceof HSSFColor) { f.setColor(((HSSFColor) fontColor).getIndex()); } return f; }
private Color getColor(Font font) { if (helper instanceof HSSFHtmlHelper) { return ((HSSFWorkbook) sheet.getWorkbook()).getCustomPalette() .getColor(font.getColor()); } else { return ((XSSFFont) font).getXSSFColor(); } }
public void styleColor(Formatter out, String attr, Color color) { if (color == null) { return; } HSSFColor hSSFColor = (HSSFColor) color; short[] rgb = hSSFColor.getTriplet(); out.format(" %s: #%02x%02x%02x; %n", attr, rgb[0], rgb[1], rgb[2]); }
public void styleColor(Formatter out, String attr, Color color) { XSSFColor xSSFColor = (XSSFColor) color; if (color == null || xSSFColor.isAuto()) return; byte[] rgb = xSSFColor.getRgb(); if (rgb == null) { return; } out.format(" %s: #%02x%02x%02x;%n", attr, rgb[0], rgb[1], rgb[2]); }
public void visitCellColor(Column column, Color color, CellVisitor visitor) { int rgb = getRGB(color); if (rgb < 0) { PageBuilder pageBuilder = visitorValue.getPageBuilder(); pageBuilder.setNull(column); return; } if (column.getType() instanceof StringType) { String s = String.format("%06x", rgb); visitor.visitCellValueString(column, color, s); } else { visitor.visitValueLong(column, color, rgb); } }
public static int getRGB(Color color) { if (color == null) { return -1; } int[] rgb = new int[3]; if (color instanceof HSSFColor) { HSSFColor hssf = (HSSFColor) color; short[] s = hssf.getTriplet(); rgb[0] = s[0] & 0xff; rgb[1] = s[1] & 0xff; rgb[2] = s[2] & 0xff; } else if (color instanceof XSSFColor) { XSSFColor xssf = (XSSFColor) color; byte[] b = xssf.getRGB(); if (b == null) { return -1; } rgb[0] = b[0] & 0xff; rgb[1] = b[1] & 0xff; rgb[2] = b[2] & 0xff; } else { throw new IllegalStateException(MessageFormat.format("unsupported POI color={0}", color)); } return (rgb[0] << 16) | (rgb[1] << 8) | rgb[2]; }
public void visitCellColor(Column column, short colorIndex, CellVisitor visitor) { Color color = getHssfColor(colorIndex); visitCellColor(column, color, visitor); }
public Color getHssfColor(short colorIndex) { HSSFWorkbook book = (HSSFWorkbook) visitorValue.getSheet().getWorkbook(); return getHssfColor(book, colorIndex); }
public static Color getHssfColor(Workbook workbook, short colorIndex) { HSSFWorkbook book = (HSSFWorkbook) workbook; HSSFPalette palette = book.getCustomPalette(); HSSFColor color = palette.getColor(colorIndex); return color; }
/** * Remove all borders from all cells in the region described by the left, * right, top, and bottom bounds. * @param sheet The <code>Sheet</code>. * @param left The 0-based index indicating the left-most part of the region. * @param right The 0-based index indicating the right-most part of the region. * @param top The 0-based index indicating the top-most part of the region. * @param bottom The 0-based index indicating the bottom-most part of the region. */ private void removeBorders(Sheet sheet, int left, int right, int top, int bottom) { if (DEBUG) System.err.println("removeBorders: " + left + ", " + right + ", " + top + ", " + bottom); CellStyleCache csCache = getWorkbookContext().getCellStyleCache(); for (int r = top; r <= bottom; r++) { Row row = sheet.getRow(r); for (int c = left; c <= right; c++) { Cell cell = row.getCell(c); if (cell != null) { CellStyle cs = cell.getCellStyle(); Font f = sheet.getWorkbook().getFontAt(cs.getFontIndex()); Color fontColor; if (cs instanceof HSSFCellStyle) { fontColor = ExcelColor.getHssfColorByIndex(f.getColor()); } else { fontColor = ((XSSFFont) f).getXSSFColor(); } // At this point, we have all of the desired CellStyle and Font // characteristics. Find a CellStyle if it exists. CellStyle foundStyle = csCache.retrieveCellStyle(f.getBoldweight(), f.getItalic(), fontColor, f.getFontName(), f.getFontHeightInPoints(), cs.getAlignment(), CellStyle.BORDER_NONE, CellStyle.BORDER_NONE, CellStyle.BORDER_NONE, CellStyle.BORDER_NONE, cs.getDataFormatString(), f.getUnderline(), f.getStrikeout(), cs.getWrapText(), cs.getFillBackgroundColorColor(), cs.getFillForegroundColorColor(), cs.getFillPattern(), cs.getVerticalAlignment(), cs.getIndention(), cs.getRotation(), null, null, null, null, f.getCharSet(), f.getTypeOffset(), cs.getLocked(), cs.getHidden()); if (foundStyle == null) { foundStyle = SheetUtil.createCellStyle(sheet.getWorkbook(), cs.getAlignment(), CellStyle.BORDER_NONE, CellStyle.BORDER_NONE, CellStyle.BORDER_NONE, CellStyle.BORDER_NONE, cs.getDataFormatString(), cs.getWrapText(), cs.getFillBackgroundColorColor(), cs.getFillForegroundColorColor(), cs.getFillPattern(), cs.getVerticalAlignment(), cs.getIndention(), cs.getRotation(), null, null, null, null, cs.getLocked(), cs.getHidden()); foundStyle.setFont(f); } cell.setCellStyle(foundStyle); } } } }
/** * Puts back borders for the newly sized merged region. * @param sheet The <code>Sheet</code>. * @param left The 0-based index indicating the left-most part of the region. * @param right The 0-based index indicating the right-most part of the region. * @param top The 0-based index indicating the top-most part of the region. * @param bottom The 0-based index indicating the bottom-most part of the region. * @param borderLeft The left border type. * @param borderRight The right border type. * @param borderTop The top border type. * @param borderBottom The bottom border type. * @param borderLeftColor The left border color. * @param borderRightColor The right border color. * @param borderTopColor The top border color. * @param borderBottomColor The bottom border color. */ private void putBackBorders(Sheet sheet, int left, int right, int top, int bottom, short borderLeft, short borderRight, short borderTop, short borderBottom, Color borderLeftColor, Color borderRightColor, Color borderTopColor, Color borderBottomColor) { if (DEBUG) System.err.println("putBackBorders: " + left + ", " + right + ", " + top + ", " + bottom); CellStyleCache csCache = getWorkbookContext().getCellStyleCache(); for (int r = top; r <= bottom; r++) { Row row = sheet.getRow(r); if (row == null) row = sheet.createRow(r); for (int c = left; c <= right; c++) { Cell cell = row.getCell(c); if (cell == null) cell = row.createCell(c); CellStyle cs = cell.getCellStyle(); Font f = sheet.getWorkbook().getFontAt(cs.getFontIndex()); Color fontColor; if (cs instanceof HSSFCellStyle) { fontColor = ExcelColor.getHssfColorByIndex(f.getColor()); } else { fontColor = ((XSSFFont) f).getXSSFColor(); } short newBorderBottom = (r == bottom) ? borderBottom : CellStyle.BORDER_NONE; short newBorderLeft = (c == left) ? borderLeft : CellStyle.BORDER_NONE; short newBorderRight = (c == right) ? borderRight : CellStyle.BORDER_NONE; short newBorderTop = (r == top) ? borderTop : CellStyle.BORDER_NONE; Color newBorderBottomColor = (r == bottom) ? borderBottomColor : null; Color newBorderLeftColor = (c == left) ? borderLeftColor : null; Color newBorderRightColor = (c == right) ? borderRightColor : null; Color newBorderTopColor = (r == top) ? borderTopColor : null; // At this point, we have all of the desired CellStyle and Font // characteristics. Find a CellStyle if it exists. CellStyle foundStyle = csCache.retrieveCellStyle(f.getBoldweight(), f.getItalic(), fontColor, f.getFontName(), f.getFontHeightInPoints(), cs.getAlignment(), newBorderBottom, newBorderLeft, newBorderRight, newBorderTop, cs.getDataFormatString(), f.getUnderline(), f.getStrikeout(), cs.getWrapText(), cs.getFillBackgroundColorColor(), cs.getFillForegroundColorColor(), cs.getFillPattern(), cs.getVerticalAlignment(), cs.getIndention(), cs.getRotation(), newBorderBottomColor, newBorderLeftColor, newBorderRightColor, newBorderTopColor, f.getCharSet(), f.getTypeOffset(), cs.getLocked(), cs.getHidden()); if (foundStyle == null) { foundStyle = SheetUtil.createCellStyle(sheet.getWorkbook(), cs.getAlignment(), newBorderBottom, newBorderLeft, newBorderRight, newBorderTop, cs.getDataFormatString(), cs.getWrapText(), cs.getFillBackgroundColorColor(), cs.getFillForegroundColorColor(), cs.getFillPattern(), cs.getVerticalAlignment(), cs.getIndention(), cs.getRotation(), newBorderBottomColor, newBorderLeftColor, newBorderRightColor, newBorderTopColor, cs.getLocked(), cs.getHidden()); foundStyle.setFont(f); } cell.setCellStyle(foundStyle); } } }
/** * Retrieve a <code>CellStyle</code> from the cache with the given * properties. * @param fontBoldweight The font boldweight. * @param fontItalic Whether the font is italic. * @param fontColor The font color. * @param fontName The font name. * @param fontHeightInPoints The font height in points. * @param alignment The horizontal alignment. * @param borderBottom The bottom border type. * @param borderLeft The left border type. * @param borderRight The right border type. * @param borderTop The top border type. * @param dataFormat The data format string. * @param fontUnderline The font underline. * @param fontStrikeout Whether the font is in strikeout. * @param wrapText Whether text is wrapped. * @param fillBackgroundColor The fill background color. * @param fillForegroundColor The fill foreground color. * @param fillPattern The fill pattern. * @param verticalAlignment The vertical alignment. * @param indention How many characters the text is indented. * @param rotation How many degrees the text is rotated. * @param bottomBorderColor The bottom border color. * @param leftBorderColor The left border color. * @param rightBorderColor The right border color. * @param topBorderColor The top border color. * @param fontCharset The font charset. * @param fontTypeOffset The font type offset. * @param locked Whether the cell is "locked". * @param hidden Whether the cell is "hidden". * @return A <code>CellStyle</code> that matches all given properties, or * <code>null</code> if it doesn't exist. */ public CellStyle retrieveCellStyle(short fontBoldweight, boolean fontItalic, Color fontColor, String fontName, short fontHeightInPoints, short alignment, short borderBottom, short borderLeft, short borderRight, short borderTop, String dataFormat, byte fontUnderline, boolean fontStrikeout, boolean wrapText, Color fillBackgroundColor, Color fillForegroundColor, short fillPattern, short verticalAlignment, short indention, short rotation, Color bottomBorderColor, Color leftBorderColor, Color rightBorderColor, Color topBorderColor, int fontCharset, short fontTypeOffset, boolean locked, boolean hidden) { String representation = getRepresentation(fontBoldweight, fontItalic, fontColor, fontName, fontHeightInPoints, alignment, borderBottom, borderLeft, borderRight, borderTop, dataFormat, fontUnderline, fontStrikeout, wrapText, fillBackgroundColor, fillForegroundColor, fillPattern, verticalAlignment, indention, rotation, bottomBorderColor, leftBorderColor, rightBorderColor, topBorderColor, fontCharset, fontTypeOffset, locked, hidden ); CellStyle cs = myCellStyleMap.get(representation); if (DEBUG) { if (cs != null) System.err.println("CSCache hit : " + representation); else System.err.println("CSCache miss!: " + representation); } return cs; }
/** * Gets the string representation of the given <code>CellStyle</code>. * @param cs A <code>CellStyle</code>. * @return The string representation. */ private String getRepresentation(CellStyle cs) { Font f = myWorkbook.getFontAt(cs.getFontIndex()); // Colors that need an instanceof check Color fontColor; Color bottomColor = null; Color leftColor = null; Color rightColor = null; Color topColor = null; if (cs instanceof HSSFCellStyle) { HSSFFont hf = (HSSFFont) f; fontColor = hf.getHSSFColor((HSSFWorkbook) myWorkbook); // HSSF only stores border colors if the borders aren't "NONE". if (cs.getBorderBottom() != CellStyle.BORDER_NONE) bottomColor = ExcelColor.getHssfColorByIndex(cs.getBottomBorderColor()); if (cs.getBorderLeft() != CellStyle.BORDER_NONE) leftColor = ExcelColor.getHssfColorByIndex(cs.getLeftBorderColor()); if (cs.getBorderRight() != CellStyle.BORDER_NONE) rightColor = ExcelColor.getHssfColorByIndex(cs.getRightBorderColor()); if (cs.getBorderTop() != CellStyle.BORDER_NONE) topColor = ExcelColor.getHssfColorByIndex(cs.getTopBorderColor()); } else if (cs instanceof XSSFCellStyle) { XSSFFont xf = (XSSFFont) f; fontColor = xf.getXSSFColor(); XSSFCellStyle xcs = (XSSFCellStyle) cs; bottomColor = xcs.getBottomBorderXSSFColor(); leftColor = xcs.getLeftBorderXSSFColor(); rightColor = xcs.getRightBorderXSSFColor(); topColor = xcs.getTopBorderXSSFColor(); } else throw new IllegalArgumentException("Bad CellStyle type: " + cs.getClass().getName()); return getRepresentation(f.getBoldweight(), f.getItalic(), fontColor, f.getFontName(), f.getFontHeightInPoints(), cs.getAlignment(), cs.getBorderBottom(), cs.getBorderLeft(), cs.getBorderRight(), cs.getBorderTop(), cs.getDataFormatString(), f.getUnderline(), f.getStrikeout(), cs.getWrapText(), cs.getFillBackgroundColorColor(), cs.getFillBackgroundColorColor(), cs.getFillPattern(), cs.getVerticalAlignment(), cs.getIndention(), cs.getRotation(), bottomColor, leftColor, rightColor, topColor, f.getCharSet(), f.getTypeOffset(), cs.getLocked(), cs.getHidden()); }
/** * Creates a new <code>CellStyle</code> for the given <code>Workbook</code>, * with the given attributes. Moved from <code>StyleTag</code> here for * 0.5.0. * @param workbook A <code>Workbook</code>. * @param alignment A <code>short</code> alignment constant. * @param borderBottom A <code>short</code> border type constant. * @param borderLeft A <code>short</code> border type constant. * @param borderRight A <code>short</code> border type constant. * @param borderTop A <code>short</code> border type constant. * @param dataFormat A data format string. * @param wrapText Whether text is wrapped. * @param fillBackgroundColor A background <code>Color</code>. * @param fillForegroundColor A foreground <code>Color</code>. * @param fillPattern A <code>short</code> pattern constant. * @param verticalAlignment A <code>short</code> vertical alignment constant. * @param indention A <code>short</code> number of indent characters. * @param rotationDegrees A <code>short</code> degrees rotation of text. * @param bottomBorderColor A border <code>Color</code> object. * @param leftBorderColor A border <code>Color</code> object. * @param rightBorderColor A border <code>Color</code> object. * @param topBorderColor A border <code>Color</code> object. * @param locked Whether the cell is locked. * @param hidden Whether the cell is hidden. * @return A new <code>CellStyle</code>. */ public static CellStyle createCellStyle(Workbook workbook, short alignment, short borderBottom, short borderLeft, short borderRight, short borderTop, String dataFormat, boolean wrapText, Color fillBackgroundColor, Color fillForegroundColor, short fillPattern, short verticalAlignment, short indention, short rotationDegrees, Color bottomBorderColor, Color leftBorderColor, Color rightBorderColor, Color topBorderColor, boolean locked, boolean hidden) { CellStyle cs = workbook.createCellStyle(); cs.setAlignment(alignment); cs.setBorderBottom(borderBottom); cs.setBorderLeft(borderLeft); cs.setBorderRight(borderRight); cs.setBorderTop(borderTop); cs.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat(dataFormat)); cs.setHidden(hidden); cs.setIndention(indention); cs.setLocked(locked); cs.setRotation(rotationDegrees); cs.setVerticalAlignment(verticalAlignment); cs.setWrapText(wrapText); // Certain properties need a type of workbook check. if (workbook instanceof HSSFWorkbook) { if (bottomBorderColor != null) cs.setBottomBorderColor(((HSSFColor) bottomBorderColor).getIndex()); if (leftBorderColor != null) cs.setLeftBorderColor(((HSSFColor) leftBorderColor).getIndex()); if (rightBorderColor != null) cs.setRightBorderColor(((HSSFColor) rightBorderColor).getIndex()); if (topBorderColor != null) cs.setTopBorderColor(((HSSFColor) topBorderColor).getIndex()); // Per POI Javadocs, set foreground color first! cs.setFillForegroundColor(((HSSFColor) fillForegroundColor).getIndex()); cs.setFillBackgroundColor(((HSSFColor) fillBackgroundColor).getIndex()); } else { // XSSFWorkbook XSSFCellStyle xcs = (XSSFCellStyle) cs; if (bottomBorderColor != null) xcs.setBottomBorderColor((XSSFColor) bottomBorderColor); if (leftBorderColor != null) xcs.setLeftBorderColor((XSSFColor) leftBorderColor); if (rightBorderColor != null) xcs.setRightBorderColor((XSSFColor) rightBorderColor); if (topBorderColor != null) xcs.setTopBorderColor((XSSFColor) topBorderColor); // Per POI Javadocs, set foreground color first! if (fillForegroundColor != null) xcs.setFillForegroundColor((XSSFColor) fillForegroundColor); if (fillBackgroundColor != null) xcs.setFillBackgroundColor((XSSFColor) fillBackgroundColor); } cs.setFillPattern(fillPattern); return cs; }
@Override public java.awt.Color asColor() { return null; }
void styleColor(Formatter out, String attr, Color color);