Example usage for org.apache.poi.ss.usermodel CellStyle getFillForegroundColorColor

List of usage examples for org.apache.poi.ss.usermodel CellStyle getFillForegroundColorColor

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel CellStyle getFillForegroundColorColor.

Prototype

Color getFillForegroundColorColor();

Source Link

Document

Gets the color object representing the current foreground fill, resolving indexes using the supplied workbook.

Usage

From source file:CellStyleDetails.java

License:Apache License

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        throw new IllegalArgumentException("Filename must be given");
    }//from   w  w  w.  j  a  va  2 s.  c  om

    Workbook wb = WorkbookFactory.create(new File(args[0]));
    DataFormatter formatter = new DataFormatter();

    for (int sn = 0; sn < wb.getNumberOfSheets(); sn++) {
        Sheet sheet = wb.getSheetAt(sn);
        System.out.println("Sheet #" + sn + " : " + sheet.getSheetName());

        for (Row row : sheet) {
            System.out.println("  Row " + row.getRowNum());

            for (Cell cell : row) {
                CellReference ref = new CellReference(cell);
                System.out.print("    " + ref.formatAsString());
                System.out.print(" (" + cell.getColumnIndex() + ") ");

                CellStyle style = cell.getCellStyle();
                System.out.print("Format=" + style.getDataFormatString() + " ");
                System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " ");
                System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " ");

                Font font = wb.getFontAt(style.getFontIndex());
                System.out.print("Font=" + font.getFontName() + " ");
                System.out.print("FontColor=");
                if (font instanceof HSSFFont) {
                    System.out.print(renderColor(((HSSFFont) font).getHSSFColor((HSSFWorkbook) wb)));
                }
                if (font instanceof XSSFFont) {
                    System.out.print(renderColor(((XSSFFont) font).getXSSFColor()));
                }

                System.out.println();
                System.out.println("        " + formatter.formatCellValue(cell));
            }
        }

        System.out.println();
    }
}

From source file:com.canoo.webtest.plugins.exceltest.ExcelVerifyCellStyle.java

License:Open Source License

public void doExecute() throws Exception {
    final String[] border = separateSides(getBorder());
    final String[] borderColor = separateSides(getBorderColor());
    for (int i = 0; i < SIDES.length; i++) {
        checkFormat(SIDES[i] + "Border", border[i], getCellBorder(i));
        checkFormat(SIDES[i] + "BorderColor", ExcelColorUtils.lookupStandardColorName(borderColor[i]),
                getCellBorderColor(i));/* w ww .  j av a  2  s.  co m*/
    }
    final Cell excelCell = getExcelCell();
    checkFormat("type", getType(),
            ExcelCellUtils.getCellType(excelCell == null ? Cell.CELL_TYPE_BLANK : excelCell.getCellType()));
    if (excelCell == null) {
        if (cellNotRequired()) {
            return;
        } else {
            throw new StepExecutionException("Can't find cell for " + getCellReferenceStr(), this);
        }
    }
    final CellStyle cellStyle = excelCell.getCellStyle();
    checkFormat("format", getFormat(),
            getExcelWorkbook().createDataFormat().getFormat(cellStyle.getDataFormat()));
    checkFormat("align", getAlign(), ExcelCellUtils.getAlignmentString(cellStyle.getAlignment()));
    checkFormat("valign", getValign(),
            ExcelCellUtils.getVerticalAlignmentString(cellStyle.getVerticalAlignment()));
    checkFormat("wrap", getWrap(), String.valueOf(cellStyle.getWrapText()));
    checkFormat("locked", getLocked(), String.valueOf(cellStyle.getLocked()));
    checkFormat("fontName", getFontName(), getFont(cellStyle).getFontName());
    checkFormat("fontSize", getFontSize(), String.valueOf(getFont(cellStyle).getFontHeightInPoints()));
    checkFormat("fontStyle", sortElements(getFontStyle()), getFontStyle(getFont(cellStyle)));
    checkFormat("fillColor", ExcelColorUtils.lookupStandardColorName(getFillColor()),
            ExcelColorUtils.getColorName(this, cellStyle.getFillForegroundColorColor()));
    checkFormat("fillBackgroundColor", ExcelColorUtils.lookupStandardColorName(getFillBackgroundColor()),
            ExcelColorUtils.getColorName(this, cellStyle.getFillBackgroundColorColor()));
    checkFormat("textColor", ExcelColorUtils.lookupStandardColorName(getTextColor()),
            ExcelColorUtils.getColorName(this, getFont(cellStyle).getColor()));
    checkFormat("fillPattern", getFillPattern(), ExcelCellUtils.getFillPattern(cellStyle.getFillPattern()));
}

From source file:de.jlo.talendcomp.excel.SpreadsheetReferencedCellInput.java

License:Apache License

private String getFgColor(Cell cell) {
    CellStyle style = cell.getCellStyle();
    if (style != null) {
        return getColorString(style.getFillForegroundColorColor());
    } else {//from   w w  w  . ja va  2s  . c  o m
        return null;
    }
}

