写有特定格式的数字单元Double值在Apache中浦二3.7 [英] Write Double value in numeric cell with specific format in Apache Poi 3.7
问题描述
我需要使用特定格式的数字单元写入一个Double值,我的意思是,生成的XLS必须有像含双值,如数字单元:8,1。我想是这样的:
I need to write a Double value in a numeric cell using a specific format, i mean, the generated xls must have numeric cells containing Double values like, for example: 8,1. I am trying something like:
DecimalFormat dFormat = new DecimalFormat("##.#");
dFormat.format(doubleValue);
但是,因为格式方法返回一个字符串,不管我是否建立细胞作为数字与否,他们总是表现为文本的单元格。我在想两个选项:
But, since format method returns a String, no matter whether I create cells as numeric or not, they always behave as text cells. I was thinking about two options:
- 强制细胞表现为数字单元。
- 忘记了DecimalFormat的,并使用Double类指定逗号作为小数点分隔符,是什么我不知道这是可能的。
你知道吗?
推荐答案
我可能失去了一些东西,但我认为你只是想用样式的格式规则的单元格中显示一个小数位
I may be missing something, but I think you just want to style the cell with a format rule to display a single decimal place
// Do this only once per file
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
wb.getCreationHelper().createDataFormat().getFormat("#.#"));
// Create the cell
Cell c = row.createCell(2);
c.setCellValue(8.1);
c.setCellStyle(cellStyle);
这将创建一个格式规则,创造一个单元格,设置单元是值8.1,那么它的样式
That will create a formatting rule, create a cell, set the cell to be the value 8.1, then style it
这篇关于写有特定格式的数字单元Double值在Apache中浦二3.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!