简体   繁体   中英

Cannot get a NUMERIC value from a STRING cell

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

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()) );
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM