Example usage for org.apache.poi.hssf.util HSSFColor getTriplet

List of usage examples for org.apache.poi.hssf.util HSSFColor getTriplet

Introduction

In this page you can find the example usage for org.apache.poi.hssf.util HSSFColor getTriplet.

Prototype


public short[] getTriplet() 

Source Link

Document

returns RGB triplet (0, 0, 0)

Usage

From source file:com.vaadin.addon.spreadsheet.HSSFColorConverter.java

License:Open Source License

private String styleColor(short index) {
    HSSFColor color = colors.getColor(index);
    if (index != HSSF_AUTO.getIndex() && color != null) {
        short[] rgb = color.getTriplet();
        return (String.format("#%02x%02x%02x;", rgb[0], rgb[1], rgb[2]));
    }/*from   w w w.j  a  v a 2s.co  m*/
    return null;
}

From source file:com.vaadin.addon.spreadsheet.HSSFColorConverter.java

License:Open Source License

private void styleBorderColor(final StringBuilder sb, String attr, short index) {
    HSSFColor color = colors.getColor(index);
    sb.append(attr);/*from   w w w . j ava 2  s .co m*/
    sb.append(":");
    if (index != HSSF_AUTO.getIndex() && color != null) {
        short[] rgb = color.getTriplet();
        sb.append(String.format("#%02x%02x%02x;", rgb[0], rgb[1], rgb[2]));
    } else {
        sb.append("#000;");
    }
}

From source file:com.vaadin.addon.spreadsheet.HSSFColorConverter.java

License:Open Source License

@SuppressWarnings("unused")
private void styleColor(final StringBuilder sb, String attr, short index) {
    HSSFColor color = colors.getColor(index);
    if (index != HSSF_AUTO.getIndex() && color != null) {
        short[] rgb = color.getTriplet();
        sb.append(attr);// www.j  av  a  2s  .  c o m
        sb.append(":");
        sb.append(String.format("#%02x%02x%02x;", rgb[0], rgb[1], rgb[2]));
    }
}

From source file:gov.nih.nci.evs.browser.utils.ResolvedValueSetIteratorHolder.java

License:Open Source License

/**
 * (Each Excel sheet cell becomes an HTML table cell) Generates an HTML
 * table cell which has the same font styles, alignments, colours and
 * borders as the Excel cell.//  w  w w  .j  a va  2 s . c o  m
 *
 * @param cell
 *            The Excel cell.
 */

