Skip to content

Commit

Permalink
feat(excel): add excel load
Browse files Browse the repository at this point in the history
  • Loading branch information
pipinet committed Jan 8, 2024
1 parent f0b87bc commit adf52a3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
35 changes: 26 additions & 9 deletions excel/src/main/java/com/qwlabs/excel/DataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.common.collect.Maps;
import com.qwlabs.lang.InputStreams;
import org.apache.commons.compress.utils.Lists;
import org.apache.logging.log4j.util.Strings;

import java.io.InputStream;
import java.util.List;
Expand All @@ -22,18 +23,26 @@ protected DataReader(InputStream inputStream) {
public List<Map<String, String>> read(SheetReadOptions options,
Map<String, String> headMapping,
int headRowIndex) {
return read(options, headMapping, headRowIndex, headRowIndex + 1);
return read(options, headMapping, headRowIndex, headRowIndex + 1, null);
}

public List<Map<String, String>> read(SheetReadOptions options,
Map<String, String> headMapping,
int headRowIndex,
String rowField) {
return read(options, headMapping, headRowIndex, headRowIndex + 1, rowField);
}

public List<Map<String, String>> read(SheetReadOptions options,
Map<String, String> headMapping,
int headRowStartIndex,
int dataRowStartIndex) {
int dataRowStartIndex,
String rowField) {
if (headRowStartIndex >= dataRowStartIndex) {
throw ExcelMessages.INSTANCE.conflictHeadAndDataRowIndex(headRowStartIndex, dataRowStartIndex);
}
InputStreams.reset(inputStream);
var listener = new Listener(headMapping, headRowStartIndex, dataRowStartIndex);
var listener = new Listener(headMapping, headRowStartIndex, dataRowStartIndex, rowField);
options.config(EasyExcel.read(inputStream, listener))
.headRowNumber(0)
.doRead();
Expand All @@ -46,15 +55,19 @@ private static final class Listener extends AnalysisEventListener<Map<Integer, S
private final List<Map<String, String>> data;
private final int headRowStartIndex;
private final int dataRowStartIndex;
private final String rowField;

public Listener(Map<String, String> headMapping,
int headRowStartIndex,
int dataRowStartIndex) {
int dataRowStartIndex,
String rowField) {
this.headMapping = headMapping;
this.headRowStartIndex = headRowStartIndex;
this.dataRowStartIndex = dataRowStartIndex;
this.rowField = rowField;
this.indexFields = Maps.newHashMap();
this.data = Lists.newArrayList();

}

@Override
Expand All @@ -63,18 +76,22 @@ public void invoke(Map<Integer, String> data, AnalysisContext context) {
if (currentRowIndex == headRowStartIndex) {
this.indexFields.putAll(ExcelHelper.lookupHeaders(data, headMapping));
} else if (currentRowIndex >= dataRowStartIndex) {
this.data.add(buildRowData(data));
this.data.add(buildRowData(data, context));
}
}

private Map<String, String> buildRowData(Map<Integer, String> data) {
Map<String, String> rowData = Maps.newLinkedHashMapWithExpectedSize(data.size());
data.forEach((rowIndex, value) -> {
var field = this.indexFields.get(rowIndex);
private Map<String, String> buildRowData(Map<Integer, String> data, AnalysisContext context) {
var hasRowField = Strings.isNotEmpty(rowField);
Map<String, String> rowData = Maps.newLinkedHashMapWithExpectedSize(data.size() + (hasRowField ? 1 : 0));
data.forEach((columnIndex, value) -> {
var field = this.indexFields.get(columnIndex);
if (Objects.nonNull(field)) {
rowData.putIfAbsent(field, value);
}
});
if (hasRowField) {
rowData.put(rowField, String.valueOf(ExcelHelper.toNaturalSequence(context.readRowHolder().getRowIndex())));
}
return rowData;
}

Expand Down
8 changes: 8 additions & 0 deletions excel/src/main/java/com/qwlabs/excel/ExcelHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ public final class ExcelHelper {
private ExcelHelper() {
}

public static int toIndex(int rowOrColumn) {
return rowOrColumn - 1;
}

public static int toNaturalSequence(int index) {
return index + 1;
}

public static Map<Integer, String> cleanup(Map<Integer, String> data) {
return cleanup(data, (index, value) -> Objects.nonNull(value));
}
Expand Down
32 changes: 31 additions & 1 deletion excel/src/test/java/com/qwlabs/excel/DataReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class DataReaderTest {

@Test
void should_read() {
void should_read_with_out_row() {
var inputStream = read("data-reader.xlsx");
var headMapping = Map.of(
"所属航司", "airline",
Expand All @@ -38,6 +38,36 @@ void should_read() {
assertThat(row.get("发动机序列号"), is("我是发动机序列号"));
}

@Test
void should_read_with_row() {
var inputStream = read("data-reader.xlsx");
var headMapping = Map.of(
"所属航司", "airline",
"机队名称", "feet",
"飞机号", "tailNo",
"飞机型号", "airplaneModel"
);
var data = Excels.dataReader(inputStream).read(
SheetReadOptions.builder().sheetNo(0).build(),
headMapping,
0,
"_row"
);

assertThat(data.size(), is(1));
var row = data.get(0);
assertThat(row.size(), is(17));
assertThat(row.get("_row"), is("2"));
assertThat(row.get("airline"), is("我是航司"));
assertThat(row.get("feet"), is("我是机队名称"));
assertThat(row.get("tailNo"), is("我是飞机号"));
assertThat(row.get("airplaneModel"), is("我是飞机型号"));
assertThat(row.get("新增字段1"), is("我是新增字段1"));
assertThat(row.get("新增字段2"), is("我是新增字段2"));
assertThat(row.get("新增字段3"), is("我是新增字段3"));
assertThat(row.get("发动机序列号"), is("我是发动机序列号"));
}

private InputStream read(String path) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
}
Expand Down

0 comments on commit adf52a3

Please sign in to comment.