0

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;                      
        }   
    }
user3835327
  • 1,194
  • 1
  • 14
  • 44
  • 1
    When you say "store excel numeric as string", do you actually mean "get cell value as text, formatted as Excel displays it"? – Andreas Aug 19 '15 at 02:22
  • possible duplicate of [Cannot get a text value from a numeric cell “Poi”](http://stackoverflow.com/questions/30125465/cannot-get-a-text-value-from-a-numeric-cell-poi) – Andreas Aug 19 '15 at 02:26

1 Answers1

0

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");
newuser
  • 8,338
  • 2
  • 25
  • 33