From source file:itpreneurs.itp.report.archive.CellStyleDetails.java

License:Apache License

public static void main(String[] args) throws Exception {

    //     if(args.length == 0) {
    //        throw new IllegalArgumentException("Filename must be given");
    //     }/*from   w  w  w  . j ava 2  s . co m*/

    String filename = "/Users/vincentgong/Documents/workspaces/Resource/itpreneurs/report/Workbook1.xlsx";

    Workbook wb = WorkbookFactory.create(new File(filename));
    DataFormatter formatter = new DataFormatter();

    for (int sn = 0; sn < wb.getNumberOfSheets(); sn++) {
        Sheet sheet = wb.getSheetAt(sn);
        System.out.println("Sheet #" + sn + " : " + sheet.getSheetName());

        for (Row row : sheet) {
            System.out.println("  Row " + row.getRowNum());

            for (Cell cell : row) {
                CellReference ref = new CellReference(cell);
                System.out.print("    " + ref.formatAsString());
                System.out.print(" (" + cell.getColumnIndex() + ") ");

                CellStyle style = cell.getCellStyle();
                System.out.print("Format=" + style.getDataFormatString() + " ");
                System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " ");
                System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " ");

                Font font = wb.getFontAt(style.getFontIndex());
                System.out.print("Font=" + font.getFontName() + " ");
                System.out.print("FontColor=");
                if (font instanceof HSSFFont) {
                    System.out.print(renderColor(((HSSFFont) font).getHSSFColor((HSSFWorkbook) wb)));
                }
                if (font instanceof XSSFFont) {
                    System.out.print(renderColor(((XSSFFont) font).getXSSFColor()));
                }

                System.out.println();
                System.out.println("        " + formatter.formatCellValue(cell));
            }
        }

        System.out.println();
    }
}

From source file:net.unit8.axebomber.parser.CellImpl.java

License:Apache License

public java.awt.Color getColor() {
    java.awt.Color awtColor = null;
    CellStyle style = cell.getCellStyle();
    Color color = style.getFillForegroundColorColor();
    if (color instanceof HSSFColor) {
        short[] rgb = ((HSSFColor) color).getTriplet();
        if (((HSSFColor) color).getIndex() == IndexedColors.AUTOMATIC.getIndex())
            rgb[0] = rgb[1] = rgb[2] = 255;
        awtColor = new java.awt.Color(rgb[0], rgb[1], rgb[2]);
    } else if (color instanceof XSSFColor) {
        byte[] rgb = ((XSSFColor) color).getRgb();
        if (((XSSFColor) color).isAuto())
            rgb[0] = rgb[1] = rgb[2] = (byte) 0xFF;
        awtColor = new java.awt.Color(rgb[0], rgb[1], rgb[2], rgb[3]);
    }// ww w  .j  a v  a 2 s .  c o  m
    return awtColor;
}

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;
    }/* w  w  w .  jav a 2  s . com*/
    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.drugepi.table.ExcelUtils.java

License:Mozilla Public License

private static boolean cellIsColor(Cell cell, ColorRepresentation color) {
    if (cell == null)
        return false;

    CellStyle style = cell.getCellStyle();

    Color bgColor = style.getFillForegroundColorColor();

    if (bgColor instanceof XSSFColor) {
        XSSFColor xssfBgColor = (XSSFColor) bgColor;
        //         System.out.printf("Cell color is %s index is %d %d\n", xssfBgColor.getARGBHex(), xssfBgColor.getRgb()[0], xssfBgColor.getIndexed());
        return xssfBgColor.getARGBHex().equalsIgnoreCase(color.xssfArgb);
    } else if (bgColor instanceof HSSFColor) {
        HSSFColor hssfBgColor = (HSSFColor) bgColor;
        //         System.out.printf("Cell color is %s\n", hssfBgColor.getHexString());
        return hssfBgColor.getHexString().equalsIgnoreCase(color.hssfHexString);
    } else/*  ww w  .j av  a 2  s  .c  o m*/
        return false;
}

From source file:ru.icc.cells.ssdc.DataLoader.java

License:Apache License

