【发布时间】:2019-08-08 19:02:07
【问题描述】:
收到错误 java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell
员工类:`
@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.....
服务类:
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()) );
}
}
在执行此操作时,它会给出如下错误:java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell
【问题讨论】:
-
能否请您在发生错误的行旁边设置注释?
-
你的 Excel 表有页眉吗?如果是这样,它将在解析标头时失败。
-
@Razib 我实际上不知道哪一行出现此错误。
-
因此,它试图将字符串标头解析为数字。为什么不跳过标题?
标签: java spring-boot apache-poi