細胞

2014-02-27 170 views
1

所以我試圖色細胞使用該功能基於某些特定的值:除着色部分其他細胞

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"); 
     } 
    } 
+0

你沒有使用'cell.setCellStyle(擴展風格);'任何地方...... 設定的樣式後,您需要申請所需的細胞的cellstyle。 – Sankumarsingh

回答

0

確定方法:

cellToType(...)

的作品,你期待?

我認爲這個問題是程序不進入這裏的if

Object result=null; 
cellToType(cell,result); 
if(result instanceof Double) { 

您可以發佈您cellToType(...)功能?

編輯: 你似乎錯誤地比較雙變量。取而代之的==嘗試使用result.equalsTo(...)

+0

剛發佈它,你可以檢查它 – Anish6595

+0

好吧,檢查我的編輯。 – FazoM

+0

是不是用於字符串和對象?不是雙打? – Anish6595

0

,因爲你是如何期待改變result對象在styleExceptions方法你cellToStyle方法是行不通的。由於Java按值傳遞對象的引用,因此cellToStyle方法會在其自己的result參數中收到result引用的副本。在該方法中,您更改了本地引用,但對styleExceptions中的result引用沒有任何作用。

您需要在cellToStyle中返回該參考號,並將其分配給resultstyleExceptions

private static Object cellToType(Cell cell){ 
    Object result; 
    // ... 

return result; 
} 

在末端。然後,在styleExceptions,這樣做:

Object result = cellToType(cell); 
+0

雅感謝我意識到昨天,並作出了昨天的變化 感謝thoug :) – Anish6595

+0

但隨後我以類似的方式傳遞Exstyle對象,並在main()中使用此(Exstyle)對象。 ,那麼爲什麼這個工作,現在我有點困惑... – Anish6595

+0

你正在操作引用變量'Exstyle'引用的對象,通過調用它的方法,而不是分配一個新的對象引用變量。這是不同的。 – rgettman