iwritecomeinmymind
iwritecomeinmymind

Reputation: 376

How to output the number from Excel as a String in java?

This is my code, which reading from excell file and insert to database, but when I insert to database numeric type from excell, it looks like this 9.94775287264E11, but I want to store this data like this 994775287264. This number value in excell store like this 994775287264

public static Response acceptFile(File file) {
        try {
            String phoneNumber = "";
            String textMessage = "";
            FileInputStream excelFile = new FileInputStream(file);
            Workbook workbook = new XSSFWorkbook(excelFile);
            Sheet datatypeSheet = workbook.getSheetAt(0);
            Iterator<Row> iterator = datatypeSheet.iterator();
            while (iterator.hasNext()) {

                Row currentRow = iterator.next();
                Iterator<Cell> cellIterator = currentRow.iterator();

                while (cellIterator.hasNext()) {

                    Cell currentCell = cellIterator.next();
                    //getCellTypeEnum shown as deprecated for version 3.15
                    //getCellTypeEnum ill be renamed to getCellType starting from version 4.0
                    if (currentCell.getCellTypeEnum() == CellType.STRING) {
                        phoneNumber = String.valueOf(currentCell.getStringCellValue());
                    } else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
                        textMessage = String.valueOf(currentCell.getNumericCellValue());
                    }

                }

            }
            insertExcellFileToDb(phoneNumber, textMessage);
            System.out.println(phoneNumber + " " + textMessage);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

Upvotes: 1

Views: 198

Answers (1)

iwritecomeinmymind
iwritecomeinmymind

Reputation: 376

I solved the problem. I've change the code to the following code:

if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
   phoneNumber = NumberToTextConverter.toText(currentCell.getNumericCellValue());
} else if (currentCell.getCellTypeEnum() == CellType.STRING) {
   textMessage = String.valueOf(currentCell.getStringCellValue());
}

Upvotes: 1

Related Questions