Java怎么使用EasyExcel实现导入导出excel
这篇文章主要介绍了Java怎么使用EasyExcel实现导入导出excel,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
一、准备工作1、导包
<!--poi相关--><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version></dependency><!--esayexcel2.1.7--><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.1.7</version></dependency>二、了解注解
1、常用注解字段注解 类注解 @ColumnWith(列宽)@ColumnWidth(全局列宽)@ExcelProperty(字段配置)@HeadFontStyle(头样式)
@HeadRowHeight(标题高度)
@ContentFontStyle(内容字体样式)
@ContentRowHeight(内容高度)2、@ExcelProperty注解
必要的一个注解,注解中有三个参数value,index分别代表列明,列序号
value和index只能二选一,通常不用设置converter
1.value 通过标题文本对应
2.index 通过文本行号对应
@ExcelProperty(value="编号",index=0)privateLongid;3、@ColumnWith注解
设置列宽度,只有一个参数value,value的单位是字符长度,最大可以设置255个字符,因为一个excel单元格最大可以写入的字符个数就是255个字符
publicclassImeiEncrypt{@ColumnWidth(value=255)//excel单个单元格最大长度255privateStringmessage;}4、@ContentFontStyle注解
用于设置单元格内容字体格式的注解
设置内容格式注解
用于定制标题字体格式
三、编码不将该字段转换成Excel
1、映射实体类----例子
packagecom.pingou.admin.bean.param;importcom.alibaba.excel.annotation.ExcelProperty;importcom.alibaba.excel.annotation.format.DateTimeFormat;importcom.alibaba.excel.annotation.write.style.ColumnWidth;importcom.alibaba.excel.annotation.write.style.ContentRowHeight;importcom.alibaba.excel.annotation.write.style.HeadRowHeight;importlombok.Data;importjava.math.BigDecimal;importjava.util.Date;@Data@ContentRowHeight(35)//文本行高度@HeadRowHeight(40)//标题高度@ColumnWidth(40)publicclassOrderExcel{//设置excel表头名称@ExcelProperty(value="编号",index=0)privateLongid;@DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒")@ExcelProperty(value="创建时间",index=1)privateDatecreateTime;}
以上是简单的举例,如果有更多属性自己逐个写就好,然后塞进该实体类就好~
2、生成excelpublicvoidexcel(){//欲导出excel的数据结果集List<OrderExcel>excel=newArrayList<>();//省略向结果集里插入数据的操作//UUID生成唯一nameStringname=UUID.randomUUID().toString().replaceAll("-","")+".xlsx";//实现excel写的操作//1设置写入文件夹地址和excel文件名称Stringfilename="/路径"+name;JSONObjectjson=newJSONObject();try{//2调用easyexcel里面的方法实现写操作//write方法两个参数:第一个参数文件路径名称,第二个参数实体类classEasyExcel.write(filename,OrderExcel.class).sheet("名字").doWrite(excel);//上传到fastdfs上不上传的话只有本机可以找到,在上面路径下生成excelFilefile=newFile(filename);Stringpath=fastDFSClient.upload(newFileInputStream(file),name,null);path=(this.fastdfsDomain+path);json.put("url",path);}catch(IOExceptione){e.printStackTrace();}finally{newFile(filename).delete();}}
感谢你能够认真阅读完这篇文章,希望小编分享的“Java怎么使用EasyExcel实现导入导出excel”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。