Java 细胞没有着色
因此,我尝试使用此函数根据某些特定值为单元格着色:Java 细胞没有着色,java,colors,apache-poi,Java,Colors,Apache Poi,因此,我尝试使用此函数根据某些特定值为单元格着色: public static void styleExceptions(CellStyle Exstyle, Font Exfont, Cell cell, AdditiveInformation obj){ Exfont.setFontHeightInPoints((short) 10); Exfont.setFontName("Calibri"); Exstyle.setFont(Exfont); Exstyl
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");
}
}
您确定该方法: 细胞类型(…) 按你的预期工作 我认为问题在于程序没有进入
if
这里:
Object result=null;
cellToType(cell,result);
if(result instanceof Double) {
你能发布你的cellToType(…)
函数吗
编辑:
您似乎错误地比较了双变量。不要使用
=
尝试使用result.equalsTo(…)
您的cellToStyle
方法不起作用,因为您希望更改styleExceptions
方法中的result
对象。由于Java通过值传递对对象的引用,cellToStyle
方法在其自己的result
参数中接收result
引用的副本。在该方法中,您可以更改该本地引用,但这对styleExceptions
中的result
引用没有任何影响
private static Object cellToType(Cell cell){
Object result;
// ...
您需要在cellToStyle
中返回该引用,并将其分配给styleExceptions
中的result
private static Object cellToType(Cell cell){
Object result;
// ...
及
最后。然后,在styleExceptions
中,执行以下操作:
Object result = cellToType(cell);
刚刚发布,你可以检查它不是用于字符串和对象的吗?非双精度?应用于所有对象,例如双精度。但是也要记住,
getNumericCellValue()
返回原语double,因此您正在比较对象与double。但是我使用double转换使对象成为原语double否,您的转换是Object->double
-到double的对象版本。另外,在这里添加额外的括号:((双)结果)==obj.getzmonthreq()
因为我不知道您是在强制执行结果还是=
操作的结果。(可能是个bug)您没有使用cell.setCellStyle(Exstyle)代码>任何地方。。。设置样式后,您需要在所需的单元格上应用单元格样式。谢谢,我昨天意识到了这一点,并在昨天进行了更改,谢谢thoug:),但随后我以类似的方式传递了Exstyle对象,我在main()中使用了这个(Exstyle)对象,那么为什么这样做,我现在有点困惑…您正在通过调用引用变量的方法来操作引用变量Exstyle
引用的对象,而不是将新对象分配给引用变量。这就是区别。