Example usage for org.apache.poi.hssf.usermodel HSSFWorkbook getCustomPalette

List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook getCustomPalette

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFWorkbook getCustomPalette.

Prototype

public HSSFPalette getCustomPalette() 

Source Link

Usage

From source file:com.ibm.ioes.bulkupload.utilities.ErrorLogServiceImpl.java

public HSSFColor setColor(HSSFWorkbook workbook, byte r, byte g, byte b) {
    HSSFPalette palette = workbook.getCustomPalette();
    HSSFColor hssfColor = null;/*w w w.j  av  a2  s .  c om*/
    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);
        Utility.LOG(true, false, e, "::BULKUPLOAD_ERROR:: Exception occured in setColor method of "
                + this.getClass().getSimpleName());
    }

    return hssfColor;
}

From source file:com.report.excel.ExcelToHtmlConverter.java

License:Apache License

private void buildStyle_border(HSSFWorkbook workbook, StringBuilder style, String type, short xlsBorder,
        short borderColor) {
    if (xlsBorder == HSSFCellStyle.BORDER_NONE)
        return;//from   w  w  w.  ja v  a 2 s.  com

    StringBuilder borderStyle = new StringBuilder();
    borderStyle.append(ExcelToHtmlUtils.getBorderWidth(xlsBorder));
    borderStyle.append(' ');
    borderStyle.append(ExcelToHtmlUtils.getBorderStyle(xlsBorder));

    final HSSFColor color = workbook.getCustomPalette().getColor(borderColor);
    if (color != null) {
        borderStyle.append(' ');
        borderStyle.append(ExcelToHtmlUtils.getColor(color));
    }

    style.append("border-" + type + ":" + borderStyle + ";");
}

From source file:com.report.excel.ExcelToHtmlConverter.java

License:Apache License

void buildStyle_font(HSSFWorkbook workbook, StringBuilder style, HSSFFont font) {
    switch (font.getBoldweight()) {
    case HSSFFont.BOLDWEIGHT_BOLD:
        style.append("font-weight:bold;");
        break;//from  ww w . j  a  va  2s.  c  om
    case HSSFFont.BOLDWEIGHT_NORMAL:
        // by default, not not increase HTML size
        // style.append( "font-weight: normal; " );
        break;
    }

    final HSSFColor fontColor = workbook.getCustomPalette().getColor(font.getColor());
    if (fontColor != null)
        style.append("color: " + ExcelToHtmlUtils.getColor(fontColor) + "; ");

    if (font.getFontHeightInPoints() != 0)
        style.append("font-size:" + font.getFontHeightInPoints() + "pt;");

    if (font.getItalic()) {
        style.append("font-style:italic;");
    }
}

From source file:com.sevenorcas.openstyle.app.service.spreadsheet.SpreadSheet.java

/**
 * Set a color by substituting an used color
 * //from ww w.  j  ava  2s  .c  om
 * Thanks to http://stackoverflow.com/questions/10528516/poi-setting-background-color-to-a-cell
 * Thanks to http://stackoverflow.com/questions/842817/how-does-java-convert-int-into-byte
 * 
 * @param workbook
 * @param r
 * @param g
 * @param b
 * @return
 */
public HSSFColor setColor(HSSFWorkbook workbook, int r, int g, int b) {
    HSSFPalette palette = workbook.getCustomPalette();
    HSSFColor hssfColor = null;
    try {
        byte rb = (byte) (r);
        byte gb = (byte) (g);
        byte bb = (byte) (b);

        hssfColor = palette.findColor(rb, gb, bb);
        if (hssfColor == null) {
            short s = colorCustomers.getColorIndex(colorCustomers.getNextIndex());
            palette.setColorAtIndex(s, rb, gb, bb);
            hssfColor = palette.getColor(s);
        }
    } catch (Exception e) {
    }

    return hssfColor;
}

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

License:Open Source License

public HSSFColorConverter(HSSFWorkbook wb) {
    this.wb = wb;
    // If there is no custom palette, then this creates a new one that is
    // a copy of the default
    colors = wb.getCustomPalette();
}

From source file:com.wangzhu.poi.ExcelToHtmlConverter.java

License:Apache License

private void buildStyle_border(HSSFWorkbook workbook, StringBuffer style, String type, short xlsBorder,
        short borderColor) {
    if (xlsBorder == CellStyle.BORDER_NONE) {
        return;/*from w ww. j a  va 2 s .  c om*/
    }

    StringBuffer borderStyle = new StringBuffer();
    borderStyle.append(AbstractExcelUtils.getBorderWidth(xlsBorder));
    borderStyle.append(' ');
    borderStyle.append(AbstractExcelUtils.getBorderStyle(xlsBorder));

    final HSSFColor color = workbook.getCustomPalette().getColor(borderColor);
    if (color != null) {
        borderStyle.append(' ');
        borderStyle.append(AbstractExcelUtils.getColor(color));
    }

    style.append("border-" + type + ":" + borderStyle + ";");
}

From source file:com.wangzhu.poi.ExcelToHtmlConverter.java

License:Apache License

