mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
CsvWriter.writeBeans增加重载,可选是否写出表头
This commit is contained in:
parent
ab941a6e27
commit
682cf265c4
@ -9,6 +9,7 @@
|
||||
* 【core 】 ListUtil增加move方法(issue#3603@Github)
|
||||
* 【core 】 CollUtil.subtract增加空判定(issue#3605@Github)
|
||||
* 【core 】 优化DateUtil.format(Date date, String format)接口效率(pr#1226@Gitee)
|
||||
* 【csv 】 CsvWriter.writeBeans增加重载,可选是否写出表头(issue#IA57W2@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复AnnotationUtil可能的空指针错误
|
||||
|
@ -236,20 +236,7 @@ public final class CsvWriter implements Closeable, Flushable, Serializable {
|
||||
* @return this
|
||||
*/
|
||||
public CsvWriter writeBeans(Iterable<?> beans) {
|
||||
if (CollUtil.isNotEmpty(beans)) {
|
||||
boolean isFirst = true;
|
||||
Map<String, Object> map;
|
||||
for (Object bean : beans) {
|
||||
map = BeanUtil.beanToMap(bean);
|
||||
if (isFirst) {
|
||||
writeHeaderLine(map.keySet().toArray(new String[0]));
|
||||
isFirst = false;
|
||||
}
|
||||
writeLine(Convert.toStrArray(map.values()));
|
||||
}
|
||||
flush();
|
||||
}
|
||||
return this;
|
||||
return writeBeans(beans, (String[]) null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -260,8 +247,22 @@ public final class CsvWriter implements Closeable, Flushable, Serializable {
|
||||
* @return this
|
||||
*/
|
||||
public CsvWriter writeBeans(Iterable<?> beans, String... properties) {
|
||||
return writeBeans(beans, true, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将一个Bean集合写出到Writer,并自动生成表头
|
||||
*
|
||||
* @param beans Bean集合
|
||||
* @param writeHeaderLine 是否写出表头,即Bean的字段名称列表作为首行
|
||||
* @param properties Bean 中指定的可以导出的属性
|
||||
* @return this
|
||||
* @since 5.8.29
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public CsvWriter writeBeans(Iterable<?> beans, boolean writeHeaderLine, String... properties) {
|
||||
if (CollUtil.isNotEmpty(beans)) {
|
||||
boolean isFirst = true;
|
||||
boolean isFirst = writeHeaderLine;
|
||||
Map<String, Object> map;
|
||||
for (Object bean : beans) {
|
||||
map = BeanUtil.beanToMap(bean, properties);
|
||||
|
Loading…
Reference in New Issue
Block a user