<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>ooxml-schemas</artifactId> <version>1.4</version> </dependency>
excel数据:
姓名 | 性别 | 年龄 | 出身年月 | ||
张三 | 男 | 12 | 1991/2/6 | ||
李四 | 男 | 13 | 1991/5/6 | ||
Java对象
package com.example.bootredis.pojo;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private String age;
private String sex;
private String birth;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", sex='" + sex + '\'' +
", birth='" + birth + '\'' +
'}';
}
}
转换exce文件转换成User对象代码
package com.example.bootredis.test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.bootredis.pojo.User;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelImport {
public static void main(String args[]) {
ExcelImport excelImport = new ExcelImport();
try {
excelImport.importExcelAction();
} catch (Exception e) {
e.printStackTrace();
}
}
//导入Excel数据
public void importExcelAction() throws Exception {
//文件路径
String filePath = "D:\\test.xlsx";
XSSFWorkbook wookbook = new XSSFWorkbook(new FileInputStream(filePath));
XSSFSheet sheet = wookbook.getSheet("Sheet1");
//获取到Excel文件中的所有行数
int rows = sheet.getPhysicalNumberOfRows();
//遍历行
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 1; i < rows; i++) {
// 读取左上端单元格
XSSFRow row = sheet.getRow(i);
// 行不为空
if (row != null) {
Map<String, Object> map = new HashMap<String, Object>();
//获取到Excel文件中的所有的列
int cells = row.getPhysicalNumberOfCells();
//姓名
XSSFCell nameCell = row.getCell(1);
String name = getValue(nameCell);
//性别
XSSFCell sexCell = row.getCell(2);
String sex = getValue(sexCell);
//年龄
XSSFCell ageCell = row.getCell(3);
String age = getValue(ageCell);
//出生年月
XSSFCell birthCell = row.getCell(4);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String birth = simpleDateFormat.format(birthCell.getDateCellValue());
map.put("name", name);
map.put("sex", sex);
map.put("age", age);
map.put("birth", birth);
list.add(map);
}
}
JSONArray jsonArray = new JSONArray();
jsonArray.addAll(list);
List<User> users = jsonArray.toJavaList(User.class);
System.out.println("list = " + users.toString());
}
private String getValue(XSSFCell xSSFCell) {
if (null == xSSFCell) {
return "";
}
if (xSSFCell.getCellType() == xSSFCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(xSSFCell.getBooleanCellValue());
} else if (xSSFCell.getCellType() == xSSFCell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
return String.valueOf(xSSFCell.getNumericCellValue());
} else {
// 返回字符串类型的值
return String.valueOf(xSSFCell.getStringCellValue());
}
}
}
输出:
list = [User{name='张三', age='12.0', sex='男', birth='1991-02-06'}, User{name='李四', age='13.0', sex='男', birth='1991-05-06'}]