void buildStyle_font(HSSFWorkbook workbook, StringBuffer style, HSSFFont font) {
    switch (font.getBoldweight()) {
    case Font.BOLDWEIGHT_BOLD:
        style.append("font-weight:bold;");
        break;//w ww.  j a  v a 2s  .  c  om
    case Font.BOLDWEIGHT_NORMAL:
        // by default, not not increase HTML size
        // style.append( "font-weight: normal; " );
        break;
    }

    final HSSFColor fontColor = workbook.getCustomPalette().getColor(font.getColor());
    if (fontColor != null) {
        style.append("color: " + AbstractExcelUtils.getColor(fontColor) + "; ");
    }

    if (font.getFontHeightInPoints() != 0) {
        style.append("font-size:" + font.getFontHeightInPoints() + "pt;");
    }

    if (font.getItalic()) {
        style.append("font-style:italic;");
    }
}

From source file:es.jamisoft.comun.io.excel.ExcelGenerator.java

License:Apache License

/**
 * Este mtodo gestiona el estilo que se proporcionar a las cabeceras.
 *
 * @param wb Objeto Excel./*from w ww. j  a  va2  s  .  c  om*/
 */
private void initStylesHeader(HSSFWorkbook wb) {

    // create instance of HSSFCellStyle
    HSSFCellStyle headerStyle = wb.createCellStyle();

    // Create Font Header
    createFontCell(wb, headerStyle, ep.getFillFontColorHeader());
    headerStyle.getFont(wb).setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

    // Create style of header cell
    HSSFPalette palette = wb.getCustomPalette();
    HSSFColor colorMapfreHSSF = palette.findColor((byte) ep.getColorMapfre().getRed(),
            (byte) ep.getColorMapfre().getGreen(), (byte) ep.getColorMapfre().getBlue());

    if (colorMapfreHSSF == null) {
        palette.setColorAtIndex(HSSFColor.LAVENDER.index, (byte) ep.getColorMapfre().getRed(),
                (byte) ep.getColorMapfre().getGreen(), (byte) ep.getColorMapfre().getBlue());
        colorMapfreHSSF = palette.getColor(HSSFColor.LAVENDER.index);
    }

    short fillBGColor = colorMapfreHSSF.getIndex();

    headerStyle.setFillForegroundColor(fillBGColor);
    headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    // create border of header cell
    createBorderCells(headerStyle);

    // set
    ep.setHeaderStyle(headerStyle);
}

From source file:net.algem.planning.export.PlanningExportService.java

License:Open Source License

/**
 *
 * @param wb workbook//from ww  w  . j  ava 2 s . co  m
 * @param c referenced color
 * @return an index
 */
private short getColorIndex(HSSFWorkbook wb, java.awt.Color c) {
    byte red = (byte) c.getRed();
    byte green = (byte) c.getGreen();
    byte blue = (byte) c.getBlue();
    short index = -1;
    HSSFColor color = wb.getCustomPalette().findColor(red, green, blue);
    if (color == null) {
        index = wb.getCustomPalette().findSimilarColor(red, green, blue).getIndex();
        wb.getCustomPalette().setColorAtIndex(index, red, green, blue);
    } else {
        index = color.getIndex();
    }
    return index;
}

From source file:org.apache.cocoon.generation.HSSFGenerator.java

License:Apache License

/**
 * Writes out the workbook data as XML, with formatting information
 *//*from  w ww.  ja va 2s . c o  m*/
private void writeStyles(HSSFWorkbook workbook, HSSFSheet sheet) throws SAXException {
    start("Styles");
    HSSFRow row = null;
    HSSFCell cell = null;
    Iterator cells = null;
    Iterator rows = sheet.rowIterator();
    while (rows.hasNext()) {
        row = (HSSFRow) rows.next();
        cells = row.cellIterator();
        while (cells.hasNext()) {
            cell = (HSSFCell) cells.next();
            attribute("startRow", Integer.toString(row.getRowNum()));
            attribute("endRow", Integer.toString(row.getRowNum()));
            attribute("startCol", Short.toString(cell.getCellNum()));
            attribute("endCol", Short.toString(cell.getCellNum()));
            start("StyleRegion");
            HSSFCellStyle style = cell.getCellStyle();
            attribute("HAlign", Integer.toString(style.getAlignment()));
            attribute("VAlign", Integer.toString(style.getVerticalAlignment()));
            attribute("WrapText", ((style.getWrapText()) ? "1" : "0"));
            attribute("Orient", Integer.toString(style.getRotation()));
            attribute("Indent", Integer.toString(style.getIndention()));
            attribute("Locked", ((style.getLocked()) ? "1" : "0"));
            attribute("Hidden", ((style.getHidden()) ? "1" : "0"));
            attribute("Fore",
                    workbook.getCustomPalette().getColor(style.getFillForegroundColor()).getHexString());
            attribute("Back",
                    workbook.getCustomPalette().getColor(style.getFillBackgroundColor()).getHexString());
            attribute("PatternColor", Integer.toString(style.getFillPattern())); // TODO
            attribute("Format", "General"); // TODO
            start("Style");
            HSSFFont font = workbook.getFontAt(style.getFontIndex());
            attribute("Unit", Short.toString(font.getFontHeightInPoints()));
            attribute("Bold", Short.toString(font.getBoldweight()));
            attribute("Italic", ((font.getItalic()) ? "1" : "0"));
            attribute("Unterline", Integer.toString(font.getUnderline()));
            attribute("StrikeThrough", ((font.getStrikeout()) ? "1" : "0"));
            start("Font");
            data(font.getFontName());
            end("Font");
            end("Style");
            end("StyleRegion");
        }
    }
    end("Styles");
}