简体   繁体   English

我如何防止所有单元格变色?

[英]how i can i prevent all the cells from being colored?

My aim is to style the cells of a row with different properties. 我的目的是为具有不同属性的行的单元格设置样式。 This is happening using a function styleExceptions(....). 这是使用函数styleExceptions(....)发生的。 Most of the function works, however the problem i'm facing is that every cell in that row is being colored light green, which should not be happening. 大多数功能都能正常工作,但是我面临的问题是该行中的每个单元格都被染成浅绿色,这是不应该发生的。 I am unable to figure out why this is happening. 我无法弄清楚为什么会这样。 can anyone help. 谁能帮忙。

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=cellToType(cell);


    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);
            }


            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);
            }
    }

}

this is the cellToType(...) function: 这是cellToType(...)函数:

private static Object cellToType(Cell cell){


        Object result=null;
        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");
        }
        return result;
    }

Problem is with the comparison you are doing. 问题在于您正在进行的比较。

(Double)result==obj.get_xmonthreq()

== is used to compare primitive values. ==用于比较原始值。 You have two options : 您有两种选择:

  1. Either Use doubleValue() method ((Double)result).doubleValue() == obj.get_xmonthreq().doubleValue() 要么使用doubleValue()方法((Double)result).doubleValue() == obj.get_xmonthreq().doubleValue()
  2. Or Use equals() method ((Double)result).equals(obj.get_xmonthreq()) 或使用equals()方法((Double)result).equals(obj.get_xmonthreq())

Hope this helps. 希望这可以帮助。

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

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