用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;
}

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

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM