poi导入excel获取单元格格式小数位数,找了很久都没找到,还是从导出设置方法去反推的导入读取方法(2023.09.27 19:02):
Object value=null;
CellType cellType=cell.getCellType();
switch(cellType){
case STRING:
value=cell.getStringCellValue();
break;
case BOOLEAN:
value=cell.getBooleanCellValue();
break;
case NUMERIC:
value=cell.getNumericCellValue();
break;
case FORMULA:
try {
value=cell.getStringCellValue();
} catch (IllegalStateException e) {
// DecimalFormat df = new DecimalFormat("#.##");
XSSFCellStyle cellStyle = cell.getCellStyle();
//获取excel里面设置单元格样式里面设置的小数位数
String dataFormatString = cellStyle.getDataFormatString();//'0.00_ '
if(dataFormatString != null && dataFormatString.endsWith("_ ")){
dataFormatString = dataFormatString.substring(0, dataFormatString.length()-2);
}
DecimalFormat df = new DecimalFormat(dataFormatString);
value = df.format(cell.getNumericCellValue());
}
break;
default:
value="";
}