Java 我怎样才能防止所有细胞着色?

Java 我怎样才能防止所有细胞着色?,java,apache-poi,Java,Apache Poi,我的目标是用不同的属性设置行的单元格样式。这是使用函数styleExceptions(..)实现的。大多数函数都可以工作,但我面临的问题是,该行中的每个单元格都被涂成浅绿色,这是不应该发生的。我无法理解为什么会发生这种情况。有人能帮忙吗 public static void styleExceptions(CellStyle Exstyle, Font Exfont, Cell cell, AdditiveInformation obj){ Exfont.setFontHeightI

我的目标是用不同的属性设置行的单元格样式。这是使用函数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;
    }

问题在于你所做的比较

(Double)result==obj.get_xmonthreq()
=
用于比较基本值。您有两个选择:

  • 使用
    doubleValue()
    方法
    ((双)结果)。doubleValue()==obj.get\u xmonthreq().doubleValue()
  • 或者使用
    equals()
    方法
    ((双精度)结果).equals(obj.get\u xmonthreq())

  • 希望这能有所帮助。

    您的附加信息中有什么?它是一个类,而list list1是一个包含该类实例的列表您确定要在Exstyle中设置样式还是必须更改的单元格对象?此外,请尝试警告或打印cellToType(…)函数的返回值。不,尽管它不起作用,但该行中的所有单元格仍为浅绿色:(可能条件有问题,但我似乎无法发现错误。您是否相应地更改了所有条件?然后,您可以找到答案的唯一方法是逐行调试styleExceptions方法。