Skip to content

Commit

Permalink
Merge pull request #393 from liaochong/feature/4.3.0.RELEASE
Browse files Browse the repository at this point in the history
Feature/4.3.0.release
  • Loading branch information
liaochong authored May 1, 2023
2 parents e09a514 + b96b328 commit 9ff6cc0
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 109 deletions.
2 changes: 1 addition & 1 deletion example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<dependency>
<groupId>com.github.liaochong</groupId>
<artifactId>myexcel</artifactId>
<version>4.3.0.RC</version>
<version>4.3.0.RC5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand Down
8 changes: 4 additions & 4 deletions 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>4.3.0.RC4</version>
<version>4.3.0.RC5</version>
<packaging>jar</packaging>

<name>myexcel</name>
Expand All @@ -23,17 +23,17 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<poi.version>5.2.3</poi.version>
<jsoup.version>1.15.4</jsoup.version>
<jsoup.version>1.16.1</jsoup.version>
<lombok.version>1.18.22</lombok.version>
<beetl.version>3.15.0.RELEASE</beetl.version>
<beetl.version>3.15.4.RELEASE</beetl.version>
<velocity.version>2.3</velocity.version>
<freemarker.version>2.3.32</freemarker.version>
<groovy-templates.version>3.0.9</groovy-templates.version>
<thymeleaf.version>3.1.1.RELEASE</thymeleaf.version>
<enjoy.version>5.0.3</enjoy.version>
<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
<junit-jupiter-api.version>5.8.2</junit-jupiter-api.version>
<imageio-jpeg.version>3.8.2</imageio-jpeg.version>
<imageio-jpeg.version>3.9.4</imageio-jpeg.version>
<commons-csv.version>1.10.0</commons-csv.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ private void setConfiguration(Class<T> dataType, boolean isMapType) {
ClassFieldContainer classFieldContainer = ReflectUtil.getAllFieldsOfClass(dataType);
ConfigurationUtil.parseConfiguration(classFieldContainer, readContext.convertContext.configuration);

List<Field> fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class);
fields.forEach(field -> {
ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class);
List<FieldDefinition> fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class);
fields.forEach(fieldDefinition -> {
ExcelColumn excelColumn = fieldDefinition.getField().getAnnotation(ExcelColumn.class);
if (excelColumn == null) {
return;
}
ExcelColumnMapping mapping = ExcelColumnMapping.mapping(excelColumn);
readContext.convertContext.excelColumnMappingMap.put(field, mapping);
readContext.convertContext.excelColumnMappingMap.put(fieldDefinition.getField(), mapping);
});
}

Expand All @@ -183,7 +183,7 @@ protected void setFieldHandlerFunction() {
fieldHandler = (colNum, content) -> {
FieldDefinition fieldDefinition = fieldDefinitionMap.get(colNum);
if (fieldDefinition != null) {
convert(content, currentRow.getRowNum(), colNum, fieldDefinition.getField());
convert(content, currentRow.getRowNum(), colNum, fieldDefinition);
}
};
} else {
Expand All @@ -197,7 +197,7 @@ protected void setFieldHandlerFunction() {
boolean isList = fieldDefinition.getField().getType() == List.class;
if (!isList && fieldDefinition.getParentFields().isEmpty()) {
if (target == null) {
convert(content, currentRow.getRowNum(), colNum, fieldDefinition.getField());
convert(content, currentRow.getRowNum(), colNum, fieldDefinition);
}
} else {
try {
Expand Down Expand Up @@ -227,15 +227,15 @@ protected void setFieldHandlerFunction() {
}
}
if (isBase) {
convert(prevObj, content, currentRow.getRowNum(), colNum, fieldDefinition.getField());
convert(prevObj, content, currentRow.getRowNum(), colNum, fieldDefinition);
} else {
Object targetParent = ((List) prevObj).get(((List) prevObj).size() - 1);
Object targetObj = fieldDefinition.getField().get(targetParent);
if (targetObj == null) {
targetObj = new LinkedList<>();
fieldDefinition.getField().set(targetParent, targetObj);
}
convert(targetObj, content, currentRow.getRowNum(), colNum, fieldDefinition.getField());
convert(targetObj, content, currentRow.getRowNum(), colNum, fieldDefinition);
}
} else {
Object value;
Expand All @@ -245,7 +245,7 @@ protected void setFieldHandlerFunction() {
} else {
value = ((List<Object>) prevObj).get(((List<Object>) prevObj).size() - 1);
}
convert(value, content, currentRow.getRowNum(), colNum, fieldDefinition.getField());
convert(value, content, currentRow.getRowNum(), colNum, fieldDefinition);
}
}
} catch (Exception e) {
Expand All @@ -257,20 +257,20 @@ protected void setFieldHandlerFunction() {
}
}

