简体   繁体   English

单元未着色

[英]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: 这是cellToType方法:

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(...) cellToType(...)

works as you expects? 符合您的预期?

I think the problem is that the program doesn't go into the if here: 我认为问题在于该程序没有进入if

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

Can you post your cellToType(...) function? 您可以发布cellToType(...)函数吗?

EDIT: You seems to compare wrongly Double-variables. 编辑:您似乎错误地比较了双变量。 Instead of == try using result.equalsTo(...) 代替==尝试使用result.equalsTo(...)

Your cellToStyle method isn't working because of how you are expecting to change the result object in the styleExceptions method. 您的cellToStyle方法无法正常工作,因为您希望如何在styleExceptions方法中更改result对象。 Because Java passes references to objects by value, the cellToStyle method receives a copy of the result reference in its own result parameter. 由于Java按值传递对对象的引用,因此cellToStyle方法在其自己的result参数中接收result引用的副本。 In that method, you change that local reference, but that does nothing to the result reference in styleExceptions . 在该方法中,您可以更改该本地引用,但这对styleExceptionsresult引用没有任何styleExceptions

You need to return that reference in cellToStyle and assign it to result in styleExceptions . 你需要返回该参考cellToStyle并将其分配给resultstyleExceptions

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

and

    return result;
}

at the end. 在末尾。 Then, in styleExceptions , do this: 然后,在styleExceptions ,执行以下操作:

Object result = cellToType(cell);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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