Skip to content

Commit

Permalink
Merge pull request #388 from liaochong/feature/4.3.0
Browse files Browse the repository at this point in the history
Feature/4.3.0
  • Loading branch information
liaochong authored Mar 25, 2023
2 parents 443ccff + c9e399c commit e09a514
Show file tree
Hide file tree
Showing 39 changed files with 680 additions and 292 deletions.
14 changes: 7 additions & 7 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.RC3</version>
<version>4.3.0.RC4</version>
<packaging>jar</packaging>

<name>myexcel</name>
Expand All @@ -23,18 +23,18 @@
<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.3</jsoup.version>
<jsoup.version>1.15.4</jsoup.version>
<lombok.version>1.18.22</lombok.version>
<beetl.version>3.9.0.RELEASE</beetl.version>
<beetl.version>3.15.0.RELEASE</beetl.version>
<velocity.version>2.3</velocity.version>
<freemarker.version>2.3.31</freemarker.version>
<freemarker.version>2.3.32</freemarker.version>
<groovy-templates.version>3.0.9</groovy-templates.version>
<thymeleaf.version>3.0.14.RELEASE</thymeleaf.version>
<enjoy.version>4.9.16</enjoy.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>
<commons-csv.version>1.9.0</commons-csv.version>
<commons-csv.version>1.10.0</commons-csv.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.github.liaochong.myexcel.core.annotation.ExcelColumn;
import com.github.liaochong.myexcel.core.annotation.MultiColumn;
import com.github.liaochong.myexcel.core.constant.Constants;
import com.github.liaochong.myexcel.core.context.ReadContext;
import com.github.liaochong.myexcel.core.context.RowContext;
import com.github.liaochong.myexcel.core.converter.ConvertContext;
import com.github.liaochong.myexcel.core.converter.ReadConverterContext;
import com.github.liaochong.myexcel.core.reflect.ClassFieldContainer;
Expand Down Expand Up @@ -55,13 +57,9 @@ abstract class AbstractReadHandler<T> {

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

protected SaxExcelReader.ReadConfig<T> readConfig;

private final ReadContext<T> context = new ReadContext<>();
protected final ReadContext<T> readContext = new ReadContext<>();

private final RowContext rowContext = new RowContext();

private final ConvertContext convertContext;
/**
* Row object currently being processed
*/
Expand Down Expand Up @@ -104,10 +102,10 @@ public AbstractReadHandler(boolean readCsv,
SaxExcelReader.ReadConfig<T> readConfig,
Map<CellAddress, CellAddress> mergeCellMapping) {
this.mergeCellMapping = mergeCellMapping;
convertContext = new ConvertContext(readCsv);
readContext.convertContext = new ConvertContext(readCsv);
Class<T> dataType = readConfig.dataType;
fieldDefinitionMap = ReflectUtil.getFieldDefinitionMapOfExcelColumn(dataType);
this.readConfig = readConfig;
readContext.readConfig = readConfig;
this.isMapType = dataType == Map.class;
readWithTitle = !isMapType && fieldDefinitionMap.isEmpty();
setNewInstanceFunction(dataType, isMapType);
Expand Down Expand Up @@ -158,7 +156,7 @@ private void setConfiguration(Class<T> dataType, boolean isMapType) {
return;
}
ClassFieldContainer classFieldContainer = ReflectUtil.getAllFieldsOfClass(dataType);
ConfigurationUtil.parseConfiguration(classFieldContainer, convertContext.configuration);
ConfigurationUtil.parseConfiguration(classFieldContainer, readContext.convertContext.configuration);

List<Field> fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class);
fields.forEach(field -> {
Expand All @@ -167,7 +165,7 @@ private void setConfiguration(Class<T> dataType, boolean isMapType) {
return;
}
ExcelColumnMapping mapping = ExcelColumnMapping.mapping(excelColumn);
convertContext.excelColumnMappingMap.put(field, mapping);
readContext.convertContext.excelColumnMappingMap.put(field, mapping);
});
}

Expand Down Expand Up @@ -263,16 +261,18 @@ protected void convert(Object prevObj, String value, int rowNum, int colNum, Fie
if (value == null || field == null) {
return;
}
context.reset(obj, field, value, rowNum, colNum);
ReadConverterContext.convert(prevObj, context, convertContext, readConfig.exceptionFunction);
readContext.reset(obj, field, 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) {
return;
}
context.reset(obj, field, value, rowNum, colNum);
ReadConverterContext.convert(obj, context, convertContext, readConfig.exceptionFunction);
readContext.reset(obj, field, value, rowNum, colNum);
ReadConverterContext.convert(obj, readContext);
readContext.revert();
}

