1. 程式人生 > 其它 >用java實現Excel匯入匯出(easyExcel)

用java實現Excel匯入匯出(easyExcel)

前幾個月系統主要功能完結撒花,只剩下後臺系統的報表。為滿足客戶的需求,我需要將使用者給的excel表資料匯入到系統中,開始我沒有怎麼關注的這個業務,我卻花費了很久很久的時間。客戶的需求一直在變,excel表格也一直在變。我原本使用的是poi,從一開始的莫名其妙的報錯,資料型別的轉換錯誤,到各種的異常抓取,我的程式碼也越來越長。這時候我尋找到了一個很方便且簡單的工具easyExcel

測試前準備

首先映入excel工具的maven依賴

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.0.1</version>
        </dependency>

這裡先準備下實體物件方便後面操作,這裡寫兩個位元組碼去接受,是我自己的使用感受。匯出的資料是從系統裡來,我們能保持系統內的資料的正確性,但是要匯入的資料是來自使用者,我們不能保證其正確性,所以統一拿字串接受,這裡就不會存在數字等轉換異常了。

//匯出使用的實體
@Data
public class Demo {
    @ExcelProperty(value = "使用者名稱")
    private String username;
    @ExcelProperty(value = "密碼")
    private String password;
    @ExcelProperty(value = "年齡")
    private Integer age;
    @ExcelProperty(value = "性別")
    private String gender;
}

//匯入使用的實體
@Data
public class Demo {
    private String username;
    private String password;
    private String age;
    private String gender;
}

一、Excel匯入

//    普通匯入
    public static void main(String[] args) throws FileNotFoundException {
        List<Demo> ls = EasyExcel.read(new FileInputStream("./demo.xlsx"), Demo.class, new SyncReadListener()).sheet(0).doReadSync();
        for (Demo l : ls) {
            //操作資料
            System.out.println(l);
        }
    }

二、Excel匯出

1、普通匯出

    //普通匯出
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<Demo> ls = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Demo demo = new Demo();
            demo.setUsername("name"+i);
            demo.setPassword("password"+i);
            demo.setAge(i);
            demo.setGender((i%10==0)?"男":"女");

            ls.add(demo);
        }
        EasyExcel.write(new FileOutputStream("./demo.xlsx"),Demo.class).sheet(0).doWrite(ls);
    }

2、模板匯出

這裡注意以下操作後面的方法,withTemplate代表著使用模板,needHead代表是否需要表頭,useDefaultStyle意思為是否使用預設樣式,也就是灰底白字的樣式。

//    模板匯出
        public static void main(String[] args) throws FileNotFoundException {
        ArrayList<Demo> ls = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Demo demo = new Demo();
            demo.setUsername("name"+i);
            demo.setPassword("password"+i);
            demo.setAge(i);
            demo.setGender((i%10==0)?"男":"女");
            ls.add(demo);
        }
        EasyExcel.write(new FileOutputStream("./demo1.xlsx"),Demo.class).withTemplate("./demo.xlsx").needHead(false).useDefaultStyle(false).sheet(0).doWrite(ls);
    }

三、拓展提高

​ 這裡主要介紹下,通過該工具的其他註解,來操作excel的樣式!

@HeadRowHeight   //設定表頭高度
@ContentRowHeight  //設定內容高度
@ColumnWidth   //設定列寬
@HeadFontStyle  //設定表頭字型樣式
@HeadStyle    //設定表頭表格樣式
@ContentStyle    //設定內容表格樣式
@ContentFontStyle    //設定內容字型樣式

@DateTimeFormate  //時間格式轉換

跨行表頭如下

@Data
public class ComplexHeadData {
    @ExcelProperty({"主標題", "字串標題"})
    private String string;
    @ExcelProperty({"主標題", "日期標題"})
    private Date date;
    @ExcelProperty({"主標題", "數字標題"})
    private Double doubleData;
}

//顯示類似於
//    |             主標題              |
//    |--------------------------------|
//    |字串標題 |  日期標題  |  數字標題  |  

以上功能相比能解決你遇到的許多的問題。如果不能的話,請點選官網參考瞭解吧。