mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-08 14:48:01 +08:00
!345 CsvWriter添加writeBeans方法,根据Bean自动生成表头,并写入数据
Merge pull request !345 from 马万里/v5-dev
This commit is contained in:
commit
d6fd4e259b
@ -1,5 +1,6 @@
|
|||||||
package cn.hutool.core.text.csv;
|
package cn.hutool.core.text.csv;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
@ -18,7 +19,10 @@ import java.io.IOException;
|
|||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CSV数据写出器
|
* CSV数据写出器
|
||||||
@ -205,6 +209,25 @@ public final class CsvWriter implements Closeable, Flushable, Serializable {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将一个Bean集合写出到Writer,并自动生成表头
|
||||||
|
*
|
||||||
|
* @param beans Bean集合
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public CsvWriter writeBeans(Collection<?> beans) {
|
||||||
|
if (CollUtil.isNotEmpty(beans)) {
|
||||||
|
List<Collection<Object>> res = new ArrayList<>();
|
||||||
|
for (Object bean : beans) {
|
||||||
|
res.add(BeanUtil.beanToMap(bean).keySet().stream().map(k -> (Object) k).collect(Collectors.toList()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
res.addAll(beans.stream().map(bean -> BeanUtil.beanToMap(bean).values()).collect(Collectors.toList()));
|
||||||
|
write(res);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 写出一行
|
* 写出一行
|
||||||
*
|
*
|
||||||
|
@ -61,6 +61,62 @@ public class CsvUtilTest {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Ignore
|
||||||
|
public void writeBeansTest() {
|
||||||
|
class Student {
|
||||||
|
Integer id;
|
||||||
|
String name;
|
||||||
|
Integer age;
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(Integer age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CsvWriter writer = CsvUtil.getWriter("d:/test/testWriteBeans.csv", CharsetUtil.CHARSET_UTF_8);
|
||||||
|
List<Student> students = new ArrayList<>();
|
||||||
|
Student student1 = new Student();
|
||||||
|
Student student2 = new Student();
|
||||||
|
Student student3 = new Student();
|
||||||
|
|
||||||
|
student1.setId(1);
|
||||||
|
student1.setName("张三");
|
||||||
|
student1.setAge(18);
|
||||||
|
|
||||||
|
student2.setId(2);
|
||||||
|
student2.setName("李四");
|
||||||
|
student2.setAge(22);
|
||||||
|
|
||||||
|
student3.setId(3);
|
||||||
|
student3.setName("王五");
|
||||||
|
student3.setAge(31);
|
||||||
|
|
||||||
|
students.add(student1);
|
||||||
|
students.add(student2);
|
||||||
|
students.add(student3);
|
||||||
|
writer.writeBeans(students);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void readLfTest(){
|
public void readLfTest(){
|
||||||
|
Loading…
Reference in New Issue
Block a user