简体   繁体   中英

cells are not being colored

so I'm trying to color cells based on some particular value using this function:

public static void styleExceptions(CellStyle Exstyle, Font Exfont, Cell cell, AdditiveInformation obj){
    Exfont.setFontHeightInPoints((short) 10);
    Exfont.setFontName("Calibri");
    Exstyle.setFont(Exfont);
    Exstyle.setAlignment(CellStyle.ALIGN_CENTER);
    Exstyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    Exstyle.setBorderBottom(CellStyle.BORDER_DOUBLE);
    Exstyle.setBorderRight(CellStyle.BORDER_THIN);
    Exstyle.setBorderLeft(CellStyle.BORDER_THIN);

    Object result=null; 
    cellToType(cell,result);

    if(result instanceof Double){

        if((Double)result==obj.get_xmonthreq() || (Double)result==obj.get_xmonthbalance() ||
                (Double)result==obj.get_xmonthendstock()){
                 Exstyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
                 Exstyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
            }

            else if((Double)result==obj.get_ymonthreq() || (Double)result==obj.get_ymonthbalance() ||
                    (Double)result==obj.get_ymonthendstock()){

                 Exstyle.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
                 Exstyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
                 Exstyle.setBorderBottom(CellStyle.BORDER_DOUBLE);
            }


            else if((Double)result==obj.get_zmonthreq() || (Double)result==obj.get_zmonthbalance() ||
                    (Double)result==obj.get_zmonthendstock()){

                 Exstyle.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
                 Exstyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
                 Exstyle.setBorderBottom(CellStyle.BORDER_DOUBLE);
            }
    }

}

everything other than the coloring part is working in the function, I'm i doing something wrong with the result object, cause none of the cells that satisfy the conditions are being colored.

this the the cellToType method:

private static void cellToType(Cell cell, Object result){

        switch(cell.getCellType()){

        case Cell.CELL_TYPE_NUMERIC:
            if(DateUtil.isCellDateFormatted(cell)){
                result= cell.getDateCellValue();
            }
            else
            result=cell.getNumericCellValue();
            break;

        case Cell.CELL_TYPE_STRING: 
            result=cell.getStringCellValue();
            break; 

        case Cell.CELL_TYPE_BOOLEAN:
            result=cell.getBooleanCellValue();
            break;

        case Cell.CELL_TYPE_FORMULA:
            result=cell.getCellFormula();
            break;  

        default:
            throw new RuntimeException("There is no support for this type of cell");
        }
    }

Are you sure the method:

cellToType(...)

works as you expects?

I think the problem is that the program doesn't go into the if here:

Object result=null; 
cellToType(cell,result);
if(result instanceof Double) {

Can you post your cellToType(...) function?

EDIT: You seems to compare wrongly Double-variables. Instead of == try using result.equalsTo(...)

Your cellToStyle method isn't working because of how you are expecting to change the result object in the styleExceptions method. Because Java passes references to objects by value, the cellToStyle method receives a copy of the result reference in its own result parameter. In that method, you change that local reference, but that does nothing to the result reference in styleExceptions .

You need to return that reference in cellToStyle and assign it to result in styleExceptions .

private static Object cellToType(Cell cell){
    Object result;
    // ...

and

    return result;
}

at the end. Then, in styleExceptions , do this:

Object result = cellToType(cell);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM