Java Apache POI Excel数字单元格值pt-br

发布于 2025-01-20 10:51:21 字数 1416 浏览 2 评论 0 原文

我想在excel中设置 xssfcell 以将 cellType 作为数字显示,并需要它以 pt-br locale (使用用于分离器,而不是(不是十进制/分数 - 但仍然 double )。代码> CellTyle 仅获取字符串。 NumberFormat locale 执行技巧,但是 cellType 文本,而不是 numbern

数字已经配置为直接来自数据库(例如: 412.000 ),但将其转换为我们( 412,000

。到我的 的单元格,并将 cellType 设置为数字

我到目前为止的:

    XSSFCell cell = row.createCell(anyCellNumber);
    double value = 412.000; //valueThatComesFromDataBaseWithLocaleFormatted
    cell.setCellType(CellType.NUMERIC);
    //At this point, double has already converted the cell value to 412,000 - So, What I did was:
    cell.setCellValue(String.format("%.3f", value).replace(",", ".")); // Replaced the , to . to match Locale

这就是 code> cellType 已设置,但是 numberformat decimalformat locale string.format.format 等,将 CellValue 转到字符串,因此 cellType is text

可以使用 double.parsedouble(valueFormatted.toString())因为它会引发 exception ,如果 String code> locale 代码>格式。

因此,如何(如果可能的话),我可以拥有一个 cellType.numeric 以及我的 locale locale (pt-br)的单元格值

提前致谢!

I want to set the XSSFCell in Excel to display the CellType as numeric, and need it to diplay the value as pt-BR Locale (with . for separator instead of , (not decimal/fractional - but still double). I managed to diplay the correct cell value, but the CellTyle only gets String. I know that double have the US configuration 0.000,00, and a simple NumberFormat with Locale do the trick, but the CellType gets to text, not number.

The number already comes configured with Locale, which comes straight from Database. (Eg: 412.000), but double converts it to US (412,000).

So, how can I format the cell to my Locale and still set the CellType as Numeric ?

This is what I got so far:

    XSSFCell cell = row.createCell(anyCellNumber);
    double value = 412.000; //valueThatComesFromDataBaseWithLocaleFormatted
    cell.setCellType(CellType.NUMERIC);
    //At this point, double has already converted the cell value to 412,000 - So, What I did was:
    cell.setCellValue(String.format("%.3f", value).replace(",", ".")); // Replaced the , to . to match Locale

So, this is the point, the CellType is set, but NumberFormat, DecimalFormat, Locale, String.format, etc, gets the cellvalue to String, and thus the CellType is text.

Can´t use Double.parseDouble(valueFormatted.toString()) because it throws Exception, if the String is Locale formatted.

So, how (if it is possible), can I have a CellType.NUMERIC AND the cell value formatted for my Locale (pt-BR)???

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2025-01-27 10:51:21

据我了解,您特别希望您的单元格为数字类型,并且您的显示是“不约会”的东西吗?我想到的唯一方法是定义您自己的细胞风格。在Excel中可以使用自定义功能。在Apache-Poi中,在修补单元格式时可能有可能:

 ...
 Cell cell = row.createCell(cellIndex);
 CellStyle cellStyle = workbook.createCellStyle();
 CreationHelper createHelper = workbook.getCreationHelper();
 createHelper.createDataFormat("some specific custom data format").
 cell.setCellStyle(cellStyle);

但是,我不确定是否有办法。我不确定,但是这些细胞格似乎仅限于默认的excel样式

最简单的方法是使您的类型“ date”单元格。即使您的数据库结果是一个数字,也可以将其转换为日期,因为日期只是时间戳上的一层。我强烈建议您查看这种批准,而不是“攻击”与日期相似的数字的方式。

As far as I understand you specifically want your Cell to be of type numeric and your display to be something "not Date" ? The only approach that I clould think of would be to define your own cellStyle. This is possible in excel with custom functions. In apache-poi it could be possible when tinkering around with cell Styles:

 ...
 Cell cell = row.createCell(cellIndex);
 CellStyle cellStyle = workbook.createCellStyle();
 CreationHelper createHelper = workbook.getCreationHelper();
 createHelper.createDataFormat("some specific custom data format").
 cell.setCellStyle(cellStyle);

I am however not sure that there is a way. I am not sure but these cellStyles seem to be limited to the default excel styles https://www.roseindia.net/java/poi/setdataformat.shtml

The simplest approach would be to make your cell of Type "Date". Even if your result from the Database is a number, it can be converted to Date, since Date is only a layer upon a timestamp. I would highly suggest to look at this approch instead of "hacking" your way around number fromats looking similar to Dates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文