poi 读取excel 的问题 日期和数值会有问题的解决方案

来源:互联网 发布:日本帝国数据银行 编辑:程序博客网 时间:2024/05/19 20:47

当excel 中的数据为数值或日期是需要特殊处理如下面红色字体部分

switch(cell.getCellType()){
        case  HSSFCell.CELL_TYPE_NUMERIC:
         if (HSSFDateUtil.isCellDateFormatted(cell)) {  
                double d = cell.getNumericCellValue();  
                Date date = HSSFDateUtil.getJavaDate(d); 
                SimpleDateFormat dformat=new SimpleDateFormat("yyyy-MM-dd");
                cellValue=dformat.format(date);
          System.out.println("=========date="+ dformat.format(date));     
             }else{
           NumberFormat nf = NumberFormat.getInstance();
           nf.setGroupingUsed(false);//true时的格式:1,234,567,890
           cellValue= nf.format(cell.getNumericCellValue());//数值类型的数据为double,所以需要转换一下
           System.out.println("===CELL_TYPE_NUMERIC"+cellValue);
             }
        break;
        case HSSFCell.CELL_TYPE_STRING:
         System.out.println("===CELL_TYPE_STRING"+cell.getStringCellValue());
           cellValue=cell.getStringCellValue();
        break;
        case HSSFCell.CELL_TYPE_BOOLEAN:
         System.out.println("====CELL_TYPE_BOOLEAN"+cell.getBooleanCellValue());
         cellValue=String.valueOf(cell.getBooleanCellValue());
        break;
        case HSSFCell.CELL_TYPE_FORMULA:
         System.out.println("====CELL_TYPE_FORMULA"+cell.getCellFormula());
         cellValue=String.valueOf(cell.getCellFormula());
        break;
        
        default:
         cellValue="";
        System.out.println("unsuported cell type");
        break;
       }

原创粉丝点击