private void td(final HSSFCell cell) {
    int colspan = 1;
    if (colIndex == mergeStart) {
        // First cell in the merging region - set colspan.
        colspan = mergeEnd - mergeStart + 1;
    } else if (colIndex == mergeEnd) {
        // Last cell in the merging region - no more skipped cells.
        mergeStart = -1;
        mergeEnd = -1;
        return;
    } else if (mergeStart != -1 && mergeEnd != -1 && colIndex > mergeStart && colIndex < mergeEnd) {
        // Within the merging region - skip the cell.
        return;
    }
    //KLO 05022018
    //out.append("<td ");

    out.append("<td height=\"15\" ");
    if (colspan > 1) {
        out.append("colspan='").append(colspan).append("' ");
    }
    if (cell == null) {
        out.append("/>\n");
        return;
    }

    out.append("style='");
    final HSSFCellStyle style = cell.getCellStyle();
    // Text alignment
    switch (style.getAlignment()) {
    case CellStyle.ALIGN_LEFT:
        out.append("text-align: left; ");
        break;
    case CellStyle.ALIGN_RIGHT:
        out.append("text-align: right; ");
        break;
    case CellStyle.ALIGN_CENTER:
        out.append("text-align: center; ");
        break;
    default:
        break;
    }

    // Font style, size and weight
    final HSSFFont font = style.getFont(book);
    if (font == null)
        return;
    if (font.getBoldweight() == HSSFFont.BOLDWEIGHT_BOLD) {
        out.append("font-weight: bold; ");
    }
    if (font.getItalic()) {
        out.append("font-style: italic; ");
    }
    if (font.getUnderline() != HSSFFont.U_NONE) {
        out.append("text-decoration: underline; ");
    }
    out.append("font-size: ").append(Math.floor(font.getFontHeightInPoints() * 0.8)).append("pt; ");
    // Cell background and font colours
    final short[] backRGB = style.getFillForegroundColorColor().getTriplet();
    final HSSFColor foreColor = palette.getColor(font.getColor());
    if (foreColor != null) {
        final short[] foreRGB = foreColor.getTriplet();
        if (foreRGB[0] != 0 || foreRGB[1] != 0 || foreRGB[2] != 0) {
            out.append("color: rgb(").append(foreRGB[0]).append(',').append(foreRGB[1]).append(',')
                    .append(foreRGB[2]).append(");");
        }
    }
    if (backRGB[0] != 0 || backRGB[1] != 0 || backRGB[2] != 0) {
        out.append("background-color: rgb(").append(backRGB[0]).append(',').append(backRGB[1]).append(',')
                .append(backRGB[2]).append(");");
    }
    // Border
    if (style.getBorderTop() != HSSFCellStyle.BORDER_NONE) {
        out.append("border-top-style: solid; ");
    }
    if (style.getBorderRight() != HSSFCellStyle.BORDER_NONE) {
        out.append("border-right-style: solid; ");
    }
    if (style.getBorderBottom() != HSSFCellStyle.BORDER_NONE) {
        out.append("border-bottom-style: solid; ");
    }
    if (style.getBorderLeft() != HSSFCellStyle.BORDER_NONE) {
        out.append("border-left-style: solid; ");
    }
    out.append("'>");
    String val = "";
    try {
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_STRING:
            val = cell.getStringCellValue();
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            // POI does not distinguish between integer and double, thus:
            final double original = cell.getNumericCellValue(), rounded = Math.round(original);
            if (Math.abs(rounded - original) < 0.00000000000000001) {
                val = String.valueOf((int) rounded);
            } else {
                val = String.valueOf(original);
            }
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            final CellValue cv = evaluator.evaluate(cell);
            if (cv == null)
                return;
            switch (cv.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                out.append(cv.getBooleanValue());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                out.append(cv.getNumberValue());
                break;
            case Cell.CELL_TYPE_STRING:
                out.append(cv.getStringValue());
                break;
            case Cell.CELL_TYPE_BLANK:
                break;
            case Cell.CELL_TYPE_ERROR:
                break;
            default:
                break;
            }
            break;
        default:
            // Neither string or number? Could be a date.
            try {
                val = sdf.format(cell.getDateCellValue());
            } catch (final Exception e1) {
            }
        }
    } catch (final Exception e) {
        val = e.getMessage();
    }
    if ("null".equals(val)) {
        val = "";
    }
    if (pix.containsKey(rowIndex)) {
        if (pix.get(rowIndex).containsKey(colIndex)) {
            for (final HSSFPictureData pic : pix.get(rowIndex).get(colIndex)) {
                out.append("<img alt='Image in Excel sheet' src='data:");
                out.append(pic.getMimeType());
                out.append(";base64,");
                try {
                    out.append(new String(Base64.encodeBase64(pic.getData()), "US-ASCII"));
                } catch (final UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                out.append("'/>");
            }
        }
    }

    if (isCode(val) && this.URL != null) {
        val = getHyperLink(val);
    }

    out.append(val);
    out.append("</td>\n");
}

From source file:gov.nih.nci.evs.browser.utils.ResolvedValueSetIteratorHolder.java

License:Open Source License

