简体   繁体   中英

How do i retrieve integer values from excel using Java

My Excel file has Telephone numbers and Integer like 0772354573 and 56674 but my output is like 07723545E9 and 56674.0, Instead i need my output to be like as defined in the excel as 0772354573 and 56674.

Below is the Getcell data class im using which i downloaded from an external source.

public String getCellData(String sheetName,String colName,int rowNum){
    try{
        if(rowNum <=0)
            return "";

        int index = workbook.getSheetIndex(sheetName);
        int col_Num=-1;
        if(index==-1)
            return "";

        sheet = workbook.getSheetAt(index);
        row=sheet.getRow(0);
        for(int i=0;i<row.getLastCellNum();i++){
            //System.out.println(row.getCell(i).getStringCellValue().trim());
            if(row.getCell(i).getStringCellValue().trim().equals(colName.trim()))
                col_Num=i;
        }
        if(col_Num==-1)
            return "";

        sheet = workbook.getSheetAt(index);
        row = sheet.getRow(rowNum-1);
        if(row==null)
            return "";
        cell = row.getCell(col_Num);

        if(cell==null)
            return "";
        //System.out.println(cell.getCellType());
        if(cell.getCellType()==CellType.STRING)
            return cell.getStringCellValue();
        else if(cell.getCellType()==CellType.NUMERIC || cell.getCellType()==CellType.FORMULA ){

            String cellText  = String.valueOf(cell.getNumericCellValue());
            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                // format in form of M/D/YY
                double d = cell.getNumericCellValue();

                Calendar cal =Calendar.getInstance();
                cal.setTime(HSSFDateUtil.getJavaDate(d));
                cellText =
                        (String.valueOf(cal.get(Calendar.YEAR))).substring(2);
                cellText = cal.get(Calendar.DAY_OF_MONTH) + "/" +
                        cal.get(Calendar.MONTH)+1 + "/" +
                        cellText;

                //System.out.println(cellText);

            }



            return cellText;
        }else if(cell.getCellType()==CellType.BLANK)
            return "";
        else
            return String.valueOf(cell.getBooleanCellValue());

    }
    catch(Exception e){

        e.printStackTrace();
        return "row "+rowNum+" or column "+colName +" does not exist in xls";
    }
}

If you store the long integer values then the excel will compress and store them so you will see values like Below :

在将<code>'</ code>附加为前缀之前

If you don't want to store the values as integers or if you want to store/retrieve the values as it is and if you don't want to change your code then you can do like below :

将<code>'</ code>附加为前缀后

Append an ' symbol before the number like above. For example, if the number is 118995099890796 then you do like this '118995099890796 . It means, it will treat that as a string and you can read that value using getStringCellValue() method.

I hope it helps...

Tough not ideal, but i would suggest to convert Number cell to String cell before reading cell value. And then parse value into int value;

if(cell.getCellType()==CellType.NUMERIC) {
 cell.setCellType(Cell.CELL_TYPE_STRING);
 String cellValue=cell.getStringCellValue();
 int numericValue=Integer.parseInt(cellValue);
}

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