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

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

Introduction

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

Prototype

public static synchronized Map<Integer, HSSFColor> getIndexHash() 

Source Link

Document

This function returns all the colours in an unmodifiable Map.

Usage

From source file:br.com.tecsinapse.dataio.example.ExcelCellColorExampleTest.java

License:LGPL

@Test
public void generateExample() throws IOException {
    Table table = new Table();

    int line = 0;
    List<HSSFColor> colors = new ArrayList<>(HSSFColor.getIndexHash().values());
    colors.add(HSSFColor.HSSFColorPredefined.AUTOMATIC.getColor());

    HSSFColor cBlue = table.newCustomColor(HSSFColor.HSSFColorPredefined.AQUA.getColor(),
            new java.awt.Color(0, 0, 255));
    HSSFColor cGreen = table.newCustomColor(HSSFColor.HSSFColorPredefined.RED.getColor(),
            new java.awt.Color(0, 255, 0));
    colors.add(cGreen);//from  w ww  . j a v  a2s .  c  o m
    colors.add(cBlue);
    for (HSSFColor color : colors) {
        table.addNewRow();
        line++;
        TableCell tableCell = new TableCell(
                String.format("Line %d - Color: %s", line, color.getClass().getSimpleName()));
        TableCellStyle style = new TableCellStyle(color);
        tableCell.setTableCellStyle(style);
        table.add(tableCell);
        for (int i = 0; i < 10; i++) {
            tableCell = new TableCell(String.format("Col %d", i));
            tableCell.setTableCellStyle(style);
            style.setBold(true);
            table.add(tableCell);
        }
    }
    String xlsx = "XLSX-cell-color.xlsx";
    Files.move(ExporterUtil.getXlsxFile(table, xlsx).toPath(),
            ResourceUtils.newFileTargetResource(xlsx).toPath(), StandardCopyOption.REPLACE_EXISTING);

    String sxlsx = "XLSX-cell-color-streaming.xlsx";
    Files.move(ExporterUtil.getSxlsxFile(table, sxlsx).toPath(),
            ResourceUtils.newFileTargetResource(sxlsx).toPath(), StandardCopyOption.REPLACE_EXISTING);

    String xls = "XLS-cell-color.xls";
    Files.move(ExporterUtil.getXlsFile(table, xls).toPath(), ResourceUtils.newFileTargetResource(xls).toPath(),
            StandardCopyOption.REPLACE_EXISTING);

    File htmlOutput = ResourceUtils.newFileTargetResource("HTML-cell-color.html");
    ExportHtml.newInstance(Charset.forName("UTF-8")).toHtml(table, htmlOutput);
}

From source file:br.com.tecsinapse.dataio.example.ExcelCellColorExampleTest.java

License:LGPL

@Test
public void generateManySheetsExample() throws IOException {
    List<Table> tables = new ArrayList<>();
    for (int s = 1; s <= 5; s++) {
        Table table = new Table();
        table.setTitle(String.format("Plan %d", s));
        int line = 0;
        for (HSSFColor color : HSSFColor.getIndexHash().values()) {
            table.addNewRow();//from w w  w  . ja  v a2  s.c o m
            line++;
            TableCell tableCell = new TableCell(
                    String.format("[Sheet %d] Line %d - Color: %s", s, line, color.getClass().getSimpleName()));
            TableCellStyle style = new TableCellStyle(color);
            tableCell.setTableCellStyle(style);
            table.add(tableCell);
            for (int i = 0; i < 10; i++) {
                tableCell = new TableCell(String.format("Col %d", i));
                tableCell.setTableCellStyle(style);
                style.setBold(true);
                table.add(tableCell);
            }
        }
        tables.add(table);
    }
    String xlsx = "XLSX-cell-color-many-sheets.xlsx";
    Files.move(ExporterUtil.getXlsxFile(tables, xlsx).toPath(),
            ResourceUtils.newFileTargetResource(xlsx).toPath(), StandardCopyOption.REPLACE_EXISTING);

    String xls = "XLS-cell-color-many-sheets.xls";
    Files.move(ExporterUtil.getXlsxFile(tables, xls).toPath(),
            ResourceUtils.newFileTargetResource(xls).toPath(), StandardCopyOption.REPLACE_EXISTING);

    /*        File htmlOutput = ResourceUtils.newFileTargetResource("HTML-cell-color.html");
            ExportHtml.newInstance(Charset.forName("UTF-8")).toHtml(table, htmlOutput);*/
}

