fix dumplicate bug

This commit is contained in:
Looly 2022-04-24 13:36:23 +08:00
parent 913e97c272
commit 37f53e9856

View File

@ -8,6 +8,8 @@ import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.core.map.multi.RowKeyTable;
import cn.hutool.core.map.multi.Table;
import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
@ -39,7 +41,6 @@ import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -1045,24 +1046,35 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
return passCurrentRow(); return passCurrentRow();
} }
final Map<?, ?> aliasMap = aliasMap(rowMap); final Table<?, ?, ?> aliasTable = aliasTable(rowMap);
if (isWriteKeyAsHead) { if (isWriteKeyAsHead) {
writeHeadRow(aliasMap.keySet()); // 写出标题行并记录标题别名和列号的关系
writeHeadRow(aliasTable.columnKeySet());
// 记录原数据key对应列号
int i = 0;
for (Object key : aliasTable.rowKeySet()) {
this.headLocationCache.putIfAbsent(StrUtil.toString(key), i);
i++;
}
} }
// 如果已经写出标题行根据标题行找对应的值写入 // 如果已经写出标题行根据标题行找对应的值写入
if (MapUtil.isNotEmpty(this.headLocationCache)) { if (MapUtil.isNotEmpty(this.headLocationCache)) {
final Row row = RowUtil.getOrCreateRow(this.sheet, this.currentRow.getAndIncrement()); final Row row = RowUtil.getOrCreateRow(this.sheet, this.currentRow.getAndIncrement());
Integer location; Integer location;
for (Entry<?, ?> entry : aliasMap.entrySet()) { for (Table.Cell<?, ?, ?> cell : aliasTable) {
location = this.headLocationCache.get(StrUtil.toString(entry.getKey())); // 首先查找原名对应的列号
location = this.headLocationCache.get(StrUtil.toString(cell.getRowKey()));
if(null == location){
// 未找到则查找别名对应的列号
location = this.headLocationCache.get(StrUtil.toString(cell.getColumnKey()));
}
if (null != location) { if (null != location) {
CellUtil.setCellValue(CellUtil.getOrCreateCell(row, location), entry.getValue(), this.styleSet, false); CellUtil.setCellValue(CellUtil.getOrCreateCell(row, location), cell.getValue(), this.styleSet, false);
} }
} }
} else { } else {
writeRow(aliasMap.values()); writeRow(aliasTable.values());
} }
return this; return this;
} }
@ -1328,23 +1340,24 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @param rowMap 一行数据 * @param rowMap 一行数据
* @return 别名列表 * @return 别名列表
*/ */
private Map<?, ?> aliasMap(Map<?, ?> rowMap) { private Table<?, ?, ?> aliasTable(Map<?, ?> rowMap) {
final Table<Object, Object, Object> filteredTable = new RowKeyTable<>(new LinkedHashMap<>(), LinkedHashMap::new);
if (MapUtil.isEmpty(this.headerAlias)) { if (MapUtil.isEmpty(this.headerAlias)) {
return rowMap; rowMap.forEach((key, value)-> filteredTable.put(key, key, value));
}else{
rowMap.forEach((key, value)->{
final String aliasName = this.headerAlias.get(StrUtil.toString(key));
if (null != aliasName) {
// 别名键值对加入
filteredTable.put(key, aliasName, value);
} else if (false == this.onlyAlias) {
// 保留无别名设置的键值对
filteredTable.put(key, key, value);
}
});
} }
final Map<Object, Object> filteredMap = MapUtil.newHashMap(rowMap.size(), true); return filteredTable;
rowMap.forEach((key, value)->{
final String aliasName = this.headerAlias.get(StrUtil.toString(key));
if (null != aliasName) {
// 别名键值对加入
filteredMap.put(aliasName, value);
} else if (false == this.onlyAlias) {
// 保留无别名设置的键值对
filteredMap.put(key, value);
}
});
return filteredMap;
} }
/** /**