当前位置:网站首页>EasyExcel的读写操作
EasyExcel的读写操作
2022-07-05 21:05:00 【letg】
一、写操作
1.1 实体类
读操作添加 @ExcelProperty注解,并且value值为excel的表头名称
@Data
public class User {
@ExcelProperty(value = "用户编号",index = 0)
private int id;
@ExcelProperty(value = "用户名称",index = 1)
private String name;
}
1.2 测试写操作
public class TestWrite {
public static void main(String[] args) {
String fileName = "C:\\data\\hzxy.xlsx";
EasyExcel.write(fileName,User.class)
.sheet("写操作")
.doWrite(data());
}
}
private static List<User> data(){
ArrayList<User> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setId(i);
user.setName("张三"+i);
list.add(user);
}
return list;
}
}

二、写操作(写操作需要继承一个监听器,进行一行一行读取)
2.1 监听器
invoke方法会进行excel一行一行读取,只会从第二行读取,第一行为表头
public class ExcelListener extends AnalysisEventListener<User> {
//一行一行读取excel的内容,把每行内容封装
//第一行不读,因为第一行为表头
@Override
public void invoke(User user, AnalysisContext analysisContext) {
System.out.println(user);
}
//表头是从该方法读取
@Override
public void invokeHead(Map<Integer, CellData> headMap, AnalysisContext context) {
System.out.println("表头:"+headMap);
super.invokeHead(headMap, context);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
2.2 测试代码
public class TestWrite {
public static void main(String[] args) {
String fileName = "C:\\data\\hzxy.xlsx";
// EasyExcel.write(fileName,User.class)
// .sheet("写操作")
// .doWrite(data());
EasyExcel.read(fileName,User.class,new ExcelListener())
.sheet("读操作")
.doRead();
}

三、实现
3.1 导出
public void exportData(HttpServletResponse response) {
try {
//设置下载信息
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("课程分类","UTF-8");
response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");
//查询所有课程数据
List<Subject> subjectList = baseMapper.selectList(null);
List<SubjectEeVo> subjectEeVos = new ArrayList<>();
for (Subject subject : subjectList) {
SubjectEeVo subjectEeVo = new SubjectEeVo();
BeanUtils.copyProperties(subject,subjectEeVo);
subjectEeVos.add(subjectEeVo);
}
EasyExcel.write(response.getOutputStream(), SubjectEeVo.class)
.sheet("课程分类")
.doWrite(subjectEeVos);
} catch (Exception e) {
e.printStackTrace();
}
}
3.2 导入
@Override
public void importData(MultipartFile file) {
try {
EasyExcel.read(file.getInputStream(),SubjectEeVo.class,subjectListener)
.sheet()
.doRead();
} catch (IOException e) {
throw new GgktException(20001,"导入失败");
}
}
@Component
public class SubjectListener extends AnalysisEventListener<SubjectEeVo> {
@Autowired
private SubjectMapper subjectMapper;
@Override
public void invoke(SubjectEeVo subjectEeVo, AnalysisContext analysisContext) {
Subject subject = new Subject();
BeanUtils.copyProperties(subjectEeVo,subject);
subjectMapper.insert(subject);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
边栏推荐
- ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
- Is it necessary for bazel to learn
- Comparison table of foreign lead American abbreviations
- Clion configures Visual Studio (MSVC) and JOM multi-core compilation
- Web Service简单入门示例
- MySQL 千万数据量深分页优化, 拒绝线上故障!
- leetcode:1755. 最接近目标值的子序列和
- Sequence alignment
- Monorepo management methodology and dependency security
- Introduction to TS, constructor and its this, inheritance, abstract class and interface
猜你喜欢
![[case] Application of element display and hiding -- element mask](/img/6e/6ea484a6e5d547e01dd8820af8e314.png)
[case] Application of element display and hiding -- element mask

PVC plastic sheets BS 476-6 determination of flame propagation properties

EN 438-7建筑覆盖物装饰用层压板材产品—CE认证

Use of thread pool

R语言【数据管理】

Pytoch practice -- MNIST dataset handwritten digit recognition

Influence of oscilloscope probe on measurement bandwidth

Open source SPL eliminates tens of thousands of database intermediate tables

教你自己训练的pytorch模型转caffe(二)

中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
随机推荐
Talk about my fate with some programming languages
木板ISO 5660-1 热量释放速率摸底测试
MySQL ifnull usage function
请查收.NET MAUI 的最新学习资源
教你自己训练的pytorch模型转caffe(三)
第05章_存储引擎
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
Viewrootimpl and windowmanagerservice notes
Establishment of terminal security capability verification environment and penetration test records
Which securities company is better and which platform is safer for stock account opening
EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
Web Service简单入门示例
启牛2980有没有用?开户安全吗、
R language [data management]
Introduction to TS, constructor and its this, inheritance, abstract class and interface
Wood board ISO 5660-1 heat release rate mapping test
研學旅遊實踐教育的開展助力文旅產業發展
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
ClickHouse 复制粘贴多行sql语句报错
[case] Application of element display and hiding -- element mask