我需要使用特定格式在数字单元格中写入Double值,我的意思是,生成的xls必须具有包含Double值的数字单元格,例如:8,1。我正在尝试类似的东西:
DecimalFormat dFormat = new DecimalFormat("##.#"); dFormat.format(doubleValue);
但是,由于format方法返回一个String,所以无论我是否将单元格创建为数字单元,它们始终表现为文本单元格。我在考虑两种选择:
任何想法?
我可能会丢失一些内容,但我认为您只想使用格式规则设置单元格的样式以显示单个小数位
// 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,然后设置其样式