java使用EasyExcel導(dǎo)入導(dǎo)出excel
一、準(zhǔn)備工作
1、導(dǎo)包
<!-- poi 相關(guān)--> <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、常用注解
字段注解 | 類(lèi)注解 |
---|---|
@ColumnWith(列寬) | @ColumnWidth(全局列寬) |
@ExcelProperty(字段配置) | @HeadFontStyle(頭樣式) |
@HeadRowHeight(標(biāo)題高度) | |
@ContentFontStyle(內(nèi)容字體樣式) | |
@ContentRowHeight(內(nèi)容高度) |
2、@ExcelProperty注解
必要的一個(gè)注解,注解中有三個(gè)參數(shù)value,index分別代表列明,列序號(hào)
value和index只能二選一,通常不用設(shè)置converter
1.value 通過(guò)標(biāo)題文本對(duì)應(yīng)
2.index 通過(guò)文本行號(hào)對(duì)應(yīng)
@ExcelProperty(value = "編號(hào)", index = 0) private Long id;
3、@ColumnWith注解
設(shè)置列寬度,只有一個(gè)參數(shù)value,value的單位是字符長(zhǎng)度,最大可以設(shè)置255個(gè)字符,因?yàn)橐粋€(gè)excel單元格最大可以寫(xiě)入的字符個(gè)數(shù)就是255個(gè)字符
public class ImeiEncrypt { @ColumnWidth(value = 255) //excel單個(gè)單元格最大長(zhǎng)度255 private String message; }
4、@ContentFontStyle注解
用于設(shè)置單元格內(nèi)容字體格式的注解
參數(shù) | 含義 |
---|---|
fontName | 字體名稱(chēng) |
fontHeightInPoints | 字體高度 |
italic | 是否斜體 |
strikeout | 是否設(shè)置刪除水平線 |
color | 字體顏色 |
typeOffset | 偏移量 |
underline | 下劃線 |
bold | 是否加粗 |
charset | 編碼格式 |
5、@ContentStyle注解
設(shè)置內(nèi)容格式注解
參數(shù) | 含義 |
---|---|
dataFormat | 日期格式 |
hidden | 設(shè)置單元格使用此樣式隱藏 |
locked | 設(shè)置單元格使用此樣式鎖定 |
quotePrefix | 在單元格前面增加`符號(hào),數(shù)字或公式將以字符串形式展示 |
horizontalAlignment | 設(shè)置是否水平居中 |
wrapped | 設(shè)置文本是否應(yīng)換行。將此標(biāo)志設(shè)置為true通過(guò)在多行上顯示使單元格中的所有內(nèi)容可見(jiàn) |
verticalAlignment | 設(shè)置是否垂直居中 |
rotation | 設(shè)置單元格中文本旋轉(zhuǎn)角度。03版本的Excel旋轉(zhuǎn)角度區(qū)間為-90°90°,07版本的Excel旋轉(zhuǎn)角度區(qū)間為0°180° |
indent | 設(shè)置單元格中縮進(jìn)文本的空格數(shù) |
borderLeft | 設(shè)置左邊框的樣式 |
borderRight | 設(shè)置右邊框樣式 |
borderTop | 設(shè)置上邊框樣式 |
leftBorderColor | 設(shè)置左邊框顏色 |
rightBorderColor | 設(shè)置右邊框顏色 |
topBorderColor | 設(shè)置上邊框顏色 |
bottomBorderColor | 設(shè)置下邊框顏色 |
fillPatternType | 設(shè)置填充類(lèi)型 |
fillBackgroundColor | 設(shè)置背景色 |
shrinkToFit | 設(shè)置自動(dòng)單元格自動(dòng)大小 |
6、@HeadFontStyle注解
用于定制標(biāo)題字體格式
參數(shù) | 含義 |
---|---|
fontName | 設(shè)置字體名稱(chēng) |
fontHeightInPoints | 設(shè)置字體高度 |
italic | 設(shè)置字體是否斜體 |
strikeout | 是否設(shè)置刪除線 |
color | 設(shè)置字體顏色 |
typeOffset | 設(shè)置偏移量 |
underline | 設(shè)置下劃線 |
charset | 設(shè)置字體編碼 |
bold | 設(shè)置字體是否加粗 |
7、ExcelIgnore注解
不將該字段轉(zhuǎn)換成Excel
三、編碼
1、映射實(shí)體類(lèi)----例子
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) //標(biāo)題高度 @ColumnWidth(40) public class OrderExcel { //設(shè)置excel表頭名稱(chēng) @ExcelProperty(value = "編號(hào)", index = 0) private Long id; @DateTimeFormat("yyyy年MM月dd日HH時(shí)mm分ss秒") @ExcelProperty(value = "創(chuàng)建時(shí)間", index = 1) private Date createTime; }
以上是簡(jiǎn)單的舉例,如果有更多屬性自己逐個(gè)寫(xiě)就好,然后塞進(jìn)該實(shí)體類(lèi)就好~
2、生成excel
public void excel() { //欲導(dǎo)出excel的數(shù)據(jù)結(jié)果集 List<OrderExcel> excel = new ArrayList<>(); //省略 向結(jié)果集里插入數(shù)據(jù)的操作 //UUID生成唯一name String name = UUID.randomUUID().toString().replaceAll("-", "") + ".xlsx"; //實(shí)現(xiàn)excel寫(xiě)的操作 //1 設(shè)置寫(xiě)入文件夾地址和excel文件名稱(chēng) String filename = "/路徑" + name; JSONObject json = new JSONObject(); try { // 2 調(diào)用easyexcel里面的方法實(shí)現(xiàn)寫(xiě)操作 // write方法兩個(gè)參數(shù):第一個(gè)參數(shù)文件路徑名稱(chēng),第二個(gè)參數(shù)實(shí)體類(lèi)class EasyExcel.write(filename, OrderExcel.class).sheet("名字").doWrite(excel); //上傳到fastdfs上 不上傳的話只有本機(jī)可以找到,在上面路徑下生成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(); } }
以上,就生成完畢了
四、結(jié)果
以上就是java使用EasyExcel導(dǎo)入導(dǎo)出excel的詳細(xì)內(nèi)容,更多關(guān)于java 用EasyExcel導(dǎo)入導(dǎo)出excel的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot中EasyExcel實(shí)現(xiàn)Excel文件的導(dǎo)入導(dǎo)出
- SpringBoot集成EasyExcel實(shí)現(xiàn)Excel導(dǎo)入的方法
- SpringBoot整合EasyExcel實(shí)現(xiàn)文件導(dǎo)入導(dǎo)出
- Java+EasyExcel實(shí)現(xiàn)文件的導(dǎo)入導(dǎo)出
- EasyExcel實(shí)現(xiàn)導(dǎo)入+各種數(shù)據(jù)校驗(yàn)功能
- Java使用EasyExcel實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出
- Java使用easyExcel批量導(dǎo)入數(shù)據(jù)詳解
- Java EasyExcel導(dǎo)入帶圖片的完整過(guò)程記錄
相關(guān)文章
Java class文件格式之特殊字符串_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
特殊字符串出現(xiàn)在class文件中的常量池中,本著循序漸進(jìn)和減少跨度的原則, 首先把class文件中的特殊字符串做一個(gè)詳細(xì)的介紹, 然后再回過(guò)頭來(lái)繼續(xù)講解常量池,對(duì)java class 文件格式相關(guān)知識(shí)感興趣的的朋友一起學(xué)習(xí)吧2017-06-06Java的StringBuilder在高性能場(chǎng)景下的正確用法
StringBuilder?對(duì)字符串的操作是直接改變字符串對(duì)象本身,而不是生成新的對(duì)象,所以新能開(kāi)銷(xiāo)小.與StringBuffer相比StringBuilder的性能略高,StringBuilder則沒(méi)有保證線程的安全,從而性能略高于StringBuffer,需要的朋友可以參考下2023-05-05Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換
這篇文章主要給大家介紹了關(guān)于Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03SpringBoot3中Spring?WebFlux?SSE服務(wù)器發(fā)送事件的實(shí)現(xiàn)步驟
本文介紹了如何使用SpringBoot3和響應(yīng)式編程實(shí)現(xiàn)服務(wù)器發(fā)送事件(SSE),并討論了其在實(shí)時(shí)數(shù)據(jù)推送場(chǎng)景中的優(yōu)勢(shì),通過(guò)示例代碼,展示了如何創(chuàng)建SSE控制器、客戶端接收數(shù)據(jù)以及優(yōu)化與擴(kuò)展,感興趣的朋友跟隨小編一起看看吧2024-11-11JAVA中實(shí)現(xiàn)鏈?zhǔn)讲僮鳎ǚ椒ㄦ湥┑暮?jiǎn)單例子
這篇文章主要介紹了JAVA中實(shí)現(xiàn)鏈?zhǔn)讲僮鞯睦?模仿jQuery的方法鏈實(shí)現(xiàn),需要的朋友可以參考下2014-04-04servlet監(jiān)聽(tīng)實(shí)現(xiàn)統(tǒng)計(jì)在線人數(shù)功能 附源碼下載
這篇文章主要為大家詳細(xì)介紹了servlet監(jiān)聽(tīng)統(tǒng)計(jì)在線人數(shù)的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Spring中的@EnableWebSecurity注解詳解
這篇文章主要介紹了Spring中的@EnableWebSecurity注解詳解,EnableWebSecurity注解是個(gè)組合注解,它的注解中,又使用了@EnableGlobalAuthentication注解,需要的朋友可以參考下2023-12-12Java8?LocalDateTime時(shí)間日期類(lèi)使用實(shí)例詳解
本文從 LocalDateTime 類(lèi)的創(chuàng)建、轉(zhuǎn)換、格式化與解析、計(jì)算與比較以及其他操作幾個(gè)方面詳細(xì)介紹了 LocalDateTime 類(lèi)在 Java 8 中的使用,感興趣的朋友跟隨小編一起看看吧2024-03-03