[英]cells are not being colored
因此,我尝试使用此函数根据某些特定值为单元格着色:
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);
}
}
}
除着色部分以外的所有其他内容都在函数中起作用,我对结果对象做错了,导致没有满足条件的单元格被着色。
这是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");
}
}
您确定方法:
cellToType(...)
符合您的预期?
我认为问题在于该程序没有进入if
:
Object result=null;
cellToType(cell,result);
if(result instanceof Double) {
您可以发布cellToType(...)
函数吗?
编辑:您似乎错误地比较了双变量。 代替==
尝试使用result.equalsTo(...)
您的cellToStyle
方法无法正常工作,因为您希望如何在styleExceptions
方法中更改result
对象。 由于Java按值传递对对象的引用,因此cellToStyle
方法在其自己的result
参数中接收result
引用的副本。 在该方法中,您可以更改该本地引用,但这对styleExceptions
的result
引用没有任何styleExceptions
。
你需要返回该参考cellToStyle
并将其分配给result
的styleExceptions
。
private static Object cellToType(Cell cell){
Object result;
// ...
和
return result;
}
在末尾。 然后,在styleExceptions
,执行以下操作:
Object result = cellToType(cell);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.