如何在Apache POI中设置百分比公式?
答
假设你最初有2个细胞A1
和B1
用数字(在我的示例,这些数字是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更改为否定,这仍然有效。
但之后,我怎么能设置180.87%的值在单元格 – water 2011-03-17 11:38:24
@water:使用'cell.setCellValue'和值 – JoseK 2011-03-17 12:54:32