App下載

Java如何使用EasyExcel實現(xiàn)導(dǎo)入導(dǎo)出excel

猿友 2021-08-02 11:08:31 瀏覽數(shù) (2761)
反饋

一、準(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、常用注解

字段注解 類注解
@ColumnWith(列寬) @ColumnWidth(全局列寬)
@ExcelProperty(字段配置) @HeadFontStyle(頭樣式)
@HeadRowHeight(標(biāo)題高度)
@ContentFontStyle(內(nèi)容字體樣式)
@ContentRowHeight(內(nèi)容高度)

2、@ExcelProperty注解

必要的一個注解,注解中有三個參數(shù)value,index分別代表列明,列序號
value和index只能二選一,通常不用設(shè)置converter
1.value 通過標(biāo)題文本對應(yīng)
2.index 通過文本行號對應(yīng)

@ExcelProperty(value = "編號", index = 0)
private Long id;

3、@ColumnWith注解

設(shè)置列寬度,只有一個參數(shù)value,value的單位是字符長度,最大可以設(shè)置255個字符,因為一個excel單元格最大可以寫入的字符個數(shù)就是255個字符

public class ImeiEncrypt {
    @ColumnWidth(value = 255) //excel單個單元格最大長度255
    private String message;
}

4、@ContentFontStyle注解

用于設(shè)置單元格內(nèi)容字體格式的注解

參數(shù) 含義
fontName 字體名稱
fontHeightInPoints 字體高度
italic 是否斜體
strikeout 是否設(shè)置刪除水平線
color 字體顏色
typeOffset 偏移量
underline 下劃線
bold 是否加粗
charset 編碼格式

5、@ContentStyle注解

設(shè)置內(nèi)容格式注解

參數(shù) 含義
dataFormat 日期格式
hidden 設(shè)置單元格使用此樣式隱藏
locked 設(shè)置單元格使用此樣式鎖定
quotePrefix 在單元格前面增加`符號,數(shù)字或公式將以字符串形式展示
horizontalAlignment 設(shè)置是否水平居中
wrapped 設(shè)置文本是否應(yīng)換行。將此標(biāo)志設(shè)置為true通過在多行上顯示使單元格中的所有內(nèi)容可見
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è)置填充類型
fillBackgroundColor 設(shè)置背景色
shrinkToFit 設(shè)置自動單元格自動大小

6、@HeadFontStyle注解

用于定制標(biāo)題字體格式

參數(shù) 含義
fontName 設(shè)置字體名稱
fontHeightInPoints 設(shè)置字體高度
italic 設(shè)置字體是否斜體
strikeout 是否設(shè)置刪除線
color 設(shè)置字體顏色
typeOffset 設(shè)置偏移量
underline 設(shè)置下劃線
charset 設(shè)置字體編碼
bold 設(shè)置字體是否加粗

7、ExcelIgnore注解

不將該字段轉(zhuǎn)換成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)    //標(biāo)題高度
@ColumnWidth(40)
public class OrderExcel {
    //設(shè)置excel表頭名稱
    @ExcelProperty(value = "編號", index = 0)
    private Long id;
    @DateTimeFormat("yyyy年MM月dd日HH時mm分ss秒")
    @ExcelProperty(value = "創(chuàng)建時間", index = 1)
    private Date createTime;
}

以上是簡單的舉例,如果有更多屬性自己逐個寫就好,然后塞進(jìn)該實體類就好~

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";
        //實現(xiàn)excel寫的操作

        //1 設(shè)置寫入文件夾地址和excel文件名稱
        String filename = "/路徑" + name;
        JSONObject json = new JSONObject();
        try {
            // 2 調(diào)用easyexcel里面的方法實現(xiàn)寫操作
            // write方法兩個參數(shù):第一個參數(shù)文件路徑名稱,第二個參數(shù)實體類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 來實現(xiàn)導(dǎo)入導(dǎo)出 excel 的詳情內(nèi)容,想要了解更多關(guān)于 Java 使用 EasyExcel 具體內(nèi)容的資料,請關(guān)注W3Cschool其它相關(guān)技術(shù)文章!也希望大家能夠多多支持!

0 人點贊