0

I have a field in the input xlsx file which can contain either email or mobile number. While reading it, I am having the following logic -

Workbook workbook = WorkbookFactory.create(new File("./TXN_Log_Mar_18_input.xlsx"));
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext() && cnt <maxRows) {
    System.out.println("\n\nNew row "+cnt+" ");
    Row row = rowIterator.next();
    if(row.getCell(11).toString() !="" || row.getCell(12).toString() !="")
    {

        String debitUser = row.getCell(11).toString();
        System.out.println("input mobile? "+debitUser);
        if(emailFormat(debitUser) || mobileFormat(debitUser))
        {
            response  = umCall(debitUser);
        }   

    }
}

With a proper 10 digit mobile number, following is the output -

input mobile? 7.358681129E9
1
  • Try using DataFormatter as shown in Getting the cell contents. DataFormatter formatter = new DataFormatter(); ... String debitUser = formatter.formatCellValue(row.getCell(11)); Commented May 25, 2018 at 7:46

2 Answers 2

1

I assume you are using Apache POI, so I recommend you to not use toString() method. Use getCellValue() methods instead.

If you know the type of the value contained by your cell, you can use specific method like getNumericCellValue() that returns a double. Then it is up to you to format data as you wish.

double cellValue = row.getCell(11).getNumericCellValue();
System.out.println("input mobile? " + (int) cellValue);

Something like that maybe.

See Cell documentation to find the best way to do.

https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html

Also I recommend you to check the type of your cell before dealing with data.

Sign up to request clarification or add additional context in comments.

1 Comment

To test cell type, you can use getCellType() and compare to CellType.NUMERIC.
0

I can't see the type returned by getCell(), but I'm guessing it is a BigDecimal, or something very similar. BigDecimal has toPlainString(), which prevents formatting large numbers using exponential notation. Whatever the type returned, look at its methods for something similar.

1 Comment

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.