您好,登錄后才能下訂單哦!
這篇文章主要介紹“Hutool Java工具類(lèi)庫(kù)_ExcelUtil怎么使用”,在日常操作中,相信很多人在Hutool Java工具類(lèi)庫(kù)_ExcelUtil怎么使用問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Hutool Java工具類(lèi)庫(kù)_ExcelUtil怎么使用”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
<!--Hutool Java工具包--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.4.5</version> </dependency>
從文件中讀取Excel為ExcelReader
ExcelReader reader = ExcelUtil.getReader(FileUtil.file("test.xlsx"));
從流中讀取Excel為ExcelReader
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xlsx"));
讀取指定的sheet
ExcelReader reader; //通過(guò)sheet編號(hào)獲取 reader = ExcelUtil.getReader(FileUtil.file("test.xlsx"), 0); //通過(guò)sheet名獲取 reader = ExcelUtil.getReader(FileUtil.file("test.xlsx"), "sheet1");
讀取Excel中所有行和列,都用列表表示
ExcelReader reader = ExcelUtil.getReader("d:/aaa.xlsx"); List<List<Object>> readAll = reader.read();
讀取為Map列表,默認(rèn)第一行為標(biāo)題行,Map中的key為標(biāo)題,value為標(biāo)題對(duì)應(yīng)的單元格值
ExcelReader reader = ExcelUtil.getReader("d:/aaa.xlsx"); List<Map<String,Object>> readAll = reader.readAll();
讀取為Bean列表,Bean中的字段名為標(biāo)題,字段值為標(biāo)題對(duì)應(yīng)的單元格值
ExcelReader reader = ExcelUtil.getReader("d:/aaa.xlsx"); List<Person> all = reader.readAll(Person.class);
Hutool將Excel寫(xiě)出封裝為ExcelWriter,原理為包裝了Workbook對(duì)象,每次調(diào)用merge(合并單元格)或者write(寫(xiě)出數(shù)據(jù))方法后只是將數(shù)據(jù)寫(xiě)入到Workbook,并不寫(xiě)出文件,只有調(diào)用flush或者close方法后才會(huì)真正寫(xiě)出文件。
由于機(jī)制原因,在寫(xiě)出結(jié)束后需要關(guān)閉ExcelWriter對(duì)象,調(diào)用close方法即可關(guān)閉,此時(shí)才會(huì)釋放Workbook對(duì)象資源,否則帶有數(shù)據(jù)的Workbook一直會(huì)常駐內(nèi)存
(1) 將行列對(duì)象寫(xiě)出到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); //通過(guò)工具類(lèi)創(chuàng)建writer ExcelWriter writer = ExcelUtil.getWriter("d:/writeTest.xlsx"); //通過(guò)構(gòu)造方法創(chuàng)建writer //ExcelWriter writer = new ExcelWriter("d:/writeTest.xls"); //跳過(guò)當(dāng)前行,既第一行,非必須,在此演示用 writer.passCurrentRow(); //合并單元格后的標(biāo)題行,使用默認(rèn)標(biāo)題樣式 writer.merge(row1.size() - 1, "測(cè)試標(biāo)題"); //一次性寫(xiě)出內(nèi)容,強(qiáng)制輸出標(biāo)題 writer.write(rows, true); //關(guān)閉writer,釋放內(nèi)存 writer.close();
(2)寫(xiě)出Map數(shù)據(jù)
Map<String, Object> row1 = new LinkedHashMap<>(); row1.put("姓名", "張三"); row1.put("年齡", 23); row1.put("成績(jī)", 88.32); row1.put("是否合格", true); row1.put("考試日期", DateUtil.date()); Map<String, Object> row2 = new LinkedHashMap<>(); row2.put("姓名", "李四"); row2.put("年齡", 33); row2.put("成績(jī)", 59.50); row2.put("是否合格", false); row2.put("考試日期", DateUtil.date()); ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(row1, row2); // 通過(guò)工具類(lèi)創(chuàng)建writer ExcelWriter writer = ExcelUtil.getWriter("d:/writeMapTest.xlsx"); // 合并單元格后的標(biāo)題行,使用默認(rèn)標(biāo)題樣式 writer.merge(row1.size() - 1, "一班成績(jī)單"); // 一次性寫(xiě)出內(nèi)容,使用默認(rèn)樣式,強(qiáng)制輸出標(biāo)題 writer.write(rows, true); // 關(guān)閉writer,釋放內(nèi)存 writer.close();
(3)寫(xiě)出Bean數(shù)據(jù)
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); // 通過(guò)工具類(lèi)創(chuàng)建writer ExcelWriter writer = ExcelUtil.getWriter("d:/writeBeanTest.xlsx"); // 合并單元格后的標(biāo)題行,使用默認(rèn)標(biāo)題樣式 writer.merge(4, "一班成績(jī)單"); // 一次性寫(xiě)出內(nèi)容,使用默認(rèn)樣式,強(qiáng)制輸出標(biāo)題 writer.write(rows, true); // 關(guān)閉writer,釋放內(nèi)存 writer.close();
(4)自定義Bean的key別名(排序標(biāo)題)
在寫(xiě)出Bean的時(shí)候,我們可以調(diào)用ExcelWriter對(duì)象的addHeaderAlias方法自定義Bean中key的別名,這樣就可以寫(xiě)出自定義標(biāo)題了
// 通過(guò)工具類(lèi)創(chuàng)建writer ExcelWriter writer = ExcelUtil.getWriter("d:/writeBeanTest.xlsx"); //自定義標(biāo)題別名 writer.addHeaderAlias("name", "姓名"); writer.addHeaderAlias("age", "年齡"); writer.addHeaderAlias("score", "分?jǐn)?shù)"); writer.addHeaderAlias("isPass", "是否通過(guò)"); writer.addHeaderAlias("examDate", "考試時(shí)間"); // 合并單元格后的標(biāo)題行,使用默認(rèn)標(biāo)題樣式 writer.merge(4, "一班成績(jī)單"); // 一次性寫(xiě)出內(nèi)容,使用默認(rèn)樣式,強(qiáng)制輸出標(biāo)題 writer.write(rows, true); // 關(guān)閉writer,釋放內(nèi)存 writer.close();
hutool包,真是好用,方便,強(qiáng)烈推薦…
https://hutool.cn/docs/index.html#/
然后使用Excel的工具類(lèi),還需要引入poi-ooxml包版本的話,報(bào)錯(cuò)里面有
自己確認(rèn)一下就好了
<!-- 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"));
到此,關(guān)于“Hutool Java工具類(lèi)庫(kù)_ExcelUtil怎么使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。