简体   繁体   English

无法从STRING单元格获得NUMERIC值

[英]Cannot get a NUMERIC value from a STRING cell

Getting the error java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell 获取错误java.lang.IllegalStateException:无法从STRING单元格获取NUMERIC值

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 执行此操作时,它给出如下错误:java.lang.IllegalStateException:无法从STRING单元格获取NUMERIC值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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