private void td(final HSSFCell cell, StringBuffer buf) {
    int colspan = 1;
    if (colIndex == mergeStart) {
        // First cell in the merging region - set colspan.
        colspan = mergeEnd - mergeStart + 1;
    } else if (colIndex == mergeEnd) {
        // Last cell in the merging region - no more skipped cells.
        mergeStart = -1;//from w  w w  .j  a v a 2s . c  o  m
        mergeEnd = -1;
        return;
    } else if (mergeStart != -1 && mergeEnd != -1 && colIndex > mergeStart && colIndex < mergeEnd) {
        // Within the merging region - skip the cell.
        return;
    }

    //KLO 05022018
    //buf.append("<td ");

    buf.append("<td height=\"15\" ");

    if (colspan > 1) {
        buf.append("colspan='").append(colspan).append("' ");
    }
    if (cell == null) {
        buf.append("/>");
        return;
    }
    buf.append("style='");
    final HSSFCellStyle style = cell.getCellStyle();
    // Text alignment
    switch (style.getAlignment()) {
    case CellStyle.ALIGN_LEFT:
        buf.append("text-align: left; ");
        break;
    case CellStyle.ALIGN_RIGHT:
        buf.append("text-align: right; ");
        break;
    case CellStyle.ALIGN_CENTER:
        buf.append("text-align: center; ");
        break;
    default:
        break;
    }
    // Font style, size and weight
    final HSSFFont font = style.getFont(book);
    if (font == null)
        return;
    if (font.getBoldweight() == HSSFFont.BOLDWEIGHT_BOLD) {
        buf.append("font-weight: bold; ");
    }
    if (font.getItalic()) {
        buf.append("font-style: italic; ");
    }
    if (font.getUnderline() != HSSFFont.U_NONE) {
        buf.append("text-decoration: underline; ");
    }
    buf.append("font-size: ").append(Math.floor(font.getFontHeightInPoints() * 0.8)).append("pt; ");

    // Cell background and font colours
    final short[] backRGB = style.getFillForegroundColorColor().getTriplet();
    final HSSFColor foreColor = palette.getColor(font.getColor());
    if (foreColor != null) {
        final short[] foreRGB = foreColor.getTriplet();
        if (foreRGB[0] != 0 || foreRGB[1] != 0 || foreRGB[2] != 0) {
            buf.append("color: rgb(").append(foreRGB[0]).append(',').append(foreRGB[1]).append(',')
                    .append(foreRGB[2]).append(");");

        }
    }
    if (backRGB[0] != 0 || backRGB[1] != 0 || backRGB[2] != 0) {
        buf.append("background-color: rgb(").append(backRGB[0]).append(',').append(backRGB[1]).append(',')
                .append(backRGB[2]).append(");");

    }
    // Border
    if (style.getBorderTop() != HSSFCellStyle.BORDER_NONE) {
        buf.append("border-top-style: solid; ");
    }
    if (style.getBorderRight() != HSSFCellStyle.BORDER_NONE) {
        buf.append("border-right-style: solid; ");
    }
    if (style.getBorderBottom() != HSSFCellStyle.BORDER_NONE) {
        buf.append("border-bottom-style: solid; ");
    }
    if (style.getBorderLeft() != HSSFCellStyle.BORDER_NONE) {
        buf.append("border-left-style: solid; ");
    }
    buf.append("'>");
    String val = "";
    try {
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_STRING:
            val = cell.getStringCellValue();
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            // POI does not distinguish between integer and double, thus:
            final double original = cell.getNumericCellValue(), rounded = Math.round(original);
            if (Math.abs(rounded - original) < 0.00000000000000001) {
                val = String.valueOf((int) rounded);
            } else {
                val = String.valueOf(original);
            }
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            final CellValue cv = evaluator.evaluate(cell);
            if (cv == null)
                return;
            switch (cv.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                buf.append(cv.getBooleanValue());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                buf.append(cv.getNumberValue());
                break;
            case Cell.CELL_TYPE_STRING:
                buf.append(cv.getStringValue());
                break;
            case Cell.CELL_TYPE_BLANK:
                break;
            case Cell.CELL_TYPE_ERROR:
                break;
            default:
                break;
            }
            break;
        default:
            // Neither string or number? Could be a date.
            try {
                val = sdf.format(cell.getDateCellValue());
            } catch (final Exception e1) {
            }
        }
    } catch (final Exception e) {
        val = e.getMessage();
    }
    if ("null".equals(val)) {
        val = "";
    }
    if (pix.containsKey(rowIndex)) {
        if (pix.get(rowIndex).containsKey(colIndex)) {
            for (final HSSFPictureData pic : pix.get(rowIndex).get(colIndex)) {
                buf.append("<img alt='Image in Excel sheet' src='data:");
                buf.append(pic.getMimeType());
                buf.append(";base64,");

                try {
                    buf.append(new String(Base64.encodeBase64(pic.getData()), "US-ASCII"));

                } catch (final UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                buf.append("'/>");
            }
        }
    }

    if (isCode(val) && this.URL != null) {
        val = getHyperLink(val);
    }
    buf.append(val);
    buf.append("</td>");

}

From source file:net.sf.jasperreports.engine.export.JRXlsExporter.java

License:Open Source License

/**
 *
 *///  w  ww.j  ava2  s . co  m
protected static HSSFColor getNearestColor(Color awtColor) {
    HSSFColor color = hssfColorsCache.get(awtColor);
    if (color == null) {
        Map<?, ?> triplets = HSSFColor.getTripletHash();
        if (triplets != null) {
            Collection<?> keys = triplets.keySet();
            if (keys != null && keys.size() > 0) {
                Object key = null;
                HSSFColor crtColor = null;
                short[] rgb = null;
                int diff = 0;
                int minDiff = 999;
                for (Iterator<?> it = keys.iterator(); it.hasNext();) {
                    key = it.next();

                    crtColor = (HSSFColor) triplets.get(key);
                    rgb = crtColor.getTriplet();

                    diff = Math.abs(rgb[0] - awtColor.getRed()) + Math.abs(rgb[1] - awtColor.getGreen())
                            + Math.abs(rgb[2] - awtColor.getBlue());

                    if (diff < minDiff) {
                        minDiff = diff;
                        color = crtColor;
                    }
                }
            }
        }

        hssfColorsCache.put(awtColor, color);
    }
    return color;
}

From source file:net.sf.jasperreports.engine.export.JRXlsMetadataExporter.java

License:Open Source License

/**
 *
 *///  w  ww  . j a v a  2 s. c o  m
protected static HSSFColor getNearestColor(Color awtColor) {
    HSSFColor color = hssfColorsCache.get(awtColor);
    if (color == null) {
        Map<?, ?> triplets = HSSFColor.getTripletHash();
        if (triplets != null) {
            Collection<?> keys = triplets.keySet();
            if (keys != null && keys.size() > 0) {
                Object key = null;
                HSSFColor crtColor = null;
                short[] rgb = null;
                int diff = 0;
                int minDiff = 999;
                for (Iterator<?> it = keys.iterator(); it.hasNext();) {
                    key = it.next();
                    crtColor = (HSSFColor) triplets.get(key);
                    rgb = crtColor.getTriplet();
                    diff = Math.abs(rgb[0] - awtColor.getRed()) + Math.abs(rgb[1] - awtColor.getGreen())
                            + Math.abs(rgb[2] - awtColor.getBlue());
                    if (diff < minDiff) {
                        minDiff = diff;
                        color = crtColor;
                    }
                }
            }
        }
        hssfColorsCache.put(awtColor, color);
    }
    return color;
}

From source file:org.apache.metamodel.excel.ExcelUtils.java

License:Apache License

public static Style getCellStyle(Workbook workbook, Cell cell) {
    if (cell == null) {
        return Style.NO_STYLE;
    }//from w  ww  .  ja  va2 s .c o m
    final CellStyle cellStyle = cell.getCellStyle();

    final short fontIndex = cellStyle.getFontIndex();
    final Font font = workbook.getFontAt(fontIndex);
    final StyleBuilder styleBuilder = new StyleBuilder();

    // Font bold, italic, underline
    if (font.getBoldweight() >= Font.BOLDWEIGHT_BOLD) {
        styleBuilder.bold();
    }
    if (font.getItalic()) {
        styleBuilder.italic();
    }
    if (font.getUnderline() != FontUnderline.NONE.getByteValue()) {
        styleBuilder.underline();
    }

    // Font size
    final Font stdFont = workbook.getFontAt((short) 0);
    final short fontSize = font.getFontHeightInPoints();
    if (stdFont.getFontHeightInPoints() != fontSize) {
        styleBuilder.fontSize(fontSize, SizeUnit.PT);
    }

    // Font color
    final short colorIndex = font.getColor();
    if (font instanceof HSSFFont) {
        if (colorIndex != HSSFFont.COLOR_NORMAL) {
            final HSSFWorkbook wb = (HSSFWorkbook) workbook;
            HSSFColor color = wb.getCustomPalette().getColor(colorIndex);
            if (color != null) {
                short[] triplet = color.getTriplet();
                styleBuilder.foreground(triplet);
            }
        }
    } else if (font instanceof XSSFFont) {
        XSSFFont xssfFont = (XSSFFont) font;

        XSSFColor color = xssfFont.getXSSFColor();
        if (color != null) {
            String argbHex = color.getARGBHex();
            if (argbHex != null) {
                styleBuilder.foreground(argbHex.substring(2));
            }
        }
    } else {
        throw new IllegalStateException(
                "Unexpected font type: " + (font == null ? "null" : font.getClass()) + ")");
    }

    // Background color
    if (cellStyle.getFillPattern() == 1) {
        Color color = cellStyle.getFillForegroundColorColor();
        if (color instanceof HSSFColor) {
            short[] triplet = ((HSSFColor) color).getTriplet();
            if (triplet != null) {
                styleBuilder.background(triplet);
            }
        } else if (color instanceof XSSFColor) {
            String argb = ((XSSFColor) color).getARGBHex();
            if (argb != null) {
                styleBuilder.background(argb.substring(2));
            }
        } else {
            throw new IllegalStateException(
                    "Unexpected color type: " + (color == null ? "null" : color.getClass()) + ")");
        }
    }

    // alignment
    switch (cellStyle.getAlignment()) {
    case CellStyle.ALIGN_LEFT:
        styleBuilder.leftAligned();
        break;
    case CellStyle.ALIGN_RIGHT:
        styleBuilder.rightAligned();
        break;
    case CellStyle.ALIGN_CENTER:
        styleBuilder.centerAligned();
        break;
    case CellStyle.ALIGN_JUSTIFY:
        styleBuilder.justifyAligned();
        break;
    }

    return styleBuilder.create();
}

