50,545
社区成员
发帖
与我相关
我的任务
分享
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(true);
nf.setMinimumFractionDigits(12);
if (type == CellType.FORMULA) {
System.out.println("cell type=CellType.FORMULA");
double value = cell.getNumericCellValue();
System.out.println("value=" + value);
String str = nf.format(value);
System.out.println("format value=" + str);
}
为何我用这个代码,必须要打开excel保存以下,才可以获取到有公式的具体值??? 大神们,有没有其他好的解决方案呢?? public static String getStringVal(HSSFCell cell) {
DecimalFormat df = new DecimalFormat("#");
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue() ? "TRUE" : "FALSE";
case Cell.CELL_TYPE_FORMULA:
return cell.getCellFormula();
case Cell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
}
return df.format(cell.getNumericCellValue());
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
default:
return "";
}
}