How can I set a percentage formula in Apache POI?
How can I set formula with counting percentage value in Apache POI?
And how can it make as a abso开发者_运维问答lute value?
For example, if it is -4.00%
then it converted into 4.00
Assuming you originally have 2 cells A1
and B1
with numbers (in my example, these numbers are 2411 and 1333), and say the formula in cell
C = A1/B1
Try this
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())));
}
Output is
formula A1/B1
cv 1.808702175543886
Formatted as percentage 180.87%
Change A1 to a negative, and this will still work.
精彩评论