java使用戶EasyExcel導入導出excel
- 2021 年 5 月 15 日
- 筆記
- JAVA, spring, springboot
使用alibab的EasyExce完成導入導出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>
<!-- esayexcel 2.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)
private Long id;
3、@ColumnWith註解
設置列寬度,只有一個參數value,value的單位是字元長度,最大可以設置255個字元,因為一個excel單元格最大可以寫入的字元個數就是255個字元
public class ImeiEncrypt {
@ColumnWidth(value = 255) //excel單個單元格最大長度255
private String message;
}
4、@ContentFontStyle註解
用於設置單元格內容字體格式的註解
參數 | 含義 |
---|---|
fontName | 字體名稱 |
fontHeightInPoints | 字體高度 |
italic | 是否斜體 |
strikeout | 是否設置刪除水平線 |
color | 字體顏色 |
typeOffset | 偏移量 |
underline | 下劃線 |
bold | 是否加粗 |
charset | 編碼格式 |
5、@ContentStyle註解
設置內容格式註解
參數 | 含義 |
---|---|
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 | 設置自動單元格自動大小 |
6、@HeadFontStyle註解
用於訂製標題字體格式
參數 | 含義 |
---|---|
fontName | 設置字體名稱 |
fontHeightInPoints | 設置字體高度 |
italic | 設置字體是否斜體 |
strikeout | 是否設置刪除線 |
color | 設置字體顏色 |
typeOffset | 設置偏移量 |
underline | 設置下劃線 |
charset | 設置字體編碼 |
bold | 設置字體是否加粗 |
7、ExcelIgnore註解
不將該欄位轉換成Excel
三、編碼
1、映射實體類—-例子
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;
}
以上是簡單的舉例,如果有更多屬性自己逐個寫就好,然後塞進該實體類就好~
2、生成excel
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();
}
}
以上,就生成完畢了!
四、結果
豁~搞定