private void fillCellStyle(CStyle cellStyle, CellStyle excelCellStyle) {
    Font excelFont = workbook.getFontAt(excelCellStyle.getFontIndex());
    // TODO    CFont newFont(excelFont)
    //CFont font = new CFont();
    //cellStyle.setFont( font );
    CFont font = cellStyle.getFont();/*from www . ja v a2 s  .  com*/

    fillFont(font, excelFont);

    cellStyle.setHidden(excelCellStyle.getHidden());
    cellStyle.setLocked(excelCellStyle.getLocked());
    cellStyle.setWrapped(excelCellStyle.getWrapText());

    cellStyle.setIndention(excelCellStyle.getIndention());
    cellStyle.setRotation(excelCellStyle.getRotation());

    cellStyle.setHorzAlignment(this.getHorzAlignment(excelCellStyle.getAlignment()));
    cellStyle.setVertAlignment(this.getVertAlignment(excelCellStyle.getVerticalAlignment()));

    CBorder leftBorder = cellStyle.getLeftBorder();
    CBorder rightBorder = cellStyle.getRightBorder();
    CBorder topBorder = cellStyle.getTopBorder();
    CBorder bottomBorder = cellStyle.getBottomBorder();

    BorderType lbType = this.convertBorderType(excelCellStyle.getBorderLeft());
    BorderType rbType = this.convertBorderType(excelCellStyle.getBorderRight());
    BorderType tbType = this.convertBorderType(excelCellStyle.getBorderTop());
    BorderType bbType = this.convertBorderType(excelCellStyle.getBorderBottom());

    leftBorder.setType(lbType);
    rightBorder.setType(rbType);
    topBorder.setType(tbType);
    bottomBorder.setType(bbType);

    //   "Fill Background Color" ???,    ??,
    //   ? ??? .  ?  .
    //      "Fill Foreground Color"
    XSSFColor bgColor = (XSSFColor) excelCellStyle.getFillBackgroundColorColor();

    // ? Index   64,  ? ,         ,
    // ?  ?   null ? 
    if (null != bgColor && 64 != bgColor.getIndexed()) {
        String bgColorHexRGB = bgColor.getARGBHex().substring(2);
        cellStyle.setBgColor(new CColor(bgColorHexRGB));
    }

    //   "Fill Background Color"      ??,
    //   ? ??? .      
    XSSFColor fgColor = (XSSFColor) excelCellStyle.getFillForegroundColorColor();

    if (null != fgColor && 64 != fgColor.getIndexed()) {
        String fgColorHexRGB = fgColor.getARGBHex().substring(2);
        cellStyle.setFgColor(new CColor(fgColorHexRGB));
    }

    // TODO   
}

From source file:utilities.DMPORosterToMapGenerator.java

public DMPORosterToMapGenerator(String xlsxFileName) throws FileNotFoundException, IOException {
    db = new TreeMap();
    File myFile = new File(xlsxFileName);

    FileInputStream fis = new FileInputStream(myFile);

    System.out.println(xlsxFileName);
    XSSFWorkbook myWorkBook = new XSSFWorkbook(fis);

    XSSFSheet mySheet = myWorkBook.getSheetAt(0);
    Iterator<Row> rowIterator = mySheet.iterator();
    DataFormatter df = new DataFormatter();
    //        System.out.println(IndexedColors.YELLOW);
    //        System.out.println(IndexedColors.BLUE.getIndex());
    //        System.out.println(IndexedColors.RED.getIndex());
    //        System.out.println(IndexedColors.WHITE);
    //        System.out.println(IndexedColors.BLACK);
    //        System.out.println(IndexedColors.GREEN);

    rowIterator.next(); // skip the header row
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();/* w w  w.j a  v  a2 s.  co m*/
        Cell cell = row.getCell(FILE_AT_DMPO_INDX); // File at DMPO      
        int type = cell.getCellType();
        //System.out.println(cell.getStringCellValue());

        if (type == HSSFCell.CELL_TYPE_STRING && cell.getStringCellValue().equalsIgnoreCase("Yes")) { // File at DMPO      
            CellStyle style = cell.getCellStyle();

            // getFillBackgroundColor() always 64.  getFillForegroundColor() is 64 for white
            // getFillForegroundColor() is 0 for yellow
            //                System.out.println(style.getFillForegroundColor());
            //                //if (style.getFillForegroundColorColor() != null)
            //                    System.out.println(style.getFillForegroundColorColor());
            //               
            //                System.out.println(style.getFillBackgroundColor());
            //                //if (style.getFillBackgroundColorColor() != null)
            //                    System.out.println(style.getFillBackgroundColorColor());
            //if(style.getFillForegroundColor() != IndexedColors.YELLOW.getIndex()) { 
            Color color = style.getFillForegroundColorColor();
            if (color == null || color.toString().equals(GlobalVar.WHITE)) { // no fill or fill with white

                Cell ssnCell = row.getCell(SSN_INDX);
                String ssnString = df.formatCellValue(ssnCell); //return ***-**-****
                ssnString = readSSN(ssnString).trim();
                if (!db.containsKey(ssnString)) {
                    List<String> list = new LinkedList<String>();
                    String ssn = displayFormatSSN(ssnString).trim();
                    String name = row.getCell(NAME_INDX).getStringCellValue().trim();
                    String dutyStation = row.getCell(ORIGIN_INDX).getStringCellValue().trim();
                    String typeCM = row.getCell(TYPE_CM_INDX).getStringCellValue().trim();
                    //String typeCM = row.getCell(TYPE_CM_INDX).getStringCellValue();
                    list.add(ssn);
                    list.add(name);
                    list.add(dutyStation);
                    list.add(typeCM);
                    db.put(ssnString, list);
                }
            }
        }
    }
}