JAva excelの日付タイプデータまたはカスタムタイプ日付をpoiで読み込む

9491 ワード

JavaがExcelテーブルの日付型データを読み込むと、このような12-10月-2019が読み出されますが、Excelに入力されているのは2019/10/12または2019-10-12です
poiがexcelを処理する場合、excelがどのタイプのデータであるかを明確に指定していない場合、poiはセルの日付データを処理する場合、一連の数字である可能性が高く、javaプログラムではほとんど変換できません.
以上の問題を解決するために、私は各種の資料を集めて、今総括して、このような問題に出会ったあなたに参考に供します.
Excelデータ処理:
Excelは日付・時間ともに数値タイプで格納しており、読み込み時にPOIが数値タイプかどうかを判断してから判断変換する
1、数値フォーマット(CELL_TYPE_NUMERIC):
1.純数値フォーマット:getNumericCellValue()直接データ取得
2.日付フォーマット:yyyy-MM-dd,d/m/yyyyy h:mm,HH:mmなど文字を含まない日付フォーマットを扱う
1).日付書式:HSSFDateUtil.isCellDateFormatted(cell)
すべての日付フォーマットはgetDataFormat()値で判断できます
yyyy-MM-dd----- 14
yyyy m d --- 31
yyyy m ------- 57
m d   ---------- 58
HH:mm----------- 20
h mm   ------- 32

次にexcelの様々なデータ型に対して変換に入ります.基本的には必要なものをカバーします.必要なものがなければ、その上で追加することができます.
//
    public static String getCellValueByCell(Cell cell) {
        //     null   
        if (cell==null || cell.toString().trim().equals("")) {
            return "";
        }
        String cellValue = "";
        int cellType=cell.getCellType();
        switch (cellType) {
        case Cell.CELL_TYPE_NUMERIC: //   
            short format = cell.getCellStyle().getDataFormat();
            if (DateUtil.isCellDateFormatted(cell)) {
                SimpleDateFormat sdf = null;  
                //System.out.println("cell.getCellStyle().getDataFormat()="+cell.getCellStyle().getDataFormat());
                if (format == 20 || format == 32) {  
                    sdf = new SimpleDateFormat("HH:mm");  
                } else if (format == 14 || format == 31 || format == 57 || format == 58) {  
                    //          :m d (          id  ,id   58)  
                    sdf = new SimpleDateFormat("yyyy-MM-dd");  
                    double value = cell.getNumericCellValue();  
                    Date date = org.apache.poi.ss.usermodel.DateUtil  
                            .getJavaDate(value);  
                    cellValue = sdf.format(date);  
                }else {//     
                    sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
                }  
                try {
                    cellValue = sdf.format(cell.getDateCellValue());//   
                } catch (Exception e) {
                    try {
                        throw new Exception("exception on get date data !".concat(e.toString()));
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }finally{
                    sdf = null;
                }
            }  else {
                BigDecimal bd = new BigDecimal(cell.getNumericCellValue()); 
                cellValue = bd.toPlainString();//       BigDecimal     plainString,            
            }
            break;
        case Cell.CELL_TYPE_STRING: //    
            cellValue = cell.getStringCellValue();
            break;
        case Cell.CELL_TYPE_BOOLEAN: // Boolean
            cellValue = cell.getBooleanCellValue()+"";;
            break;
        case Cell.CELL_TYPE_FORMULA: //   
            cellValue = cell.getCellFormula();
            break;
        case Cell.CELL_TYPE_BLANK: //   
            cellValue = "";
            break;
        case Cell.CELL_TYPE_ERROR: //   
            cellValue = "ERROR VALUE";
            break;
        default:
            cellValue = "UNKNOW VALUE";
            break;
        }
        return cellValue;
    }