我怎么能阻止所有的细胞被染色?

时间:2014-03-02 06:04:11

标签: java apache-poi

我的目标是为具有不同属性的行的单元格设置样式。这是使用函数styleExceptions(....)发生的。大多数功能都有效,但是我面临的问题是该行中的每个单元都是浅绿色,不应该发生。我无法弄清楚为什么会这样。任何人都可以帮忙。

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

}

这是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;
    }

1 个答案:

答案 0 :(得分:0)

问题在于您正在进行的比较。

(Double)result==obj.get_xmonthreq()

==用于比较原始值。您有两种选择:

  1. 使用doubleValue()方法((Double)result).doubleValue() == obj.get_xmonthreq().doubleValue()
  2. 或使用equals()方法((Double)result).equals(obj.get_xmonthreq())
  3. 希望这有帮助。

相关问题