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

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

问题描述

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



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



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

解决方案

您需要:


  1. 将数据设置为数字(浮点数),而不是文本。

  2. 指定单元格格式为百分比。

类似于:

  cell.setCellValue(0.123); //将值设置为number 
CellStyle style = workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat()。getFormat(0.000%));
cell.setCellStyle(style);

查看用户定义的格式部分。您也可以浏览示例,该示例展示如何使用不同的POI功能。 / p>

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?

解决方案

You need to:

  1. Set your data as number (floating-point), not as text.
  2. Specify cell format as percentage.

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 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天全站免登陆