protected void newRow(int rowNum, boolean newInstance) {
Expand All @@ -293,16 +293,16 @@ protected void handleField(Integer colNum, String content) {
return;
}
isBlankRow = false;
content = readConfig.trim.apply(content);
if (readConfig.rowFilter.test(currentRow)) {
content = readContext.readConfig.trim.apply(content);
if (readContext.readConfig.rowFilter.test(currentRow)) {
fieldHandler.accept(colNum, content);
} else if (readWithTitle) {
Map<Integer, String> rowMapping = titles.computeIfAbsent(currentRow.getRowNum(), rowNum -> new HashMap<>());
rowMapping.put(colNum, content);
if (titleRowNum == -1) {
// 尝试下一行是否为标题行
Row nextRow = new Row(currentRow.getRowNum() + 1);
if (readConfig.rowFilter.test(nextRow)) {
if (readContext.readConfig.rowFilter.test(nextRow)) {
titleRowNum = currentRow.getRowNum();
}
}
Expand All @@ -311,19 +311,19 @@ protected void handleField(Integer colNum, String content) {

protected void handleResult() {
if (isBlankRow) {
if (readConfig.stopReadingOnBlankRow) {
if (readContext.readConfig.stopReadingOnBlankRow) {
throw new StopReadException();
}
// 忽略空白行
if (readConfig.ignoreBlankRow) {
if (readContext.readConfig.ignoreBlankRow) {
return;
}
}
this.initFieldMap();
if (!readConfig.rowFilter.test(currentRow)) {
if (!readContext.readConfig.rowFilter.test(currentRow)) {
return;
}
if (!readConfig.beanFilter.test(obj)) {
if (!readContext.readConfig.beanFilter.test(obj)) {
return;
}
if (readWithTitle && currentRow.getRowNum() == 0) {
Expand All @@ -342,7 +342,7 @@ private void initFieldMap() {
if (currentRow.getRowNum() != titleRowNum || !fieldDefinitionMap.isEmpty()) {
return;
}
Map<String, Field> titleFieldMap = ReflectUtil.getFieldMapOfTitleExcelColumn(readConfig.dataType);
Map<String, Field> titleFieldMap = ReflectUtil.getFieldMapOfTitleExcelColumn(readContext.readConfig.dataType);
fieldDefinitionMap = new HashMap<>(titleFieldMap.size());
// 获取最大列数
List<Integer> colNums = titles.values().stream().flatMap(t -> t.keySet().stream()).collect(Collectors.toList());
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/com/github/liaochong/myexcel/core/CiConsumer.java

This file was deleted.

11 changes: 0 additions & 11 deletions src/main/java/com/github/liaochong/myexcel/core/CiFunction.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public void read() {
return;
}
long startTime = System.currentTimeMillis();
try (Reader reader = new InputStreamReader(new BOMInputStream(is), readConfig.csvCharset);
CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withDelimiter(readConfig.csvDelimiter))) {
try (Reader reader = new InputStreamReader(new BOMInputStream(is), readContext.readConfig.csvCharset);
CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withDelimiter(readContext.readConfig.csvDelimiter))) {
for (final CSVRecord record : parser) {
newRow((int) (record.getRecordNumber() - 1), true);
Iterator<String> iterator = record.stream().iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.github.liaochong.myexcel.core;

import com.github.liaochong.myexcel.core.annotation.ExcelColumn;
import com.github.liaochong.myexcel.core.context.ReadContext;
import com.github.liaochong.myexcel.core.converter.ConvertContext;
import com.github.liaochong.myexcel.core.converter.ReadConverterContext;
import com.github.liaochong.myexcel.core.reflect.ClassFieldContainer;
Expand Down Expand Up @@ -84,7 +85,7 @@ public class DefaultExcelReader<T> {

private BiFunction<Throwable, ReadContext, Boolean> exceptionFunction = (e, c) -> false;

private final ReadContext<T> context = new ReadContext<>();
private final ReadContext<T> readContext = new ReadContext<>(new ConvertContext(false));

private Map<String, XSSFPicture> xssfPicturesMap = Collections.emptyMap();

Expand All @@ -94,8 +95,6 @@ public class DefaultExcelReader<T> {

private String sheetName;

private final ConvertContext convertContext = new ConvertContext(false);

private Function<String, String> trim = v -> {
if (v == null) {
return v;
Expand All @@ -114,7 +113,7 @@ private DefaultExcelReader(Class<T> dataType) {
// 全局配置获取
if (dataType != Map.class) {
ClassFieldContainer classFieldContainer = ReflectUtil.getAllFieldsOfClass(dataType);
ConfigurationUtil.parseConfiguration(classFieldContainer, convertContext.configuration);
ConfigurationUtil.parseConfiguration(classFieldContainer, readContext.convertContext.configuration);

List<Field> fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class);
fields.forEach(field -> {
Expand All @@ -123,7 +122,7 @@ private DefaultExcelReader(Class<T> dataType) {
return;
}
ExcelColumnMapping mapping = ExcelColumnMapping.mapping(excelColumn);
convertContext.excelColumnMappingMap.put(field, mapping);
readContext.convertContext.excelColumnMappingMap.put(field, mapping);
});
}
}
Expand Down Expand Up @@ -424,8 +423,8 @@ private T instanceObj(Map<Integer, FieldDefinition> fieldDefinitionMap, DataForm
return;
}
content = trim.apply(content);
context.reset(obj, fieldDefinition.getField(), content, row.getRowNum(), index);
ReadConverterContext.convert(obj, context, convertContext, exceptionFunction);
readContext.reset(obj, fieldDefinition.getField(), content, row.getRowNum(), index);
ReadConverterContext.convert(obj, readContext);
});
return obj;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
*/
package com.github.liaochong.myexcel.core;

import com.github.liaochong.myexcel.core.context.Hyperlink;
import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.HyperlinkRecord;
import org.apache.poi.hssf.record.MergeCellsRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.util.CellAddress;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand All @@ -32,16 +34,14 @@
* @author liaochong
* @version 1.0
*/
class HSSFMergeReadHandler extends AbstractHSSFReadHandler {
class HSSFPreReadHandler extends AbstractHSSFReadHandler {

private final Map<Integer, Map<CellAddress, CellAddress>> mergeCellIndexMapping;
private final HSSFPreData hssfPreData = new HSSFPreData();

public HSSFMergeReadHandler(File file,
SaxExcelReader.ReadConfig<?> readConfig,
Map<Integer, Map<CellAddress, CellAddress>> mergeCellIndexMapping) throws IOException {
public HSSFPreReadHandler(File file,
SaxExcelReader.ReadConfig<?> readConfig) throws IOException {
this.readConfig = readConfig;
this.mergeCellIndexMapping = mergeCellIndexMapping;
this.fs = new POIFSFileSystem(new FileInputStream(file));
this.fs = new POIFSFileSystem(Files.newInputStream(file.toPath()));
}


Expand All @@ -67,7 +67,7 @@ public void processRecord(Record record) {
}
MergeCellsRecord mergeCellsRecord = (MergeCellsRecord) record;
int numAreas = mergeCellsRecord.getNumAreas();
Map<CellAddress, CellAddress> mergeCellMapping = new HashMap<>();
Map<CellAddress, CellAddress> mergeCellMapping = hssfPreData.mergeCellIndexMapping.computeIfAbsent(sheetIndex, k -> new HashMap<>());
for (int i = 0; i < numAreas; i++) {
Iterator<CellAddress> iterator = mergeCellsRecord.getAreaAt(i).iterator();
CellAddress firstCellAddress = null;
Expand All @@ -80,7 +80,11 @@ public void processRecord(Record record) {
}
}
}
mergeCellIndexMapping.put(sheetIndex, mergeCellMapping);
break;
case HyperlinkRecord.sid:
HyperlinkRecord hr = (HyperlinkRecord) record;
Map<CellAddress, Hyperlink> hyperlinkMapping = hssfPreData.hyperlinkMapping.computeIfAbsent(sheetIndex, k -> new HashMap<>());
hyperlinkMapping.put(new CellAddress(hr.getFirstRow(), hr.getLastColumn()), new Hyperlink(hr.getAddress(), hr.getLabel(), hr));
break;
default:
break;
Expand All @@ -96,4 +100,14 @@ private boolean isSelectedSheet() {
}
return readConfig.sheetIndexs.contains(sheetIndex);
}

public HSSFPreData getHssfPreData() {
return hssfPreData;
}

public static class HSSFPreData {
public Map<Integer, Map<CellAddress, CellAddress>> mergeCellIndexMapping = new HashMap<>();

public Map<Integer, Map<CellAddress, Hyperlink>> hyperlinkMapping = new HashMap<>();
}
}
Loading

0 comments on commit e09a514

Please sign in to comment.