1、简单读取,没有合并单元格
2、复杂读取,合并单元格-方法一
1、简单读取,没有合并单元格
1.1、引入pom文件
javascript
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
1.2、Excel读取
1.3、测试代码
javascript
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@Data
public class userDTo {
@ExcelProperty(value = "姓名")
private String name;
@ExcelProperty(value = "年龄")
private String age;
/**
* 项目名称
*/
@ExcelProperty(value = "工号")
private String workNo;
@ExcelProperty(value = "部门")
private String dept;
}
javascript
@Test
void contextLoads() {
File file = new File("D:\\maven_workplace\\BootProject_test\\boot-project-test\\EasyExcel-test\\src\\main\\resources\\测试.xls");
Class<userDTo> userDToClass = userDTo.class;
EasyExcel.read(file, userDToClass, new AnalysisEventListener<userDTo>() {
@Override
public void invoke(userDTo userDTo, AnalysisContext analysisContext) {
//一条一条数据进解析
System.out.println("数据解析---->"+userDTo);
}
//所有的数据都解析完之后执行
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}).sheet().headRowNumber(1).doRead();
}
// sheet 从0 开始(默认)
//headRowNumber 标题
1.4、结果
2、复杂读取,合并单元格 -数据嵌套
2.1、读取Excel格式
2.2、测试代码
javascript
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.easyexceltest.pojo.userDTo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.File;
@SpringBootTest
class EasyExcelTestApplicationTests {
@Test
void contextLoads() {
File file = new File("D:\\maven_workplace\\BootProject_test\\boot-project-test\\EasyExcel-test\\src\\main\\resources\\测试.xls");
Class<userDTo> userDToClass = userDTo.class;
EasyExcel.read(file, userDToClass, new AnalysisEventListener<userDTo>() {
@Override
public void invoke(userDTo userDTo, AnalysisContext analysisContext) {
//一条一条数据进解析
System.out.println("数据解析---->"+userDTo);
}
//所有的数据都解析完之后执行
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("数据解析结束");
}
}).sheet().headRowNumber(1).doRead();
}
}
2.3、测试结果
发现使用刚刚的方法已经,合并之后并不能够正常读取数据。只能读合并之后 第一行
数据。
解决办法:
1、实体类:
javascript
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import java.util.List;
import java.util.Map;
@Data
public class userDTo {
@ExcelProperty(value = "姓名")
private String name;
@ExcelProperty(value = "年龄")
private String age;
/**
* 项目名称
*/
@ExcelProperty(value = "工号")
private String workNo;
// 把 要从Excel 读取的字段都要写,即使部门要封装到list中
@ExcelProperty(value = "部门")
private String dept;
// 忽略该字段
@ExcelIgnore
private List<Map<String,String>> deptMap;
@ExcelIgnore
private List<Dept> deptList;
}
javascript
@Data
public class Dept {
private String dept;
}
2、测试代码:
javascript
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.easyexceltest.pojo.Dept;
import com.example.easyexceltest.pojo.userDTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
@Slf4j
class EasyExcelTestApplicationTests {
@Test
void contextLoads() {
File file = new File("D:\\maven_workplace\\BootProject_test\\boot-project-test\\EasyExcel-test\\src\\main\\resources\\测试.xls");
Class<userDTo> userDToClass = userDTo.class;
List<userDTo> deptsInfo = new ArrayList<>();
EasyExcel.read(file, userDToClass, new AnalysisEventListener<userDTo>() {
@Override
public void invoke(userDTo userDTo, AnalysisContext analysisContext) {
//一条一条数据进解析
System.out.println("数据解析---->"+userDTo);
deptsInfo.add(userDTo);
}
//所有的数据都解析完之后执行
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("数据解析结束");
importCustomer(deptsInfo);
}
}).sheet().headRowNumber(1).doRead();
}
public void importCustomer(List<userDTo> list) {
// 待保存到数据库的数据
List<userDTo> data = new ArrayList<>();
userDTo customer = new userDTo();
List<Map<String, String>> contactsList = new ArrayList<>();
for (userDTo obj : list) {
// 判断用户名是否为空,
if (StringUtils.isNotBlank(obj.getName())) {
//信息列表是否有数据,如果有则要把数据保存起来
if (contactsList.size() != 0) {
customer.setDeptMap(contactsList);
data.add(customer);
}
//重置对象
customer = new userDTo();
contactsList = new ArrayList<>();
customer.setName(obj.getName());
customer.setAge(obj.getAge());
customer.setWorkNo(obj.getWorkNo());
//一个list嵌套写一个map,如果有两个需要写两个map
Map map = new HashMap();
map.put("dept", obj.getDept());
contactsList.add(map);
} else {
Map map = new HashMap();
map.put("dept", obj.getDept());
contactsList.add(map);
}
}
// 保存最后一条数据
if (contactsList.size() != 0) {
customer.setDeptMap(contactsList);
data.add(customer);
}
System.out.println(data.toString());
System.out.println("----------------==");
for (userDTo user : data) {
List<Map<String, String>> issrInfo = user.getDeptMap();
ArrayList<Dept> depts = new ArrayList<>();
for (Map<String, String> map : issrInfo) {
// map 转成 ArrIssExcelDto.class;
Dept dept = JSONObject.parseObject(JSON.toJSONString(map), Dept.class);
depts.add(dept);
}
System.out.println(">>>>>>>>>>>>>>>>>>>>将map数据封装到list中");
System.out.println(depts);
user.setDeptList(depts);
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>");
}
log.info("---读取data数据为:{}" , JSONObject.toJSONString(data));
}
}
3、结果: