0

I'm trying to have numbers display as percentages in my cell. So far the calculations seems to all be working but I cannot get the numbers displayed to be formatted as percentages I've tried two different ways so far.

CellValue cv = evaluator.evaluate(cell);
                    DecimalFormat df = new DecimalFormat("###.##%");
                   cell.setCellValue(df.format(Math.abs(cell.getNumericCellValue())));

This way basically evaluates the cell and then gives it a java decimalFormat and uses java to create the formatting and the just places the result into the cell. The second method uses a CellStyle to try and attempt it and it looks like this

        CellStyle stylePercent = wb.createCellStyle();
    stylePercent.setDataFormat(wb.createDataFormat().getFormat("0.000%"));

where I then places the values into the sheet and then apply the style to the cell.

Neither of these have worked for me always leaving the value of 0.5 as 0.5 instead of 50% or even giving it 0.5%, it always just remains the same as without the styling.

This is also how I am applying the style and my formula as well.

            cell.setCellFormula(""+cellValue+"/(SUM(D"+(row.getRowNum()+1)+" + F"+(row.getRowNum()+1)+" + H"+(row.getRowNum()+1)+"+ J"+(row.getRowNum()+1)+" + L"+(row.getRowNum()+1)+"+ N"+(row.getRowNum()+1)+"+ P"+(row.getRowNum()+1)+"+ R"+(row.getRowNum()+1)+"+ T"+(row.getRowNum()+1)+"+ V"+(row.getRowNum()+1)+"+ X"+(row.getRowNum()+1)+")+ Z"+(row.getRowNum()+1)+")");
                    cell.setCellStyle(stylePercent);
Ian Gallant
  • 35
  • 2
  • 7
  • maybe you should take a look here : http://stackoverflow.com/questions/1613202/display-percentage-values-in-excel-using-poi-api – VLEFF Jul 06 '15 at 14:34
  • This is doing the same thing as my second method. I'm going to try changing where I set the cell value currently I'm doing it before the style but I'm going to change it and see what happens. – Ian Gallant Jul 06 '15 at 14:42
  • 1
    Cell Styles aren't magic, you need to assign them to cells for them to work! – Gagravarr Jul 06 '15 at 16:57

0 Answers0