Java Hutool工具實(shí)現(xiàn)驗(yàn)證碼生成及Excel文件的導(dǎo)入和導(dǎo)出
1、Hutool工具簡介
HuTool工具(糊涂工具),第三方插件工具,簡化操作,是國產(chǎn)的一個(gè)產(chǎn)品,界面簡潔易懂,比較人性化。(上班可能經(jīng)常用的到,建議收藏起來)
Hutool是一個(gè)小而全的Java工具類庫,通過靜態(tài)方法封裝,降低相關(guān)API的學(xué)習(xí)成本,提高工作效率,使Java擁有函數(shù)式語言般的優(yōu)雅,讓Java語言也可以“甜甜的”。
2、Hutool的相關(guān)依賴
maven項(xiàng)目在pom.xml添加以下依賴即可:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.3</version>
</dependency>
3、驗(yàn)證碼工具
@Test
public void hutoolCodeTest() throws FileNotFoundException {
/**、
* 1、創(chuàng)建一個(gè)驗(yàn)證碼:
* 驗(yàn)證碼 captcha['k?pt??]
* line:線條
* 參數(shù)說明
* width:寬度
* height:高度
* codeCount:字符數(shù)量
* lineCount:干擾線數(shù)量
*/
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 40, 4, 4);
//2.獲得生成的驗(yàn)證碼的真實(shí)碼值
String code = lineCaptcha.getCode();
System.out.println(code);
//3.將驗(yàn)證碼圖片輸出到D盤根目錄下
lineCaptcha.write(new FileOutputStream("D:/text.png"));
}?
?
4、excel工具
POI依賴導(dǎo)入,否則報(bào)錯(cuò)
You need to add dependency of 'poi-ooxml' to your project, and version >= 3.17
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
(1)Exel導(dǎo)出
@Test
public void HutollDownLoadExcelTest() throws FileNotFoundException {
//1.創(chuàng)建一個(gè)Excel寫出工具Writer
ExcelWriter writer = ExcelUtil.getWriter(true);
//2.模擬List數(shù)據(jù)
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
//3.將list數(shù)據(jù)輸出到excel中
writer.write(list);
//4. 將填充數(shù)據(jù)后的excel文件保存在d盤文件中.
writer.flush(new FileOutputStream("d:/id.xlsx"));
}

(2)excel導(dǎo)出詳細(xì)講解
實(shí)體類
public class User {
private String id;
private String name;
private Integer age;
測試類
//1. 創(chuàng)建一個(gè)Excel寫出工具Writer
ExcelWriter writer = ExcelUtil.getWriter(true);
//2. 設(shè)置別名
writer.addHeaderAlias("id","編號");
writer.addHeaderAlias("name","名字");
writer.addHeaderAlias("age","年齡");
writer.addHeaderAlias("birth","生日");
//3. 先輸出一行標(biāo)題(參數(shù)1是跨列數(shù), 從0開始. 參數(shù)2 是標(biāo)題字符串)
writer.merge(3,"標(biāo)題");
//4. 模擬list集合數(shù)據(jù)
List<User> users = getUsers();
//5. 將users數(shù)據(jù)輸出到excel文件中.
writer.write(users,true);// true表示并輸出標(biāo)題。
//6. 將填充數(shù)據(jù)后的excel文件保存在d盤文件中.
writer.flush(new FileOutputStream("D:/users.xlsx"));
}
private List<User> getUsers(){
List<User> list = new ArrayList<>();
list.add(new User("1001","張三",18,new Date()));
list.add(new User("1002","張三",18,new Date()));
list.add(new User("1003","張三",18,new Date()));
list.add(new User("1004","張三",18,new Date()));
return list;
}

(2)Excel導(dǎo)入
@Test
public void HutollUploadExcelTest() throws FileNotFoundException {
//1. 創(chuàng)建一個(gè)Excel讀取工具reader
ExcelReader reader = ExcelUtil.getReader(new FileInputStream("D:/users.xlsx"));
//2. 設(shè)置讀取的數(shù)據(jù)的別名和封裝的實(shí)體的屬性對應(yīng)關(guān)系.
reader.addHeaderAlias("編號","id");
reader.addHeaderAlias("名字","name");
reader.addHeaderAlias("年齡","age");
reader.addHeaderAlias("生日","birth");
/*
reader讀取excel文件數(shù)據(jù)
參數(shù)說明
headerRowIndex: 映射的數(shù)據(jù)標(biāo)題在第幾行, 從0開始算.
startRowIndex: 實(shí)際讀取的數(shù)據(jù)從第幾行開始, 從0開始算.
class: 讀取的數(shù)據(jù)封裝成什么類型的對象
*/
List<User> users = reader.read(1, 2, User.class);
// 輸出返回的users集合
System.out.println(users);
// 輸出users的大小.
System.out.println(users.size());
}
?
到此這篇關(guān)于Java Hutool工具實(shí)現(xiàn)驗(yàn)證碼生成及Excel文件的導(dǎo)入和導(dǎo)出的文章就介紹到這了,更多相關(guān)Java Hutool工具內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java啟動jar包修改JVM默認(rèn)內(nèi)存問題
這篇文章主要介紹了java啟動jar包修改JVM默認(rèn)內(nèi)存問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
基于Java設(shè)計(jì)一個(gè)高并發(fā)的秒殺系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了如何基于Java設(shè)計(jì)一個(gè)高并發(fā)的秒殺系統(tǒng),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2023-10-10
Java面試題-實(shí)現(xiàn)復(fù)雜鏈表的復(fù)制代碼分享
這篇文章主要介紹了Java面試題-實(shí)現(xiàn)復(fù)雜鏈表的復(fù)制代碼分享,小編覺得還是挺不錯(cuò)的,具有參考價(jià)值,需要的朋友可以了解下。2017-10-10
Java Semaphore實(shí)現(xiàn)高并發(fā)場景下的流量控制
在java開發(fā)的工作中是否會出現(xiàn)這樣的場景,你需要實(shí)現(xiàn)一些異步運(yùn)行的任務(wù),該任務(wù)可能存在消耗大量內(nèi)存的情況,所以需要對任務(wù)進(jìn)行并發(fā)控制。本文將介紹通過Semaphore類優(yōu)雅的實(shí)現(xiàn)并發(fā)控制,感興趣的可以了解一下2021-12-12
IDEA創(chuàng)建Java Web項(xiàng)目不能及時(shí)刷新HTML或JSP頁面問題
這篇文章主要介紹了IDEA創(chuàng)建Java Web項(xiàng)目不能及時(shí)刷新HTML或JSP頁面問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

