1

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;
    }
6
  • How are you effectively storing the data in the database? In the snipped you provided both phoneNumber and textMessage are strings, but somewhere they are being converted to numbers. Maybe in insertExcellFileToDb? Commented Oct 13, 2021 at 20:58
  • Why not just call getStringCellValue() ? Commented Oct 13, 2021 at 21:03
  • @Picoral in my database phone_column type String, so I just want to store it as true way, not only weird. Commented Oct 13, 2021 at 21:13
  • I solved already which using the following code phoneNumber = NumberToTextConverter.toText(currentCell.getNumericCellValue()); Commented Oct 13, 2021 at 21:23
  • 1
    @tjheslin1 I explain my own question! Commented Oct 15, 2021 at 7:18

1 Answer 1

1

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());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.