使用 POI API 在 Excel 中显示百分比值 [英] Display percentage values in Excel using POI API

查看:41
本文介绍了使用 POI API 在 Excel 中显示百分比值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to display a value in an excel cell formatted like a percentage, e.g. like 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?

推荐答案

您需要:

  1. 将您的数据设置为数字(浮点数),而不是文本.
  2. 将单元格格式指定为百分比.

类似于:

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

查看 POI 快速指南的用户定义格式部分更多细节.您可能还想阅读示例,其中展示了如何使用不同的 POI 功能.

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

这篇关于使用 POI API 在 Excel 中显示百分比值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