Skip to content

Commit

Permalink
feat: add excel
Browse files Browse the repository at this point in the history
  • Loading branch information
pipinet committed Jan 3, 2024
1 parent c30b6b5 commit 988a420
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 40 deletions.
2 changes: 1 addition & 1 deletion excel/src/main/java/com/qwlabs/excel/DataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Listener(Map<String, String> headMapping) {

@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
this.indexFields.putAll(ExcelHeaders.lookupHeaders(headMap, headMapping));
this.indexFields.putAll(ExcelHelper.lookupHeaders(headMap, headMapping));
}

@Override
Expand Down
29 changes: 0 additions & 29 deletions excel/src/main/java/com/qwlabs/excel/ExcelHeaders.java

This file was deleted.

43 changes: 43 additions & 0 deletions excel/src/main/java/com/qwlabs/excel/ExcelHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.qwlabs.excel;

import com.google.common.collect.Maps;
import jakarta.validation.constraints.NotNull;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiPredicate;

public final class ExcelHelper {
private ExcelHelper() {
}

public static Map<Integer, String> cleanup(Map<Integer, String> data) {
return cleanup(data, (index, value) -> Objects.nonNull(value));
}

public static Map<Integer, String> cleanup(Map<Integer, String> data,
BiPredicate<Integer, String> predicate) {
Map<Integer, String> cleanup = Maps.newHashMap();
Optional.ofNullable(data).orElseGet(Map::of)
.forEach((index, value) -> {
if (predicate.test(index, value)) {
cleanup.put(index, value);
}
});
return cleanup;
}

public static Map<Integer, String> lookupHeaders(@NotNull Map<Integer, String> headerData,
@NotNull Map<String, String> lookups) {
Map<Integer, String> headers = Maps.newHashMapWithExpectedSize(headerData.size());
cleanup(headerData).forEach((index, title) -> {
var newTitle = lookups.get(title);
if (Objects.nonNull(newTitle)) {
headers.put(index, newTitle);
}
});
headerData.forEach(headers::putIfAbsent);
return headers;
}
}
5 changes: 2 additions & 3 deletions excel/src/main/java/com/qwlabs/excel/SingleRowReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import java.io.InputStream;
import java.util.Map;
import java.util.Optional;

public class SingleRowReader {
private final InputStream inputStream;
Expand All @@ -22,11 +21,12 @@ public Map<Integer, String> read(SheetReadOptions options, int rowIndex) {
options.config(EasyExcel.read(inputStream, listener))
.headRowNumber(rowIndex + 1)
.doRead();
return listener.getData();
return ExcelHelper.cleanup(listener.getData());
}

private static final class Listener extends AnalysisEventListener<Map<Integer, String>> {
private Map<Integer, String> data;

@Override
public boolean hasNext(AnalysisContext context) {
return false;
Expand All @@ -44,7 +44,6 @@ public void invoke(Map<Integer, String> data, AnalysisContext context) {

@Override
public void doAfterAllAnalysed(AnalysisContext context) {
this.data = Optional.ofNullable(this.data).orElseGet(Map::of);
}

public Map<Integer, String> getData() {
Expand Down
14 changes: 7 additions & 7 deletions excel/src/test/java/com/qwlabs/excel/SingleRowReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void should_read_twice() {
SheetReadOptions.builder().sheetNo(0).build(),
1
);

valid(data1);
valid(data2);
}
Expand All @@ -46,12 +46,12 @@ private void valid(Map<Integer, String> data) {
assertThat(data.get(4), is("发动机型号"));
assertThat(data.get(5), is("发动机序列号"));
assertThat(data.get(6), is("航站"));
assertThat(data.get(7), is("ATA章节"));
assertThat(data.get(8), is("故障描述"));
assertThat(data.get(9), is("处置措施"));
assertThat(data.get(10), is("故障发生时间"));
assertThat(data.get(11), is("故障记录时间"));
assertThat(data.get(12), is("故障关闭时间"));
assertThat(data.get(8), is("ATA章节"));
assertThat(data.get(9), is("故障描述"));
assertThat(data.get(10), is("处置措施"));
assertThat(data.get(11), is("故障发生时间"));
assertThat(data.get(12), is("故障记录时间"));
assertThat(data.get(13), is("故障关闭时间"));
}

private InputStream read(String path) {
Expand Down
Binary file modified excel/src/test/resources/single-row-reader.xlsx
Binary file not shown.

0 comments on commit 988a420

Please sign in to comment.