hutool常用操作備忘


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import java.util.List;

public class HutoolStudy {
    public static void main(String[] args) {
        //身份證操作,根據身份證號取得年齡
        IdcardUtil.getBirthByIdCard("321083197812162119");
        System.out.println(IdcardUtil.getAgeByIdCard("321083197812162119"));
        //集合轉字符串,逗號分割
        String[] col= new String[]{"a","b","c","d","e"};
        List<String> colList = CollUtil.newArrayList(col);
        String str = CollUtil.join(colList, ",");
        System.out.println(str);
        //圖形驗證碼
        //定義圖形驗證碼的長、寬、驗證碼字符數、干擾線寬度
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
        //ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
        //圖形驗證碼寫出,可以寫出到文件,也可以寫出到流
        captcha.write("d:/shear.png");
        //驗證圖形驗證碼的有效性,返回boolean值
        captcha.verify("1234");
        //生成任意長度隨機字符串,可重複
        RandomGenerator randomGenerator = new RandomGenerator("0123456789abcdefh!@#$%^&*", 8);
        for(int index = 0; index < 20; index++)
        {
            System.out.println(randomGenerator.generate());
        }
        //excel操作
        List<String> row1 = CollUtil.newArrayList("aa", "bb", "cc", "dd");
        List<String> row2 = CollUtil.newArrayList("aa1fffffffff", "bb1", "cc1", "dd1");
        List<String> row3 = CollUtil.newArrayList("aa2", "bb2", "ccfffffff2", "dd2");
        List<String> row4 = CollUtil.newArrayList("aa3", "bb3fffffffffffff", "cc3", "dd3");
        List<String> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4ffffffffff");
        List<List<String>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5);
        //通過工具類創建writer
        ExcelWriter writer = ExcelUtil.getWriter("d:/writeTest.xlsx");
        //合併單元格後的標題行,使用默認標題樣式
        writer.merge(row1.size() - 1, "測試標題");
        writer.write(rows);
        //關閉writer,釋放內存
        writer.close();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章