From source file:br.com.tecsinapse.exporter.example.ExcelCellColorExampleTest.java

License:LGPL

@Test
public void generateExample() throws IOException {
    Table table = new Table();

    int line = 0;
    for (HSSFColor color : HSSFColor.getIndexHash().values()) {
        table.addNewRow();// w ww  .j  a  v  a2 s  .  com
        line++;
        TableCell tableCell = new TableCell(
                String.format("Line %d - Color: %s", line, color.getClass().getSimpleName()));
        TableCellStyle style = new TableCellStyle(color);
        tableCell.setTableCellStyle(style);
        table.add(tableCell);
        for (int i = 0; i < 10; i++) {
            tableCell = new TableCell(String.format("Col %d", i));
            tableCell.setTableCellStyle(style);
            style.setBold(true);
            table.add(tableCell);
        }
    }
    String xlsx = "XLSX-cell-color.xlsx";
    Files.move(ExporterUtil.getXlsFile(table, xlsx).toPath(),
            ResourceUtils.newFileTargetResource(xlsx).toPath(), StandardCopyOption.REPLACE_EXISTING);

    String xls = "XLS-cell-color.xls";
    Files.move(ExporterUtil.getXlsFile(table, xls).toPath(), ResourceUtils.newFileTargetResource(xls).toPath(),
            StandardCopyOption.REPLACE_EXISTING);

    File htmlOutput = ResourceUtils.newFileTargetResource("HTML-cell-color.html");
    ExportHtml.newInstance(Charset.forName("UTF-8")).toHtml(table, htmlOutput);
}

From source file:br.com.tecsinapse.exporter.example.ExcelCellColorExampleTest.java

License:LGPL

@Test
public void generateManySheetsExample() throws IOException {
    List<Table> tables = new ArrayList<>();
    for (int s = 1; s <= 5; s++) {
        Table table = new Table();
        table.setTitle(String.format("Plan %d", s));
        int line = 0;
        for (HSSFColor color : HSSFColor.getIndexHash().values()) {
            table.addNewRow();//www .  ja va2  s .  c o  m
            line++;
            TableCell tableCell = new TableCell(
                    String.format("[Sheet %d] Line %d - Color: %s", s, line, color.getClass().getSimpleName()));
            TableCellStyle style = new TableCellStyle(color);
            tableCell.setTableCellStyle(style);
            table.add(tableCell);
            for (int i = 0; i < 10; i++) {
                tableCell = new TableCell(String.format("Col %d", i));
                tableCell.setTableCellStyle(style);
                style.setBold(true);
                table.add(tableCell);
            }
        }
        tables.add(table);
    }
    String xlsx = "XLSX-cell-color-many-sheets.xlsx";
    Files.move(ExporterUtil.getMoreThanOneSheetXlsFile(tables, xlsx).toPath(),
            ResourceUtils.newFileTargetResource(xlsx).toPath(), StandardCopyOption.REPLACE_EXISTING);

    String xls = "XLS-cell-color-many-sheets.xls";
    Files.move(ExporterUtil.getMoreThanOneSheetXlsxFile(tables, xls).toPath(),
            ResourceUtils.newFileTargetResource(xls).toPath(), StandardCopyOption.REPLACE_EXISTING);

    /*        File htmlOutput = ResourceUtils.newFileTargetResource("HTML-cell-color.html");
            ExportHtml.newInstance(Charset.forName("UTF-8")).toHtml(table, htmlOutput);*/
}

From source file:cn.trymore.core.util.excel.PoiExcelParser.java

License:Open Source License

@SuppressWarnings("rawtypes")
private HSSFColor getHSSFColor(short color) {
    Hashtable hash = HSSFColor.getIndexHash();
    Iterator itor = hash.keySet().iterator();
    while (itor.hasNext()) {
        Integer itg = (Integer) itor.next();
        if (itg.intValue() == color) {
            return ((HSSFColor) hash.get(itg));
        }/*from   w  w  w.  j  ava2  s  .  c  om*/
    }
    return null;
}

From source file:com.actelion.research.spiritapp.ui.util.PDFUtils.java

License:Open Source License

