本文介绍了Apache POI:如何格式化数字单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Apache POI 3.9 进行 XLS/XLSX 文件处理.

I am using Apache POI 3.9 for XLS/XLSX file processing.

在 XLS 表格中,有一列数值为3000053406".

In the XLS sheet, there is a column with numeric value like "3000053406".

当我用 POI 阅读它时......

When I read it with POI with..

cell.getNumericCellValue()

它给了我像3.00E+08"这样的价值.这在我的应用程序中造成了巨大的问题.

It gives me value like "3.00E+08". This create huge problem in my application.

如何在 Apcahe POI 中读取数据时设置数字格式?

How can I set the number formatting while reading data in Apcahe POI ?

我知道有一种方法是将列设置为文本"类型.但是我想知道在读取数据时Apache POI端是否还有其他方式.或者我们可以使用简单的 java DecimalFormatter 对其进行格式化吗?

There is a way that I know is to set the column as "text" type. But I want to know if there is any other way at Apache POI side while reading the data. OR can we format it by using simple java DecimalFormatter ?

推荐答案

这个问题经常出现....

This one comes up very often....

选择我过去对一个几乎相同问题的回答

你想要做的是使用 DataFormatter类.您将这个单元格传递给它,它会尽最大努力返回一个字符串,其中包含 Excel 将为您显示的该单元格内容.如果你传递给它一个字符串单元格,你会得到字符串.如果你传递给它一个应用了格式规则的数字单元格,它会根据它们格式化数字并将字符串返回给你.

对于您的情况,我假设数字单元格应用了整数格式规则.如果您要求 DataFormatter 格式化这些单元格,它会返回一个包含整数字符串的字符串.

For your case, I'd assume that the numeric cells have an integer formatting rule applied to them. If you ask DataFormatter to format those cells, it'll give you back a string with the integer string in it.

这篇关于Apache POI:如何格式化数字单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 12:15