CsvWriter.writeBeans增加重载,可选是否写出表头

This commit is contained in:
Looly 2024-06-14 01:30:09 +08:00
parent ab941a6e27
commit 682cf265c4
2 changed files with 17 additions and 15 deletions

View File

@ -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可能的空指针错误

View File

@ -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);