private static Chunk getChunk(Workbook wb, Cell cell) {
    Chunk phrase = null;//from  ww  w . jav a 2  s . c  o m

    switch (cell.getCellType() == Cell.CELL_TYPE_FORMULA ? cell.getCachedFormulaResultType()
            : cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        phrase = new Chunk("" + cell.getStringCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        String format = cell.getCellStyle().getDataFormatString();
        if (cell.getCellStyle().getDataFormat() > 0) {
            try {
                if (format.contains("0")) {
                    //Decimal
                    DecimalFormat df = new DecimalFormat(format);
                    phrase = new Chunk(df.format(cell.getNumericCellValue()));
                } else if (format.contains("h:")) {
                    phrase = new Chunk(FormatterUtils.formatDateTimeShort(cell.getDateCellValue()));
                } else if (format.contains("yy")) {
                    phrase = new Chunk(FormatterUtils.formatDate(cell.getDateCellValue()));
                }
            } catch (Exception e) {
                System.err.println(e);
            }
        }
        if (phrase == null) {
            phrase = new Chunk("" + (int) cell.getNumericCellValue());
        }
        break;
    case Cell.CELL_TYPE_BLANK:
        phrase = new Chunk("");
        break;
    default:
        phrase = new Chunk("" + cell.getCellType());
    }
    Font font = wb.getFontAt(cell.getCellStyle().getFontIndex());
    short[] rgb = HSSFColor.getIndexHash().get((int) font.getColor()).getTriplet();

    phrase.setFont(new com.lowagie.text.Font(phrase.getFont().getBaseFont(), font.getFontHeightInPoints() - 3,
            (font.getBold() ? com.lowagie.text.Font.BOLD : com.lowagie.text.Font.NORMAL),
            new Color(rgb[0], rgb[1], rgb[2])));
    return phrase;
}

From source file:com.eryansky.core.excelTools.ExcelUtils.java

License:Apache License

private static short findColor(short index, HSSFWorkbook srcwb, HSSFWorkbook destwb) {
    Integer id = new Integer(index);
    if (HSSFColor.getIndexHash().containsKey(id))
        return index;
    if (index == HSSFColor.AUTOMATIC.index)
        return index;
    HSSFColor color = srcwb.getCustomPalette().getColor(index);
    if (color == null) {
        return index;
    }/*from w  w w  . j  av a 2s  . c o  m*/

    HSSFColor ncolor = destwb.getCustomPalette().findColor((byte) color.getTriplet()[0],
            (byte) color.getTriplet()[1], (byte) color.getTriplet()[2]);
    if (ncolor != null)
        return ncolor.getIndex();
    destwb.getCustomPalette().setColorAtIndex(index, (byte) color.getTriplet()[0], (byte) color.getTriplet()[1],
            (byte) color.getTriplet()[2]);
    return index;
}

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

License:Apache License

/**
 * Converts a POI color to an AWT color.
 *//*w  w w.  ja v  a2  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.tiefaces.components.websheet.utility.ColorUtility.java

License:MIT License

/**
 * Gets the bg color from cell./*from  w w w  .  j a  va  2s.c o m*/
 *
 * @param wb
 *            the wb
 * @param poiCell
 *            the poi cell
 * @param cellStyle
 *            the cell style
 * @return the bg color from cell
 */
static String getBgColorFromCell(final Workbook wb, final Cell poiCell, final CellStyle cellStyle) {

    String style = "";
    if (poiCell instanceof HSSFCell) {
        int bkColorIndex = cellStyle.getFillForegroundColor();
        HSSFColor color = HSSFColor.getIndexHash().get(bkColorIndex);
        if (color != null) {
            // correct color for customPalette
            HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette();
            HSSFColor color2 = palette.getColor(bkColorIndex);
            if (!color.getHexString().equalsIgnoreCase(color2.getHexString())) {
                color = color2;
            }
            style = "background-color:rgb(" + FacesUtility.strJoin(color.getTriplet(), ",") + ");";
        }
    } else if (poiCell instanceof XSSFCell) {
        XSSFColor color = ((XSSFCell) poiCell).getCellStyle().getFillForegroundColorColor();
        if (color != null) {
            style = "background-color:rgb(" + FacesUtility.strJoin(getTripletFromXSSFColor(color), ",") + ");";
        }
    }
    return style;
}