解析C:/imagF/student.xlsx下面的student.xlsx文件
springBoot的依赖:
<!-- excel文件的解析 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
实体类:
public class Student {
private String name;
private String gender;
private long phone;
private String profession;
private String grade;
private String birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student [name=" + name + ", gender=" + gender + ", phone=" + phone + ", profession=" + profession
+ ", grade=" + grade + ", birthday=" + birthday + "]";
}
}
controller解析student.xlsx文件:
@RequestMapping("/impPriceRecord")
public void impPriceRecord() throws Exception{
FileInputStream iStream = new FileInputStream(new File("C:/imagF/student.xlsx"));
XSSFWorkbook bWorkbook =new XSSFWorkbook(iStream);
XSSFSheet sheet = bWorkbook.getSheetAt(0);
for(int i =2;i<sheet.getLastRowNum();i++){
Student student =new Student();
XSSFRow xssfRow = sheet.getRow(i);
//获取最大的的列
int maxCell = xssfRow.getLastCellNum();
//获取最小的列
int minCell = xssfRow.getFirstCellNum();
//获取行的第一列内容
String name =xssfRow.getCell(0).getStringCellValue();
System.out.println("name="+name);
//获取行的第二列内容
String gender = xssfRow.getCell(1).getStringCellValue();
//获取行的第三列内容
long phone = Long.valueOf((long) xssfRow.getCell(2).getNumericCellValue()) ;
//获取行的第四列内容
String profession = xssfRow.getCell(3).getStringCellValue();
//获取行的第五列内容
String grade = xssfRow.getCell(4).getStringCellValue();
//获取行的第六列内容
XSSFCell birthday = xssfRow.getCell(5);
Date datebirthday = null;
SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd");
if(birthday != null){
datebirthday = birthday.getDateCellValue();
//设置出生日期
student.setBirthday(dateFormat.format(datebirthday));
}
System.out.println("datebirthday = "+datebirthday);
//将获取到的值设置给实体对象
student.setName(name);
student.setGender(gender);
student.setPhone(phone);
student.setProfession(profession);
student.setGrade(grade);
System.out.println("student="+student);
}
}
网友评论