在 Java Poi (Excel) 中更改单元格格式(长度?)
Change cell format (Length?) in Java Poi (Excel)
我一直在寻找一种方法来执行此操作,但只找到了几种调整列大小的方法,而且还不够。
很简单:
当我写入一个 excel 文件时,我遇到了一个数字高于 10.000.000 的点,并且格式发生了变化,例如:
row 11: 9.800.000
row 12: 9.900.000
row 13: 1.0E7
row 14: 1.01E7
如何防止这种情况发生?我需要完整的数字,因为我稍后会从我的代码中再次访问此信息。
(我无法手动编辑 excel,因为我是动态创建的,我猜(并希望)在某些 Java POI class 中有一个变量,我可以为其分配一个值以更改?)
谢谢大家
最简单的方法可能是在从 excel 文件中提取值时转换值。
BigDecimal bigNumber = new BigDecimal(cell.getNumericCellValue());
//Use bigNumber when doing the calculations
根据您的示例,您必须将 DataFormat 设置为 "#.000"
:
DataFormat format = workbook.createDataFormat();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(format.getFormat("#.000"));
cell.setCellStyle(cellStyle);
其中:
0 - Digit placeholder. For example, if you type 8.9 and you want it to display as 8.90, then use the format #.00
# - Digit placeholder. Follows the same rules as the 0 symbol except Excel does not display extra zeros when the number you type has fewer digits on either side of the decimal than there are # symbols in the format. For example, if the custom format is #.## and you type 8.9 in the cell, the number 8.9 is displayed.
更多 excel 格式:How to control and understand settings in the Format Cells dialog box in Excel
这是一个单元格格式化案例,默认情况下Excel对低于特定值的数字使用指数格式,对于 2013 版本,它低于 1E10。对于旧版本,我猜它是在 1E7 左右。
要解决此问题,您需要在创建 xls 文件之前使用 Java 代码格式化单元格,使用 HSSFCellStyle,检查以下代码:
HSSFCellStyle style = workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat("#"));
Double d = 10000000000.0;
thecell.setCellValue(d);
thecell.setCellType(Cell.CELL_TYPE_NUMERIC);
thecell.setCellStyle(style);
您可能还需要通过添加
来调整列的大小以适合数字
sheet.autoSizeColumn(0);
这是一个带有单元格格式 (A1) 和不带格式 (B1) 的输出示例:
不要忘记导入必要的 类。
我一直在寻找一种方法来执行此操作,但只找到了几种调整列大小的方法,而且还不够。
很简单:
当我写入一个 excel 文件时,我遇到了一个数字高于 10.000.000 的点,并且格式发生了变化,例如:
row 11: 9.800.000
row 12: 9.900.000
row 13: 1.0E7
row 14: 1.01E7
如何防止这种情况发生?我需要完整的数字,因为我稍后会从我的代码中再次访问此信息。
(我无法手动编辑 excel,因为我是动态创建的,我猜(并希望)在某些 Java POI class 中有一个变量,我可以为其分配一个值以更改?)
谢谢大家
最简单的方法可能是在从 excel 文件中提取值时转换值。
BigDecimal bigNumber = new BigDecimal(cell.getNumericCellValue());
//Use bigNumber when doing the calculations
根据您的示例,您必须将 DataFormat 设置为 "#.000"
:
DataFormat format = workbook.createDataFormat();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(format.getFormat("#.000"));
cell.setCellStyle(cellStyle);
其中:
0 - Digit placeholder. For example, if you type 8.9 and you want it to display as 8.90, then use the format #.00
# - Digit placeholder. Follows the same rules as the 0 symbol except Excel does not display extra zeros when the number you type has fewer digits on either side of the decimal than there are # symbols in the format. For example, if the custom format is #.## and you type 8.9 in the cell, the number 8.9 is displayed.
更多 excel 格式:How to control and understand settings in the Format Cells dialog box in Excel
这是一个单元格格式化案例,默认情况下Excel对低于特定值的数字使用指数格式,对于 2013 版本,它低于 1E10。对于旧版本,我猜它是在 1E7 左右。
要解决此问题,您需要在创建 xls 文件之前使用 Java 代码格式化单元格,使用 HSSFCellStyle,检查以下代码:
HSSFCellStyle style = workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat("#"));
Double d = 10000000000.0;
thecell.setCellValue(d);
thecell.setCellType(Cell.CELL_TYPE_NUMERIC);
thecell.setCellStyle(style);
您可能还需要通过添加
来调整列的大小以适合数字sheet.autoSizeColumn(0);
这是一个带有单元格格式 (A1) 和不带格式 (B1) 的输出示例:
不要忘记导入必要的 类。