From source file:org.joeffice.spreadsheet.cell.CellUtils.java

License:Apache License

/**
 * Converts a POI color to an AWT color.
 *///from   ww w .j av  a 2 s .  c  o  m
public static Color shortToColor(short xlsColorIndex) {
    if (xlsColorIndex > 0) {
        HSSFColor xlsColor = HSSFColor.getIndexHash().get(new Integer(xlsColorIndex));
        if (xlsColor != null) {
            short[] rgb = xlsColor.getTriplet();
            return new Color(rgb[0], rgb[1], rgb[2]);
            //return Color.decode(xlsColor.getHexString());
        }
    }
    return null;
}

From source file:org.netxilia.impexp.impl.PoiUtils.java

License:Open Source License

public static Styles poiStyle2Netxilia(CellStyle poiStyle, Font font, HSSFPalette palette,
        NetxiliaStyleResolver styleResolver) {
    List<Style> entries = new ArrayList<Style>();

    if (!poiStyle.getWrapText()) {
        entries.add(DefaultStyle.nowrap.getStyle());
    }//from w  w  w.j a  v a  2 s  .  c  om
    // font
    if (font.getItalic()) {
        entries.add(DefaultStyle.italic.getStyle());
    }
    if (font.getStrikeout()) {
        entries.add(DefaultStyle.strikeout.getStyle());
    }
    if (font.getBoldweight() == Font.BOLDWEIGHT_BOLD) {
        entries.add(DefaultStyle.bold.getStyle());
    }
    if (font.getUnderline() != Font.U_NONE) {
        entries.add(DefaultStyle.underline.getStyle());
    }
    // borders
    if (poiStyle.getBorderBottom() != CellStyle.BORDER_NONE) {
        entries.add(DefaultStyle.borderBottom.getStyle());
    }
    if (poiStyle.getBorderLeft() != CellStyle.BORDER_NONE) {
        entries.add(DefaultStyle.borderLeft.getStyle());
    }
    if (poiStyle.getBorderTop() != CellStyle.BORDER_NONE) {
        entries.add(DefaultStyle.borderTop.getStyle());
    }
    if (poiStyle.getBorderRight() != CellStyle.BORDER_NONE) {
        entries.add(DefaultStyle.borderRight.getStyle());
    }
    // align
    switch (poiStyle.getAlignment()) {
    case CellStyle.ALIGN_LEFT:
        entries.add(DefaultStyle.alignLeft.getStyle());
        break;
    case CellStyle.ALIGN_RIGHT:
        entries.add(DefaultStyle.alignRight.getStyle());
        break;
    case CellStyle.ALIGN_CENTER:
        entries.add(DefaultStyle.alignCenter.getStyle());
        break;
    case CellStyle.ALIGN_JUSTIFY:
        entries.add(DefaultStyle.alignJustify.getStyle());
        break;
    }
    if (font != null && font.getColor() != 0) {
        HSSFColor poiForeground = palette.getColor(font.getColor());
        if (poiForeground != null && poiForeground != HSSFColor.AUTOMATIC.getInstance()) {
            Style foregroundDef = styleResolver.approximateForeground(poiForeground.getTriplet()[0],
                    poiForeground.getTriplet()[1], poiForeground.getTriplet()[2]);
            if (foregroundDef != null) {
                entries.add(foregroundDef);
            }
        }
    }

    if (poiStyle.getFillForegroundColor() != 0) {
        HSSFColor poiBackground = palette.getColor(poiStyle.getFillForegroundColor());
        if (poiBackground != null && poiBackground != HSSFColor.AUTOMATIC.getInstance()) {
            Style backgroundDef = styleResolver.approximateBackground(poiBackground.getTriplet()[0],
                    poiBackground.getTriplet()[1], poiBackground.getTriplet()[2]);
            if (backgroundDef != null) {
                entries.add(backgroundDef);
            }
        }
    }
    return entries.size() > 0 ? Styles.styles(entries) : null;
}