protected void convert(Object prevObj, String value, int rowNum, int colNum, Field field) {
if (value == null || field == null) {
protected void convert(Object prevObj, String value, int rowNum, int colNum, FieldDefinition fieldDefinition) {
if (value == null || fieldDefinition.getField() == null) {
return;
}
readContext.reset(obj, field, value, rowNum, colNum);
readContext.reset(obj, fieldDefinition, value, rowNum, colNum);
ReadConverterContext.convert(prevObj, readContext);
readContext.revert();
}

protected void convert(String value, int rowNum, int colNum, Field field) {
if (value == null || field == null) {
protected void convert(String value, int rowNum, int colNum, FieldDefinition fieldDefinition) {
if (value == null || fieldDefinition.getField() == null) {
return;
}
readContext.reset(obj, field, value, rowNum, colNum);
readContext.reset(obj, fieldDefinition, value, rowNum, colNum);
ReadConverterContext.convert(obj, readContext);
readContext.revert();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.github.liaochong.myexcel.core.reflect.ClassFieldContainer;
import com.github.liaochong.myexcel.core.strategy.WidthStrategy;
import com.github.liaochong.myexcel.utils.ConfigurationUtil;
import com.github.liaochong.myexcel.utils.FieldDefinition;
import com.github.liaochong.myexcel.utils.ReflectUtil;
import com.github.liaochong.myexcel.utils.StringUtil;
import com.github.liaochong.myexcel.utils.TdUtil;
Expand Down Expand Up @@ -74,7 +75,7 @@ abstract class AbstractSimpleExcelBuilder {
/**
* 已排序字段
*/
protected List<Field> filteredFields = Collections.emptyList();
protected List<FieldDefinition> filteredFields = Collections.emptyList();
/**
* 标题
*/
Expand Down Expand Up @@ -129,16 +130,17 @@ public AbstractSimpleExcelBuilder(boolean isCsvBuild) {
* @param groups 分组
* @return Field
*/
protected List<Field> getFilteredFields(ClassFieldContainer classFieldContainer, Class<?>... groups) {
protected List<FieldDefinition> getFilteredFields(ClassFieldContainer classFieldContainer, Class<?>... groups) {
ConfigurationUtil.parseConfiguration(classFieldContainer, configuration);
this.parseGlobalStyle();
List<Field> preElectionFields = this.getPreElectionFields(classFieldContainer);
List<Field> buildFields = this.getGroupFields(preElectionFields, groups);
List<FieldDefinition> preElectionFields = this.getPreElectionFields(classFieldContainer);
List<FieldDefinition> buildFields = this.getGroupFields(preElectionFields, groups);
// 初始化标题容器
List<String> titles = new ArrayList<>(buildFields.size());

for (int i = 0, size = buildFields.size(); i < size; i++) {
Field field = buildFields.get(i);
FieldDefinition fieldDefinition = buildFields.get(i);
Field field = fieldDefinition.getField();
ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class);
String[] columnStyles = null;
if (excelColumn != null) {
Expand All @@ -164,7 +166,7 @@ protected List<Field> getFilteredFields(ClassFieldContainer classFieldContainer,
formats.put(i, excelColumn.dateFormatPattern());
}
ExcelColumnMapping mapping = ExcelColumnMapping.mapping(excelColumn);
excelColumnMappingMap.put(field, mapping);
excelColumnMappingMap.put(fieldDefinition.getField(), mapping);
} else {
if (configuration.useFieldNameAsTitle) {
titles.add(field.getName());
Expand All @@ -176,7 +178,7 @@ protected List<Field> getFilteredFields(ClassFieldContainer classFieldContainer,
setGlobalFormat(i, field);
}
setTitles(titles);
hasMultiColumn = buildFields.stream().anyMatch(field -> field.isAnnotationPresent(MultiColumn.class));
hasMultiColumn = buildFields.stream().anyMatch(fieldDefinition -> fieldDefinition.getField().isAnnotationPresent(MultiColumn.class));
return buildFields;
}

Expand Down Expand Up @@ -366,19 +368,19 @@ private void setFormula(int i, Td td) {
if (filteredFields.isEmpty()) {
return;
}
Field field = filteredFields.get(i);
ExcelColumnMapping excelColumnMapping = excelColumnMappingMap.get(field);
FieldDefinition fieldDefinition = filteredFields.get(i);
ExcelColumnMapping excelColumnMapping = excelColumnMappingMap.get(fieldDefinition.getField());
if (excelColumnMapping != null && excelColumnMapping.formula) {
td.formula = true;
}
}

protected void setPrompt(Td td, int index) {
if (filteredFields == null || filteredFields.isEmpty()) {
if (filteredFields.isEmpty()) {
return;
}
Field field = filteredFields.get(index);
ExcelColumnMapping excelColumnMapping = excelColumnMappingMap.get(field);
FieldDefinition fieldDefinition = filteredFields.get(index);
ExcelColumnMapping excelColumnMapping = excelColumnMappingMap.get(fieldDefinition.getField());
if (excelColumnMapping != null && excelColumnMapping.promptContainer != null) {
td.promptContainer = excelColumnMapping.promptContainer;
}
Expand Down Expand Up @@ -468,22 +470,22 @@ protected void setTitles(List<String> titles) {
}
}

protected List<Field> getGroupFields(List<Field> preElectionFields, Class<?>[] groups) {
protected List<FieldDefinition> getGroupFields(List<FieldDefinition> preElectionFields, Class<?>[] groups) {
List<Class<?>> selectedGroupList = Objects.nonNull(groups) ? Arrays.stream(groups).filter(Objects::nonNull).collect(Collectors.toList()) : Collections.emptyList();
return preElectionFields.stream()
.filter(field -> (!field.isAnnotationPresent(ExcludeColumn.class) && !field.isAnnotationPresent(IgnoreColumn.class)) && ReflectUtil.isFieldSelected(selectedGroupList, field))
.filter(fieldDefinition -> (!fieldDefinition.getField().isAnnotationPresent(ExcludeColumn.class) && !fieldDefinition.getField().isAnnotationPresent(IgnoreColumn.class)) && ReflectUtil.isFieldSelected(selectedGroupList, fieldDefinition))
.sorted(ReflectUtil::sortFields)
.collect(Collectors.toList());
}

protected List<Field> getPreElectionFields(ClassFieldContainer classFieldContainer) {
protected List<FieldDefinition> getPreElectionFields(ClassFieldContainer classFieldContainer) {
if (Objects.nonNull(fieldDisplayOrder) && !fieldDisplayOrder.isEmpty()) {
this.selfAdaption();
return fieldDisplayOrder.stream()
.map(classFieldContainer::getFieldByName)
.collect(Collectors.toList());
}
List<Field> preElectionFields;
List<FieldDefinition> preElectionFields;
if (configuration.includeAllField) {
if (configuration.excludeParent) {
preElectionFields = classFieldContainer.getDeclaredFields();
Expand All @@ -493,15 +495,15 @@ protected List<Field> getPreElectionFields(ClassFieldContainer classFieldContain
} else {
if (configuration.excludeParent) {
preElectionFields = classFieldContainer.getDeclaredFields().stream()
.filter(field -> field.isAnnotationPresent(ExcelColumn.class))
.filter(fieldDefinition -> fieldDefinition.getField().isAnnotationPresent(ExcelColumn.class))
.collect(Collectors.toList());
} else {
preElectionFields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class);
}
}
if (configuration.ignoreStaticFields) {
preElectionFields = preElectionFields.stream()
.filter(field -> !Modifier.isStatic(field.getModifiers()))
.filter(fieldDefinition -> !Modifier.isStatic(fieldDefinition.getField().getModifiers()))
.collect(Collectors.toList());
}
return preElectionFields;
Expand Down Expand Up @@ -529,7 +531,7 @@ private void selfAdaption() {
* @param <T> 泛型
* @return 结果集
*/
protected <T> List<List<Pair<? extends Class, ?>>> getMultiRenderContent(T data, List<Field> sortedFields) {
protected <T> List<List<Pair<? extends Class, ?>>> getMultiRenderContent(T data, List<FieldDefinition> sortedFields) {
List<Pair<? extends Class, ?>> convertResult = this.getOriginalRenderContent(data, sortedFields);

// 获取最大长度
Expand Down Expand Up @@ -576,7 +578,7 @@ private void selfAdaption() {
* @param <T> 泛型
* @return 结果集
*/
protected <T> LinkedList<Pair<? extends Class, ?>> getOriginalRenderContent(T data, List<Field> sortedFields) {
protected <T> LinkedList<Pair<? extends Class, ?>> getOriginalRenderContent(T data, List<FieldDefinition> sortedFields) {
return sortedFields.stream()
.map(field -> {
Pair<? extends Class, Object> value = WriteConverterContext.convert(field, data, convertContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ private DefaultExcelReader(Class<T> dataType) {
ClassFieldContainer classFieldContainer = ReflectUtil.getAllFieldsOfClass(dataType);
ConfigurationUtil.parseConfiguration(classFieldContainer, readContext.convertContext.configuration);

List<Field> fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class);
fields.forEach(field -> {
ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class);
List<FieldDefinition> fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class);
fields.forEach(fieldDefinition -> {
ExcelColumn excelColumn = fieldDefinition.getField().getAnnotation(ExcelColumn.class);
if (excelColumn == null) {
return;
}
ExcelColumnMapping mapping = ExcelColumnMapping.mapping(excelColumn);
readContext.convertContext.excelColumnMappingMap.put(field, mapping);
readContext.convertContext.excelColumnMappingMap.put(fieldDefinition.getField(), mapping);
});
}
}
Expand Down Expand Up @@ -423,7 +423,7 @@ private T instanceObj(Map<Integer, FieldDefinition> fieldDefinitionMap, DataForm
return;
}
content = trim.apply(content);
readContext.reset(obj, fieldDefinition.getField(), content, row.getRowNum(), index);
readContext.reset(obj, fieldDefinition, content, row.getRowNum(), index);
ReadConverterContext.convert(obj, readContext);
});
return obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ else if ("c".equals(localName)) {
this.nextDataType = xssfDataType.NUMBER;
cellRef = attributes.getValue("r");
String cellType = attributes.getValue("t");
String cellStyleStr = attributes.getValue("s");
if ("b".equals(cellType))
nextDataType = xssfDataType.BOOLEAN;
else if ("e".equals(cellType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.github.liaochong.myexcel.core.SaxExcelReader;
import com.github.liaochong.myexcel.core.converter.ConvertContext;
import com.github.liaochong.myexcel.utils.FieldDefinition;

import java.lang.reflect.Field;

Expand All @@ -31,6 +32,8 @@ public class ReadContext<T> {

private Field field;

private FieldDefinition fieldDefinition;

private String val;

private int rowNum;
Expand All @@ -50,9 +53,10 @@ public ReadContext(ConvertContext convertContext) {
this.convertContext = convertContext;
}

public void reset(T object, Field field, String val, int rowNum, int colNum) {
public void reset(T object, FieldDefinition fieldDefinition, String val, int rowNum, int colNum) {
this.object = object;
this.field = field;
this.fieldDefinition = fieldDefinition;
this.field = fieldDefinition.getField();
this.val = val;
this.rowNum = rowNum;
this.colNum = colNum;
Expand All @@ -66,10 +70,6 @@ public T getObject() {
return this.object;
}

public Field getField() {
return this.field;
}

public String getVal() {
return this.val;
}
Expand All @@ -86,10 +86,6 @@ public void setObject(T object) {
this.object = object;
}

public void setField(Field field) {
this.field = field;
}

public void setVal(String val) {
this.val = val;
}
Expand All @@ -109,4 +105,20 @@ public Hyperlink getHyperlink() {
public void setHyperlink(Hyperlink hyperlink) {
this.hyperlink = hyperlink;
}

public FieldDefinition getFieldDefinition() {
return fieldDefinition;
}

public void setFieldDefinition(FieldDefinition fieldDefinition) {
this.fieldDefinition = fieldDefinition;
}

public Field getField() {
return field;
}

public void setField(Field field) {
this.field = field;
}
}
Loading

0 comments on commit 9ff6cc0

Please sign in to comment.