1

I am trying to import the data from Excel to MySQL.

    java.util.ArrayList;
    import java.util.Iterator;

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.DataFormatter;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;

    import moduleobject.Summary;

    public class SummaryReader implements SheetReader<ArrayList<Summary>> {

        public ArrayList<Summary> read(Sheet sheet) {

            Iterator<Row> rowiterator = sheet.iterator();
            rowiterator.next();
            ArrayList<Summary> list = new ArrayList<>();
            Summary obj = new Summary();
            while (rowiterator.hasNext()) {

                Row nextRow = rowiterator.next();
                Iterator<Cell> celliterator = nextRow.cellIterator();

                while (celliterator.hasNext()) {

                    obj.setProcessor(celliterator.next().getStringCellValue());
                    obj.setProcessName(celliterator.next().getStringCellValue());

                    obj.setLevel(celliterator.next().getNumericCellValue());
                    obj.setPA11(celliterator.next().getNumericCellValue());
                    obj.setPA21(celliterator.next().getNumericCellValue());
                    obj.setPA22(celliterator.next().getNumericCellValue());

                    // obj.setPA31(celliterator.next().getNumericCellValue());
                    // obj.setPA32(celliterator.next().getNumericCellValue());
                }
            }

            list.add(obj);

            return list;
        }

    }

The error I am getting is

Exception in thread "main" java.lang.IllegalStateException: Cannot get a numeric value from a text cell
    at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:994)
    at org.apache.poi.xssf.usermodel.XSSFCell.getNumericCellValue(XSSFCell.java:305)
    at reader.SummaryReader.read(SummaryReader.java:32)
    at reader.SummaryReader.read(SummaryReader.java:1)
    at controller.JobController.startJob(JobController.java:69) at job.JobLauncher.main(JobLauncher.java:21)

Why do I get this exception?

7
  • 2
    Why jpa and Projecttag? Commented Jul 20, 2016 at 6:06
  • 2
    I think the error message is pretty clear. The value in the cell is formated as Text not as numeric Commented Jul 20, 2016 at 6:07
  • no the cell is having a numeric value Commented Jul 20, 2016 at 6:09
  • It has a numeric value. but formated as text!! Commented Jul 20, 2016 at 6:10
  • 2
    read the cell value as text and parse it using Integer.parse(string) Commented Jul 20, 2016 at 6:26

2 Answers 2

2

Please use:

DataFormatter formatter = new DataFormatter();
Cell cell = sheet.getRow(i).getCell(0);
String var_name = formatter.formatCellValue(cell);
Sign up to request clarification or add additional context in comments.

1 Comment

this thing doesn't work for me though, the out put of it something like: SUM(I38+D39+F39-G39-H39)
-1

Perhaps,one of the way is that you can set the cell type as

celliterator.next().setCellType(CELL_TYPE_STRING); 

before you read the cell value and read the cell value as string.Then you can parse the string value to number if needed.

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.