0

Getting the error java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell

Employee class:`

@Entity
@Table(name="Employee")
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int Id;
@Column(name = "firstname")
private String FirstName;
@Column(name = "lastname")
private String LastName;
@Column(name = "email")
private String Email;
@Column(name = "mobile")
private long Mobile;

@Transient
private MultipartFile file;

getters and setters.....

Service Class:

Workbook book=null;
                try {
                    book = new HSSFWorkbook(file.getInputStream());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Sheet sheet=book.getSheetAt(0);
                Iterator<Row> rows =    sheet.iterator();

        while(rows.hasNext()) {
            Row row = rows.next();

                emp.setId(row.getCell(0).getNumericCellValue());

                emp.setFirstName(row.getCell(1).getStringCellValue());

                emp.setLastName(row.getCell(2).getStringCellValue());

                emp.setEmail(row.getCell(3).getStringCellValue());

                emp.setMobile(NumberToTextConverter.toText(row.getCell(4).getNumericCellValue()) );
            }
        }

While executing this it is giving error like:java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell

1 Answers1

0

You can simply skip the first line as follows:

            Workbook book=null;
            try {
                book = new HSSFWorkbook(file.getInputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Sheet sheet=book.getSheetAt(0);
            Iterator<Row> rows =    sheet.iterator();


    // SKIP HEADER
    if (rows.hasNext()) {
        rows.next();
    }

    while(rows.hasNext()) {
        Row row = rows.next();

            emp.setId(row.getCell(0).getNumericCellValue());

            emp.setFirstName(row.getCell(1).getStringCellValue());

            emp.setLastName(row.getCell(2).getStringCellValue());

            emp.setEmail(row.getCell(3).getStringCellValue());

           emp.setMobile(NumberToTextConverter.toText(row.getCell(4).getNumericCellValue()) );
        }
    }
firegloves
  • 5,581
  • 2
  • 29
  • 50