<!-- 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> <!-- esayexcel 2.1.7 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.1.7</version> </dependency>
必要的一个注解,注解中有三个参数value,index分别代表列明,列序号
value和index只能二选一,通常不用设置converter
1.value 通过标题文本对应
2.index 通过文本行号对应
@ExcelProperty(value = "编号", index = 0) private Long id;
设置列宽度,只有一个参数value,value的单位是字符长度,最大可以设置255个字符,因为一个excel单元格最大可以写入的字符个数就是255个字符
public class ImeiEncrypt { @ColumnWidth(value = 255) //excel单个单元格最大长度255 private String message; }
用于设置单元格内容字体格式的注解
设置内容格式注解
用于定制标题字体格式
不将该字段转换成Excel
package com.pingou.admin.bean.param; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.annotation.write.style.HeadRowHeight; import lombok.Data; import java.math.BigDecimal; import java.util.Date; @Data @ContentRowHeight(35) //文本行高度 @HeadRowHeight(40) //标题高度 @ColumnWidth(40) public class OrderExcel { //设置excel表头名称 @ExcelProperty(value = "编号", index = 0) private Long id; @DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒") @ExcelProperty(value = "创建时间", index = 1) private Date createTime; }
以上是简单的举例,如果有更多属性自己逐个写就好,然后塞进该实体类就好~
public void excel() { //欲导出excel的数据结果集 List<OrderExcel> excel = new ArrayList<>(); //省略 向结果集里插入数据的操作 //UUID生成唯一name String name = UUID.randomUUID().toString().replaceAll("-", "") + ".xlsx"; //实现excel写的操作 //1 设置写入文件夹地址和excel文件名称 String filename = "/路径" + name; JSONObject json = new JSONObject(); try { // 2 调用easyexcel里面的方法实现写操作 // write方法两个参数:第一个参数文件路径名称,第二个参数实体类class EasyExcel.write(filename, OrderExcel.class).sheet("名字").doWrite(excel); //上传到fastdfs上 不上传的话只有本机可以找到,在上面路径下生成excel File file = new File(filename); String path = fastDFSClient.upload(new FileInputStream(file), name, null); path = (this.fastdfsDomain + path); json.put("url", path); } catch (IOException e) { e.printStackTrace(); } finally { new File(filename).delete(); } }
以上,就生成完毕了
以上就是java使用EasyExcel导入导出excel的详细内容,更多关于java 用EasyExcel导入导出excel的资料请关注脚本之家其它相关文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、来自互联网转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系QQ:712375056 进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
Copyright © 2009-2021 56dr.com. All Rights Reserved. 特网科技 特网云 版权所有 珠海市特网科技有限公司 粤ICP备16109289号
域名注册服务机构:阿里云计算有限公司(万网) 域名服务机构:烟台帝思普网络科技有限公司(DNSPod) CDN服务:阿里云计算有限公司 中国互联网举报中心 增值电信业务经营许可证B2
建议您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流浏览器浏览本网站