简体   繁体   English

Apache POI百分比格式不起作用

[英]Apache POI percentage format not working

I have a module where I have to take double values from html form and then populate them in an excel using Apache POI. 我有一个模块,我必须从html表单中获取双精度值,然后使用Apache POI在Excel中填充它们。

Excel cells are showing percentage. Excel单元格显示百分比。 Module is not working. 模块不起作用。 please see code 请看代码

    CellStyle style = wb.createCellStyle();
    style.setDataFormat(wb.createDataFormat().getFormat("##.##%"));
    sheet.getRow(4).getCell(3).setCellValue(wifimodel.getMobileSubscriberCountToday());
    sheet.getRow(4).getCell(3).setCellStyle(style);

Now wifimodel.getMobileSubscriberCountToday() value is 10.0 and in excel it comes as 1000,% 现在wifimodel.getMobileSubscriberCountToday()的值为10.0,在excel中为1000%

please help 请帮忙

Promoting a comment to an answer 对答案发表评论

In maths equivalences, and in computers, percentages are stored as relative to 1 not 100. As wikipedia explains , that's because a percentage is a number or ratio expressed as a fraction of 100. As such, 10% is not stored as 10 , but instead as 0.10 . 在数学等价物中和在计算机中,百分比相对于1而不是100进行存储。如Wikipedia所述 ,这是因为百分比是数字或比率,表示为100的分数。因此, 10%不会存储为10 ,而是改为0.10 That means that to store your value of 10% , you need to put it in an Apache POI cell formatted as a percentage, with a numeric value of 0.10 . 这意味着要存储10%的值,您需要将其放入以百分比表示的Apache POI单元格,其数值为0.10

As you have found, putting in a value of 10 is treated as 10*100 = 1,000 % , which is 100 times what you had in mind! 如您所知,将值10视为10*100 = 1,000 % ,这是您所想到的100倍!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM