[英]Cannot get a NUMERIC value from a STRING cell
获取错误java.lang.IllegalStateException:无法从STRING单元格获取NUMERIC值
员工类别:
@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:无法从STRING单元格获取NUMERIC值
您可以简单地跳过第一行,如下所示:
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.