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();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章