Java 使用 POI API 在 Excel 中显示百分比值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1613202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:49:57  来源:igfitidea点击:

Display percentage values in Excel using POI API

javaexcelapache-poi

提问by nagireddy

I need to display a value in an excel cell formatted like a percentage, e.g. like 12.3%.

我需要在格式为百分比的 excel 单元格中显示一个值,例如像12.3%.

By default the value is displayed as Text, but I need to display it as a number.

默认情况下,该值显示为文本,但我需要将其显示为数字。

What is the appropriate method to achieve this?

实现这一目标的适当方法是什么?

回答by ChssPly76

You need to:

你需要:

  1. Set your data as number (floating-point), not as text.
  2. Specify cell format as percentage.
  1. 将您的数据设置为数字(浮点数),而不是文本。
  2. 将单元格格式指定为百分比。

Something like:

就像是:

cell.setCellValue(0.123); // set value as number
CellStyle style = workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat("0.000%"));
cell.setCellStyle(style);

Take a look at user defined formats sectionof POI quick guide for more details. You may also want to go through the exampleswhich show how to use different POI capabilities.

有关更多详细信息,请查看POI 快速指南的用户定义格式部分。您可能还想查看展示如何使用不同 POI 功能的示例

回答by Aasim ali

POI has built-in formats check this linkfirst

POI有内置的格式,检查此链接第一

and check this linkfor example

并检查此链接例如

For percentage, it will be something like this

对于百分比,它将是这样的

dataCell.setCellValue(.12)
CellStyle stylePercentage = workbook.createCellStyle();
stylePercentage.setDataFormat(workbook.createDataFormat()
.getFormat(BuiltinFormats.getBuiltinFormat( 10 )));
 dataCell.setCellStyle(stylePercentage);