简体   繁体   English

如何将Excel数字存储为字符串

[英]How to store excel numeric as string

How can i set numeric number from excel to string type? 如何设置从Excel到字符串类型的数字? as i want to show 0001 instead of 1 in system. 因为我想在系统中显示0001而不是1。 Below are part of the detect excel cell type function. 以下是“检测Excel单元格类型”功能的一部分。 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, Thilo给出了最简单的方法,

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

Use LongValidator to achieve the same by third party library. 使用LongValidator通过第三方库实现相同的目的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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