Skip to content

Commit

Permalink
Merge pull request #332 from liaochong/feature/3.11.8
Browse files Browse the repository at this point in the history
Feature/3.11.8
  • Loading branch information
liaochong authored Nov 6, 2021
2 parents d74857b + 5f556a7 commit 837ffb4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.github.liaochong</groupId>
<artifactId>myexcel</artifactId>
<version>3.11.7</version>
<version>3.11.8</version>
<packaging>jar</packaging>

<name>myexcel</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@ abstract class AbstractReadHandler<T> {

private T obj;

protected Map<String, Integer> titles = new HashMap<>();
protected Map<Integer, String> titles = new HashMap<>();

protected SaxExcelReader.ReadConfig<T> readConfig;

private BiConsumer<String, Integer> addTitleConsumer = (v, colNum) -> {
};

private ReadContext<T> context = new ReadContext<>();

private RowContext rowContext = new RowContext();
Expand Down Expand Up @@ -95,10 +92,7 @@ public AbstractReadHandler(boolean readCsv,
fieldMap = ReflectUtil.getFieldMapOfExcelColumn(dataType);
this.readConfig = readConfig;
boolean isMapType = dataType == Map.class;
if (!isMapType && fieldMap.isEmpty()) {
addTitleConsumer = this::addTitles;
readWithTitle = true;
}
readWithTitle = !isMapType && fieldMap.isEmpty();
setNewInstanceFunction(dataType, isMapType);
// 全局配置获取
setConfiguration(dataType, isMapType);
Expand Down Expand Up @@ -183,12 +177,6 @@ protected void convert(String value, int rowNum, int colNum, Field field) {
ReadConverterContext.convert(obj, context, convertContext, readConfig.getExceptionFunction());
}

private void addTitles(String formattedValue, int thisCol) {
if (currentRow.getRowNum() == titleRowNum) {
titles.put(formattedValue, thisCol);
}
}

protected void newRow(int rowNum) {
currentRow.setRowNum(rowNum);
obj = newInstance.get();
Expand All @@ -209,15 +197,13 @@ protected void handleField(Integer colNum, String content) {
if (readConfig.getRowFilter().test(currentRow)) {
fieldHandler.accept(colNum, content);
} else if (readWithTitle) {
titles.put(colNum, content);
if (titleRowNum == -1) {
// 尝试下一行是否为标题行
Row nextRow = new Row(currentRow.getRowNum() + 1);
if (readConfig.getRowFilter().test(nextRow)) {
titleRowNum = currentRow.getRowNum();
this.addTitleConsumer.accept(content, colNum);
}
} else {
this.addTitleConsumer.accept(content, colNum);
}
}
}
Expand Down Expand Up @@ -258,7 +244,7 @@ private void initFieldMap() {
Map<String, Field> titleFieldMap = ReflectUtil.getFieldMapOfTitleExcelColumn(readConfig.getDataType());
fieldMap = new HashMap<>(titleFieldMap.size());
titles.forEach((k, v) -> {
fieldMap.put(v, titleFieldMap.get(k));
fieldMap.put(k, titleFieldMap.get(v));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.github.liaochong.myexcel.core.annotation.ExcelColumn;
import com.github.liaochong.myexcel.core.cache.WeakCache;
import com.github.liaochong.myexcel.core.constant.Constants;
import com.github.liaochong.myexcel.core.reflect.ClassFieldContainer;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -118,7 +119,8 @@ public static Map<String, Field> getFieldMapOfTitleExcelColumn(Class<?> dataType
throw new IllegalStateException("Title cannot be repeated: " + title + ". Please check it.");
}
field.setAccessible(true);
fieldMap.put(title, field);
String[] splits = title.split(Constants.ARROW);
fieldMap.put(splits[splits.length - 1], field);
}
TITLE_FIELD_CACHE.cache(dataType, fieldMap);
return fieldMap;
Expand Down

0 comments on commit 837ffb4

Please sign in to comment.