2011-03-17 314 views

回答

3

假設你最初有2個細胞A1B1用數字(在我的示例,這些數字是2411和1333),並說,在小區中的公式

C = A1/B1 

試試這個

System.out.println ("formula "+ cell1.getCellFormula()); 

if(cell1.getCellType() == Cell.CELL_TYPE_FORMULA) { 
       CellValue cv = evaluator.evaluate(cell1); 
       System.out.println ("cv "+ cv.formatAsString()); 



      DecimalFormat df = new DecimalFormat("###.##%"); 
      System.out.println("Formatted as percentage "+df.format(Math.abs(cell1.getNumericCellValue()))); 



} 

輸出爲

formula A1/B1 

cv 1.808702175543886 

Formatted as percentage 180.87% 

將A1更改爲否定,這仍然有效。

+0

但之後,我怎麼能設置180.87%的值在單元格 – water 2011-03-17 11:38:24

+0

@water:使用'cell.setCellValue'和值 – JoseK 2011-03-17 12:54:32