这篇文章主要介绍了java如何使用Easyexcel导入导出excel,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。一、准备工作1、导包<!-- poi&nb
这篇文章主要介绍了java如何使用Easyexcel导入导出excel,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
<!-- 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>
字段注解 | 类注解 |
---|---|
@ColumnWith(列宽) | @ColumnWidth(全局列宽) |
@ExcelProperty(字段配置) | @HeadFontStyle(头样式) |
@HeadRowHeight(标题高度) | |
@ContentFontStyle(内容字体样式) | |
@ContentRowHeight(内容高度) |
必要的一个注解,注解中有三个参数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;}
用于设置单元格内容字体格式的注解
参数 | 含义 |
---|---|
fontName | 字体名称 |
fontHeightInPoints | 字体高度 |
italic | 是否斜体 |
strikeout | 是否设置删除水平线 |
color | 字体颜色 |
typeOffset | 偏移量 |
underline | 下划线 |
bold | 是否加粗 |
charset | 编码格式 |
设置内容格式注解
参数 | 含义 |
---|---|
dataFORMat | 日期格式 |
hidden | 设置单元格使用此样式隐藏 |
locked | 设置单元格使用此样式锁定 |
quotePrefix | 在单元格前面增加`符号,数字或公式将以字符串形式展示 |
horizontalAlignment | 设置是否水平居中 |
wrapped | 设置文本是否应换行。将此标志设置为true通过在多行上显示使单元格中的所有内容可见 |
verticalAlignment | 设置是否垂直居中 |
rotation | 设置单元格中文本旋转角度。03版本的Excel旋转角度区间为-90°90°,07版本的Excel旋转角度区间为0°180° |
indent | 设置单元格中缩进文本的空格数 |
borderLeft | 设置左边框的样式 |
borderRight | 设置右边框样式 |
borderTop | 设置上边框样式 |
leftBorderColor | 设置左边框颜色 |
rightBorderColor | 设置右边框颜色 |
topBorderColor | 设置上边框颜色 |
bottomBorderColor | 设置下边框颜色 |
fillPatternType | 设置填充类型 |
fillBackgroundColor | 设置背景色 |
shrinkToFit | 设置自动单元格自动大小 |
用于定制标题字体格式
参数 | 含义 |
---|---|
fontName | 设置字体名称 |
fontHeightInPoints | 设置字体高度 |
italic | 设置字体是否斜体 |
strikeout | 是否设置删除线 |
color | 设置字体颜色 |
typeOffset | 设置偏移量 |
underline | 设置下划线 |
charset | 设置字体编码 |
bold | 设置字体是否加粗 |
不将该字段转换成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的特点有哪些1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。2.Java具有简单性、面向对象、分布式、安全性、平台独立与可移植性、动态性等特点。3.使用Java可以编写桌面应用程序、WEB应用程序、分布式系统和嵌入式系统应用程序等。
感谢你能够认真阅读完这篇文章,希望小编分享的“java如何使用EasyExcel导入导出excel”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网精选频道,更多相关知识等着你来学习!
--结束END--
本文标题: java如何使用EasyExcel导入导出excel
本文链接: https://www.lsjlt.com/news/277585.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0