Hutool Java工具類庫_ExcelUtil的使用

Hutool Java工具類庫_ExcelUtil

依賴

<!--Hutool Java工具包-->
    <dependency>
       <groupId>cn.hutool</groupId>
       <artifactId>hutool-all</artifactId>
       <version>5.4.5</version>
 </dependency>

ExcelUtil

從文件中讀取Excel為ExcelReader

ExcelReader reader = ExcelUtil.getReader(FileUtil.file("test.xlsx"));

從流中讀取Excel為ExcelReader

ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xlsx"));

讀取指定的sheet

ExcelReader reader;
//通過sheet編號獲取
reader = ExcelUtil.getReader(FileUtil.file("test.xlsx"), 0);
//通過sheet名獲取
reader = ExcelUtil.getReader(FileUtil.file("test.xlsx"), "sheet1");

ExcelReader

讀取Excel中所有行和列,都用列表表示

ExcelReader reader = ExcelUtil.getReader("d:/aaa.xlsx");
List<List<Object>> readAll = reader.read();

讀取為Map列表,默認第一行為標題行,Map中的key為標題,value為標題對應的單元格值

ExcelReader reader = ExcelUtil.getReader("d:/aaa.xlsx");
List<Map<String,Object>> readAll = reader.readAll();

讀取為Bean列表,Bean中的字段名為標題,字段值為標題對應的單元格值

ExcelReader reader = ExcelUtil.getReader("d:/aaa.xlsx");
List<Person> all = reader.readAll(Person.class);

ExcelWriter

Hutool將Excel寫出封裝為ExcelWriter,原理為包裝瞭Workbook對象,每次調用merge(合並單元格)或者write(寫出數據)方法後隻是將數據寫入到Workbook,並不寫出文件,隻有調用flush或者close方法後才會真正寫出文件。

由於機制原因,在寫出結束後需要關閉ExcelWriter對象,調用close方法即可關閉,此時才會釋放Workbook對象資源,否則帶有數據的Workbook一直會常駐內存

(1) 將行列對象寫出到Excel

List<String> row1 = CollUtil.newArrayList("aa", "bb", "cc", "dd");
List<String> row2 = CollUtil.newArrayList("aa1", "bb1", "cc1", "dd1");
List<String> row3 = CollUtil.newArrayList("aa2", "bb2", "cc2", "dd2");
List<String> row4 = CollUtil.newArrayList("aa3", "bb3", "cc3", "dd3");
List<String> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4");
List<List<String>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5);
//通過工具類創建writer
ExcelWriter writer = ExcelUtil.getWriter("d:/writeTest.xlsx");
//通過構造方法創建writer
//ExcelWriter writer = new ExcelWriter("d:/writeTest.xls");
//跳過當前行,既第一行,非必須,在此演示用
writer.passCurrentRow();
//合並單元格後的標題行,使用默認標題樣式
writer.merge(row1.size() - 1, "測試標題");
//一次性寫出內容,強制輸出標題
writer.write(rows, true);
//關閉writer,釋放內存
writer.close();

(2)寫出Map數據

Map<String, Object> row1 = new LinkedHashMap<>();
row1.put("姓名", "張三");
row1.put("年齡", 23);
row1.put("成績", 88.32);
row1.put("是否合格", true);
row1.put("考試日期", DateUtil.date());
Map<String, Object> row2 = new LinkedHashMap<>();
row2.put("姓名", "李四");
row2.put("年齡", 33);
row2.put("成績", 59.50);
row2.put("是否合格", false);
row2.put("考試日期", DateUtil.date());
ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(row1, row2);
// 通過工具類創建writer
ExcelWriter writer = ExcelUtil.getWriter("d:/writeMapTest.xlsx");
// 合並單元格後的標題行,使用默認標題樣式
writer.merge(row1.size() - 1, "一班成績單");
// 一次性寫出內容,使用默認樣式,強制輸出標題
writer.write(rows, true);
// 關閉writer,釋放內存
writer.close();

(3)寫出Bean數據

public class TestBean {
    private String name;
    private int age;
    private double score;
    private boolean isPass;
    private Date examDate;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    public boolean isPass() {
        return isPass;
    }
    public void setPass(boolean isPass) {
        this.isPass = isPass;
    }
    public Date getExamDate() {
        return examDate;
    }
    public void setExamDate(Date examDate) {
        this.examDate = examDate;
    }
}
TestBean bean1 = new TestBean();
bean1.setName("張三");
bean1.setAge(22);
bean1.setPass(true);
bean1.setScore(66.30);
bean1.setExamDate(DateUtil.date());
TestBean bean2 = new TestBean();
bean2.setName("李四");
bean2.setAge(28);
bean2.setPass(false);
bean2.setScore(38.50);
bean2.setExamDate(DateUtil.date());
List<TestBean> rows = CollUtil.newArrayList(bean1, bean2);
// 通過工具類創建writer
ExcelWriter writer = ExcelUtil.getWriter("d:/writeBeanTest.xlsx");
// 合並單元格後的標題行,使用默認標題樣式
writer.merge(4, "一班成績單");
// 一次性寫出內容,使用默認樣式,強制輸出標題
writer.write(rows, true);
// 關閉writer,釋放內存
writer.close();

(4)自定義Bean的key別名(排序標題)

在寫出Bean的時候,我們可以調用ExcelWriter對象的addHeaderAlias方法自定義Bean中key的別名,這樣就可以寫出自定義標題瞭

// 通過工具類創建writer
ExcelWriter writer = ExcelUtil.getWriter("d:/writeBeanTest.xlsx");
//自定義標題別名
writer.addHeaderAlias("name", "姓名");
writer.addHeaderAlias("age", "年齡");
writer.addHeaderAlias("score", "分數");
writer.addHeaderAlias("isPass", "是否通過");
writer.addHeaderAlias("examDate", "考試時間");
// 合並單元格後的標題行,使用默認標題樣式
writer.merge(4, "一班成績單");
// 一次性寫出內容,使用默認樣式,強制輸出標題
writer.write(rows, true);
// 關閉writer,釋放內存
writer.close();

java解析Excel使用hutool工具類

hutool包,真是好用,方便,強烈推薦…

https://hutool.cn/docs/index.html#/

然後使用Excel的工具類,還需要引入poi-ooxml包版本的話,報錯裡面有

自己確認一下就好瞭

 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

一行代碼搞定:常用的就是read方法瞭.

ExcelReader reader = ExcelUtil.getReader(FileUtil.file("C:\\Users\\stack\\Desktop\\hanke\\aaa.xlsx"));

在這裡插入圖片描述

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: