简体   繁体   中英

How to store excel numeric as string

How can i set numeric number from excel to string type? as i want to show 0001 instead of 1 in system. Below are part of the detect excel cell type function. How should i modify it ?

switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                value = dateFormat.format(cell.getDateCellValue());
            } 
            else 
            {
                double value2 = cell.getNumericCellValue();
                if( Math.floor(value2) == value2 ) 
                {
                    int value3 = (int)value2;
                    value = ""+value3;
                }else{
                    value = String.valueOf(value2);
                }
            }
            break;
        case HSSFCell.CELL_TYPE_STRING:
            value = cell.getStringCellValue();
            break;
        case HSSFCell.CELL_TYPE_FORMULA:     
            if(cell.getCachedFormulaResultType()==HSSFCell.CELL_TYPE_NUMERIC)
            {
                double value2 = cell.getNumericCellValue();
                if( Math.floor(value2) == value2 ) 
                {
                    int value3 = (int)value2;
                    value = ""+value3;
                }else{
                    value = String.valueOf(value2);
                }                                       
            }else if(cell.getCachedFormulaResultType()==HSSFCell.CELL_TYPE_STRING){
                value = cell.getStringCellValue();
            }   
            break;      
        case HSSFCell.CELL_TYPE_BLANK:   
            value = "";
            break;                      
        }   
    }

Thilo gave the easiest way to do this,

String.format("%04d", 1);

Use LongValidator to achieve the same by third party library.

LongValidator longValidator = new LongValidator();
String result = longValidator.format(1, "0000");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM