hutool java封装工具 网址https://www.hutool.cn/docs/#/poi/Word%E7%94%9F%E6%88%90-Word07Writer
我在本次的博客中介绍20例子.更多的例子可以访问网站官网.
1.准备依赖
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.代码部分
@SpringBootTest
class HutoolTestApplicationTests {
@Test
void contextLoads() throws IOException {
/**hutool:ResourceUtil 资源工具
* */
//读取文件
String str = ResourceUtil.readUtf8Str("q.txt");
System.out.println(str);
//读取配置文件
ClassPathResource resource = new ClassPathResource("application.properties");
Properties prop = new Properties();
prop.load(resource.getStream());
Console.log("propperties: {}",prop);
}
@Test
void contextLoads1() {
/**hutool:DateUtil 时间工具
* */
//当前时间
DateTime date = DateUtil.date();
Console.log("date:",date);
//当前时间
DateTime date1 = DateUtil.date(Calendar.getInstance());
Console.log("date:",date1);
//当前时间
DateTime date2 = DateUtil.date(System.currentTimeMillis());
Console.log("date:",date2);
//当前时间字符串,格式:yyyy-MM-dd HH:mm:ss
String now = DateUtil.now();
Console.log("date:",now);
//当前日期字符串,格式:yyyy-MM-dd
String today= DateUtil.today();
Console.log("date:",today);
//常用格式的格式化,2021250933591
String format = DateUtil.format(date, "yyyymmddssSS");
Console.log("date:",format);
//计时工具
TimeInterval timer = DateUtil.timer();
long interval = timer.interval();
Console.log("interval:",interval);
long minute = timer.intervalMinute();
Console.log("minute:",minute);
}
@Test
void contextLoads3() throws IOException {
/**hutool: IoUtil 拷贝工具
* */
BufferedInputStream inputStream = FileUtil.getInputStream("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\q.txt");
BufferedOutputStream outputStream = FileUtil.getOutputStream("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\a.txt");
//默认:覆盖
long copy = IoUtil.copy(inputStream, outputStream, IoUtil.EOF);
System.out.println(copy);
}
@Test
void contextLoads4() throws IOException {
/**hutool: FileUtil 文件工具
*
* */
//列出目录
File[] ls = FileUtil.ls("D:\\ideaProject\\hutoolTest");
for (File l : ls) {
System.out.println(l);
}
//创建文件
File touch = FileUtil.touch("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\config1\\c.txt");
touch.canRead();
touch.canWrite();
//mkdir 创建目录
File mkdir = FileUtil.mkdir("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\config1");
mkdir.canWrite();
mkdir.canRead();
//del 递归删除
boolean del = FileUtil.del("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\config1\\c.txt");
System.out.println(del);
}
@Test
void contextLoads5() throws IOException {
/**hutool: FileTypeUtil 文件类型工具
* */
File file = FileUtil.file("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\a.txt");
String type = FileTypeUtil.getType(file);
System.out.println(type);
String xml = FileTypeUtil.putFileType("15f7", "xml");
}
@Test
void contextLoads6() throws IOException {
/**hutool: 16进制工具-HexUtil
* */
String str = "我是一个字符串";
String hex = HexUtil.encodeHexStr(str, CharsetUtil.CHARSET_UTF_8);
Console.log("hex:",hex);
}
@Test
void contextLoads7() throws IOException {
/**
* hutool: XML工具-XmlUtil
* */
Document document = XmlUtil.readXML("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\c.xml");
System.out.println(document);
Object byXPath = XmlUtil.getByXPath("//returnsms/message", document, XPathConstants.STRING);
System.out.println(byXPath);
}
@Test
void contextLoads8() throws Exception {
/**
* hutool: 命令行工具-RuntimeUtil
* */
String ipconfig = RuntimeUtil.execForStr("ipconfig");
System.out.println(ipconfig);
List<String> ipconfig1 = RuntimeUtil.execForLines("ipconfig");
System.out.println(ipconfig1);
}
@Test
void contextLoads9() throws Exception {
/**
* hutool: 随机工具-RandomUtil
* */
//获得指定范围内的随机数
int i = RandomUtil.randomInt(1000);
System.out.println(i);
//获得一个只包含数字的字符串
String s = RandomUtil.randomNumbers(10);
System.out.println(s);
// 随机UUID 暂时不能使用
// RandomUtil.randomUUID()
}
@Test
void contextLoads10() throws Exception {
/**
* hutool: 唯一ID工具-IdUtil
* */
//生成的UUID是带-的字符串
String uuid = IdUtil.randomUUID();
System.out.println(uuid);
//生成的是不带-的字符串
String simpleUUID = IdUtil.simpleUUID();
System.out.println(simpleUUID);
//MongoDB使用
String objectId = IdUtil.objectId();
System.out.println(objectId);
}
@Test
void contextLoads11() throws Exception {
/**
* hutool: 身份证工具-IdcardUtil
* */
String ID_18="321083197812162119";
String ID_15 = "150102880730303";
//验证合法性
boolean validCard = IdcardUtil.isValidCard(ID_18);
boolean validCard1 = IdcardUtil.isValidCard(ID_15);
System.out.println(validCard);
System.out.println(validCard1);
//获取性别
int genderByIdCard = IdcardUtil.getGenderByIdCard(ID_18);
System.out.println(genderByIdCard);
//获取省份
String provinceByIdCard = IdcardUtil.getProvinceByIdCard(ID_18);
System.out.println(provinceByIdCard);
//年龄
DateTime date = DateUtil.parse("2021-08-09");
int ageByIdCard = IdcardUtil.getAgeByIdCard(ID_18, date);
System.out.println(ageByIdCard);
//生日
String birth = IdcardUtil.getBirthByIdCard(ID_18);
System.out.println(birth);
}
@Test
void contextLoads12() throws Exception {
/**
* hutool: 信息脱敏工具-DesensitizedUtil
* */
//身份证
String s = DesensitizedUtil.idCardNum("51343620000320711X", 1, 2);
System.out.println(s);
//手机号
String s1 = DesensitizedUtil.mobilePhone("13566901251");
System.out.println(s1);
//中文名字
String s2 = DesensitizedUtil.chineseName("张三");
System.out.println(s2);
//邮箱
String email = DesensitizedUtil.email("qrw1314@163.com");
System.out.println(email);
}
@Test
void contextLoads13() throws Exception {
/**
* hutool: HashMap扩展-Dict(Python字典)
* */
Dict dict = Dict.create()
.set("key1", 1)//int
.set("key2", 1000L)//long
.set("key3", DateTime.now());
System.out.println(dict);
}
@Test
void contextLoads14() throws Exception {
/**
* hutool: 控制台打印封装-Console
* */
String[] a = {"abc", "bcd", "def"};
Console.log(a);
}
@Test
void contextLoads15() throws Exception {
/**
* hutool: 邮件工具-MailUtil
* 注意:AuthenticationFailedException: 550 User has no permission
* */
MailUtil.send("hutool@foxmail.com", "测试", "邮件来自Hutool测试", false);
}
@Test
void contextLoads16() throws Exception {
/**
* hutool: Excel工具-ExcelUtil
*注意:在写入时候一定关闭文件
* */
ExcelReader reader = ExcelUtil.getReader(FileUtil.file("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\1.xlsx"));
List<List<Object>> read = reader.read();
for (List<Object> objects : read) {
System.out.println(objects);
}
Map<String, Object> row1 = new LinkedHashMap<>();
row1.put("id",1);
row1.put("name","xiaolanlan");
row1.put("age",34);
row1.put("addree","shanghai");
Map<String, Object> row2 = new LinkedHashMap<>();
row2.put("id",1);
row2.put("name","xiaolanlan");
row2.put("age",34);
row2.put("addree","shanghai");
ArrayList<Map<String, Object>> maps = CollUtil.newArrayList(row1, row2,row1,row2);
ExcelWriter writer = ExcelUtil.getWriter("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\1.xlsx");
writer.write(maps,true);
writer.close();
}
@Test
void contextLoads17() throws Exception {
/**
* hutool: 系统属性调用-SystemUtil
*
* */
JvmSpecInfo info = SystemUtil.getJvmSpecInfo();
System.out.println(info);
JvmInfo jvmInfo = SystemUtil.getJvmInfo();
System.out.println(jvmInfo);
JavaRuntimeInfo javaRuntimeInfo = SystemUtil.getJavaRuntimeInfo();
System.out.println(javaRuntimeInfo);
RuntimeInfo runtimeInfo = SystemUtil.getRuntimeInfo();
System.out.println(runtimeInfo);
HostInfo hostInfo = SystemUtil.getHostInfo();
System.out.println(hostInfo);
}
@Test
void contextLoads18() throws Exception {
/**
* hutool: Emoji工具-EmojiUtil
* */
String s = EmojiUtil.toAlias("\uD83D\uDE04");
System.out.println(s);
String emoji = EmojiUtil.toUnicode(":smile:");
System.out.println(emoji);
}
@Test
void contextLoads19() throws Exception {
/**
* hutool: Script工具-ScriptUtil
* */
Object eval = ScriptUtil.eval("print('Script test!');");
System.out.println(eval);
CompiledScript script = ScriptUtil.compile("print('Script test!');");
Object eval1 = script.eval();
}
@Test
void contextLoads20() throws Exception {
/**
* hutool: Word生成-Word07Writer
* */
Word07Writer writer = new Word07Writer();
writer.addText(new Font("方正小标宋简体", Font.PLAIN, 22), "我是第一部分", "我是第二部分");
writer.addText(new Font("宋体", Font.PLAIN, 22), "我是正文第一部分", "我是正文第二部分");
writer.flush(FileUtil.file("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\wordWrite.docx"));
writer.close();
}
}