javaexcelapache-poihssf

HSSF apache cell styles - all cells are gray


I create cell styles for HSSFWorkbook:

private static HSSFCellStyle createNewColorCellStyle(Map<Color, HSSFCellStyle> cellStylesMap, HSSFWorkbook workbook, Color color) {

    if (cellStylesMap.get(color) == null) {
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        HSSFColor hssfColor = setColor(workbook, (byte) color.getRed(), (byte) color.getGreen(), (byte) color.getBlue());
        cellStyle.setFillForegroundColor(hssfColor.getIndex());
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);

        cellStylesMap.put(color, cellStyle);
    }

    return cellStylesMap.get(color);
}

There is the function where I set color to cell style

private static HSSFColor setColor(HSSFWorkbook workbook, byte r, byte g, byte b) {
    HSSFPalette palette = workbook.getCustomPalette();
    HSSFColor hssfColor = null;
    try {
        hssfColor = palette.findColor(r, g, b);
        if (hssfColor == null) {
            palette.setColorAtIndex(HSSFColor.BLUE_GREY.index, r, g, b);
            HSSFColor hssfColor = palette.getColor(HSSFColor.BLUE_GREY.index);
        }
    } catch (Exception e) {
        LOGGER.info(String.valueOf(e));
    }

    return hssfColor;
}

And I use it in that method, where I set my style to the HSSFCell. The function for usiing my map style. I create rows and cells in the loop:

void excell(Path path, JTable table) {
    try {
        HSSFWorkbook fWorkbook = new HSSFWorkbook();
        HSSFSheet fSheet = fWorkbook.createSheet("the sheet");
        Map<Color, HSSFCellStyle> cellStylesMap = new HashMap<>();
        TableModel model = table.getModel();
            for (int i = 0; i < model.getRowCount(); i++) {
                HSSFRow fRow = fSheet.createRow((short) i);
                for (int j = 0; j < table.getColumnModel().getColumnCount(); j++) {
                        HSSFCell cell = fRow.createCell(j);
                        cell.setCellValue(model.getValueAt(i, j));
                        Component c = table.getCellRenderer(i, j).getTableCellRendererComponent(table, cell, table.isCellSelected(i, j), table.hasFocus(), i, j);
                        Color color = c.getBackground() != null ? c.getBackground() : table.getBackground();
                        cell.setCellStyle(createNewColorCellStyle(cellStylesMap, fWorkbook, color));
                    }
                }
        }

        FileOutputStream fileOutputStream;
        fileOutputStream = new FileOutputStream(path.toString());
        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
        fWorkbook.write(bos);
        bos.close();
        fileOutputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

And all my cells are one color. In the different machine is different colors. What error is there? Why all my colors for excel are the same? I don't understand what I do wrong. Thanks.


Solution

  • You basically copy the background color of the components of your table to the Excel; you will need to debug/find out which color that is.

    I assume that they are all of the same color - that's why you get the sme colors in Excel.

    If - for example - you change

    Color color = c.getBackground() != null ? c.getBackground() : table.getBackground();
    

    to

    Color color = (i % 2 == 0 ? Color.RED : Color.BLUE);
    

    you get a mix of red and blue rows - so your code does work. Basically you need to check which color goes into createNewColorCellStyle, and then change that one way or another.

    Also, in setColor you define hssfColor twice; in the line

    HSSFColor hssfColor = palette.getColor(HSSFColor.BLUE_GREY.index);
    

    you need to delete the declaration HSSFColor.