EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel。
官方doc地址:https://www.yuque.com/easyexcel/doc/easyexcel
EasyExcel优势
注解式自定义操作。
输入输出简单,提供输入输出过程的接口
支持一定程度的单元格合并等灵活化操作
@ExcelProperty
指定当前字段对应excel中的那一列。可以根据名字或者Index去匹配。当然也可以不写,默认第一个字段就是index=0,以此类推。千万注意,要么全部不写,要么全部用index,要么全部用名字去匹配。千万别三个混着用,除非你非常了解源代码中三个混着用怎么去排序的。
@ExcelIgnore
默认所有字段都会和excel去匹配,加了这个注解会忽略该字段
@DateTimeFormat
日期转换,用String去接收excel日期格式的数据会调用这个注解。里面的value参照java.text.SimpleDateFormat
@NumberFormat
数字转换,用String去接收excel数字格式的数据会调用这个注解。里面的value参照java.text.DecimalFormat
@ExcelIgnoreUnannotated
默认不加ExcelProperty 的注解的都会参与读写,加了不会参与
com.alibaba
easyexcel
2.1.4
javax.servlet
javax.servlet-api
4.0.1
provided
com.alibaba
fastjson
1.2.47
/**
* EasyExcel 导入监听
*/
public class ExcelListener extends AnalysisEventListener {
//可以通过实例获取该值
private List
try {
//获取文件名
String filename = file.getOriginalFilename();
//获取文件流
InputStream inputStream = file.getInputStream();
//实例化实现了AnalysisEventListener接口的类
ExcelListener listener = new ExcelListener();
//传入参数
ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, listener);
//读取信息
excelReader.read(new Sheet(1, 0, Test.class));
//获取数据
List list = listener.getDatas();
if (list.size() > 1) {
for (int i = 0; i < list.size(); i++) {
Testobj = (Test) list.get(i);
JSONObject jo = new JSONObject();
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
HttpServletResponse response, HttpServletRequest request
try {
String filenames = "test";
String userAgent = request.getHeader("User-Agent");
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
filenames = URLEncoder.encode(filenames, "UTF-8");
} else {
filenames = new String(filenames.getBytes("UTF-8"), "ISO-8859-1");
}
response.setContentType("application/vnd.ms-exce");
response.setCharacterEncoding("utf-8");
response.addHeader("Content-Disposition", "filename=" + filenames + ".xlsx");
EasyExcel.write(response.getOutputStream(), Test.class).sheet("sheet").doWrite(testList);
} catch (Exception e) {
}
List testList = new ArrayList<>();
try {
String strUrl = "C:\\Users\\Administrator\\Desktop\\json2.xlsx";
File multipartFile = new File(strUrl);
InputStream inputStream = new FileInputStream(multipartFile);
//实例化实现了AnalysisEventListener接口的类
ExcelListener listener = new ExcelListener();
//传入参数
ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, listener);
//读取信息
excelReader.read(new Sheet(1, 0, Test.class));
//获取数据
List list = listener.getDatas();
if (list.size() > 1) {
for (int i = 0; i < list.size(); i++) {
Testobj = (Test) list.get(i);
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
try {
String strUrl = "C:\\Users\\Administrator\\Desktop\\json2"+System.currentTimeMillis()+".xlsx";
EasyExcel.write(strUrl,Test.class).sheet("sheet").doWrite(testList);
} catch (Exception e) {
}
以上就是EasyExcel的基础使用过程,欢迎点赞关注交流。
点击下方卡片/微信搜索,关注公众号“天宇文创意乐派”(ID:gh_cc865e4c536b)
听说点赞和关注本号的都找到漂亮的小姐姐了哟且年后必入百万呀!!
往期推荐
[
巨好看的漫画变身AI!快来看看自己的漫画脸长啥样
](http://mp.weixin.qq.com/s?__biz=MzI4MDQ5MTUzMg==&mid=2247488377&idx=3&sn=8cef6a61bbc117fe2270ec248c9f18f6&chksm=ebb6f01adcc1790c8170d73e201d084bc0477a7d3f4035f3ee2fc5be27707d144e8ff58a5333&scene=21#wechat_redirect)
[
Detox 移动端 APP 自动化测试框架
](http://mp.weixin.qq.com/s?__biz=MzI4MDQ5MTUzMg==&mid=2247488377&idx=5&sn=1a3460a4bd8c1b5d58c540c697f716bb&chksm=ebb6f01adcc1790c2afd9c4a4deb2de97ab0e656741d20effb23b323e7b56c9ec3af0c6dfdec&scene=21#wechat_redirect)
[
开源工具,又是免费的神器 ,有点意思!
](http://mp.weixin.qq.com/s?__biz=MzI4MDQ5MTUzMg==&mid=2247488268&idx=1&sn=38c89310e4c4090bbda12fd4478e0e96&chksm=ebb6f06fdcc17979ce5623d8b758dbd341ca34a80cebaa80015e5b5da572b22e6ae2051d2b4f&scene=21#wechat_redirect)
[
再见 Swagger UI!国人开源了一款超好用的 API 文档生成框架,Star 4.7K+,真香!!
](http://mp.weixin.qq.com/s?__biz=MzI4MDQ5MTUzMg==&mid=2247488219&idx=2&sn=36e5232ac6a75c2df040fec508bbefe0&chksm=ebb6f1b8dcc178aee371f4325cbb7f6493173d14fef22f9b4e6a741914c264a19715c59b26a1&scene=21#wechat_redirect)
[
@Transactional 错误集锦以及如何正确使用
](http://mp.weixin.qq.com/s?__biz=MzI4MDQ5MTUzMg==&mid=2247487967&idx=1&sn=29723aa49e0fd311c2f365126a04bc7b&chksm=ebb6f2bcdcc17baae1e0b08775ba25d3db134d9b0e67279319f13880817d765d6243a6541ed5&scene=21#wechat_redirect)