Skip to content
This repository was archived by the owner on Apr 11, 2025. It is now read-only.

Commit 91c3a1a

Browse files
committed
🔖 release 0.0.2
1 parent e23a39c commit 91c3a1a

File tree

10 files changed

+132
-31
lines changed

10 files changed

+132
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Add maven dependency
2727
<dependency>
2828
<groupId>io.github.biezhi</groupId>
2929
<artifactId>excel-plus</artifactId>
30-
<version>0.0.1</version>
30+
<version>0.0.2</version>
3131
</dependency>
3232
```
3333

docs/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<dependency>
3636
<groupId>io.github.biezhi</groupId>
3737
<artifactId>excel-plus</artifactId>
38-
<version>0.0.1</version>
38+
<version>0.0.2</version>
3939
</dependency>
4040
```
4141

@@ -226,6 +226,13 @@ excelPlus.export(Exporter.create(cardSecrets).byTemplate("tpl.xls")).writeAsFile
226226

227227
# 版本更新
228228

229+
<b>v0.0.2</b>
230+
231+
- 1. 添加验证行记录各项指标
232+
- 2. 区分验证成功和失败行
233+
- 3. 可配置是否添加到验证成功行
234+
- 4. 取消 `columnName` 选项为必选
235+
229236
<b>v0.0.1</b>
230237

231238
- 发布第一个版本

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.biezhi</groupId>
88
<artifactId>excel-plus</artifactId>
9-
<version>0.0.1</version>
9+
<version>0.0.2</version>
1010
<name>excel-plus</name>
1111
<url>https://biezhi.github.io/excel-plus</url>
1212
<description>excel read and write framework</description>

src/main/java/io/github/biezhi/excel/plus/annotation/ExcelField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*
3333
* @return excel header column title
3434
*/
35-
String columnName();
35+
String columnName() default "";
3636

3737
/**
3838
* When a field is a Date or a Time type,

src/main/java/io/github/biezhi/excel/plus/converter/Converter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public interface Converter<T> {
2222
* @param value
2323
* @return
2424
*/
25-
T read(String value);
25+
default T read(String value) {
26+
return null;
27+
}
2628

2729
}

src/main/java/io/github/biezhi/excel/plus/reader/ExcelReader.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class ExcelReader<T> {
2828
private Class<T> type;
2929
private Predicate<T> filter;
3030
private Function<T, ValidRow> validFunction;
31-
private int startRowIndex = 1;
31+
private int startRowIndex = 1;
3232

3333
public ExcelReader(Workbook workbook, Class<T> type) {
3434
this.workbook = workbook;
@@ -38,28 +38,28 @@ public ExcelReader(Workbook workbook, Class<T> type) {
3838
public ExcelResult<T> asResult() {
3939
ExcelResult<T> excelResult = new ExcelResult<>();
4040

41-
Stream<Pair<Integer, T>> stream = this.asStream();
41+
List<Pair<Integer, T>> collect = this.asStream().collect(Collectors.toList());
42+
Stream<T> listStream = collect.stream().map(Pair::getV);
4243

43-
if (null != this.validFunction) {
44+
if (null != this.filter) {
45+
listStream = listStream.filter(this.filter);
46+
}
47+
48+
List<T> rows = listStream.collect(Collectors.toList());
49+
excelResult.rows(rows);
4450

45-
stream = stream.filter(pair -> {
51+
if (null != this.validFunction) {
52+
collect.forEach(pair -> {
4653
Integer rowNum = pair.getK();
4754
T item = pair.getV();
4855
ValidRow validRow = validFunction.apply(item);
49-
if (!validRow.valid()) {
50-
validRow.rowNum(rowNum);
51-
excelResult.addError(validRow);
52-
return false;
56+
validRow.rowNum(rowNum);
57+
if (validRow.isValid() && validRow.allowAdd()) {
58+
excelResult.addSuccessRow(item);
5359
}
54-
return true;
60+
excelResult.addValidRow(validRow);
5561
});
5662
}
57-
58-
Stream<T> listStream = stream.map(Pair::getV);
59-
if (null != this.filter) {
60-
listStream = listStream.filter(this.filter);
61-
}
62-
excelResult.rows(listStream.collect(Collectors.toList()));
6363
return excelResult;
6464
}
6565

src/main/java/io/github/biezhi/excel/plus/reader/ExcelResult.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.stream.Collectors;
56

67
/**
78
* Excel valid result
@@ -11,27 +12,61 @@
1112
*/
1213
public class ExcelResult<T> {
1314

14-
private List<ValidRow> errors = new ArrayList<>();
15-
private List<T> rows;
15+
private List<ValidRow> validRows = new ArrayList<>();
16+
private List<T> successRows = new ArrayList<>();
17+
private List<T> rows = new ArrayList<>();
18+
private int totalRows;
19+
private Boolean isValid = true;
1620

17-
public boolean isValid() {
18-
return errors.size() == 0;
21+
public void addValidRow(ValidRow validRow) {
22+
validRows.add(validRow);
23+
if (validRow.isValidFail()) {
24+
isValid = false;
25+
}
1926
}
2027

21-
public void addError(ValidRow validRow) {
22-
errors.add(validRow);
28+
public void addSuccessRow(T item) {
29+
successRows.add(item);
2330
}
2431

2532
public void rows(List<T> rows) {
2633
this.rows = rows;
34+
this.totalRows = rows.size();
2735
}
2836

2937
public List<T> rows() {
3038
return rows;
3139
}
3240

41+
public List<T> successRows() {
42+
return successRows;
43+
}
44+
45+
public String popMsg() {
46+
List<ValidRow> errors = errors();
47+
if (null != errors && errors.size() > 0) {
48+
return errors.get(0).msg();
49+
}
50+
return null;
51+
}
52+
3353
public List<ValidRow> errors() {
34-
return errors;
54+
return validRows.stream()
55+
.filter(ValidRow::isValidFail)
56+
.filter(ValidRow::hasMsg)
57+
.collect(Collectors.toList());
58+
}
59+
60+
public int totalRows() {
61+
return totalRows;
62+
}
63+
64+
public int counter(String key) {
65+
return validRows.stream().map(v -> v.counter(key)).reduce(0, (x, y) -> x + y);
66+
}
67+
68+
public boolean isValid() {
69+
return isValid;
3570
}
3671

3772
}

src/main/java/io/github/biezhi/excel/plus/reader/ValidRow.java

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package io.github.biezhi.excel.plus.reader;
22

3+
import java.util.Map;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
36
/**
47
* Verify that the row data is passed and the row number
58
* and error messages are stored internally.
@@ -11,25 +14,45 @@ public class ValidRow {
1114

1215
private String msg;
1316
private boolean valid;
17+
private boolean allowAdd;
1418
private int rowNum;
19+
private Map<String, Integer> counter = new ConcurrentHashMap<>();
1520

16-
public ValidRow(boolean valid, String msg) {
21+
public ValidRow(boolean valid, boolean allowAdd, String msg) {
1722
this.valid = valid;
23+
this.allowAdd = allowAdd;
1824
this.msg = msg;
1925
}
2026

2127
public static ValidRow ok() {
22-
return new ValidRow(true, null);
28+
return new ValidRow(true, true, null);
2329
}
2430

2531
public static ValidRow fail(String msg) {
26-
return new ValidRow(false, msg);
32+
return new ValidRow(false, false, msg);
2733
}
2834

29-
boolean valid() {
35+
boolean isValid() {
3036
return valid;
3137
}
3238

39+
boolean isValidFail() {
40+
return !valid;
41+
}
42+
43+
boolean hasMsg() {
44+
return null != msg;
45+
}
46+
47+
boolean allowAdd() {
48+
return this.allowAdd;
49+
}
50+
51+
public ValidRow allowAdd(boolean allowAdd) {
52+
this.allowAdd = allowAdd;
53+
return this;
54+
}
55+
3356
public String msg() {
3457
return this.msg;
3558
}
@@ -42,6 +65,36 @@ public int rowNum() {
4265
return this.rowNum;
4366
}
4467

68+
public ValidRow addCounter(String key) {
69+
return this.addCounter(key, 1);
70+
}
71+
72+
public ValidRow addCounter(String key, int count) {
73+
Integer currentCount = counter.get(key);
74+
if (null == currentCount) {
75+
currentCount = count;
76+
}
77+
counter.put(key, currentCount);
78+
return this;
79+
}
80+
81+
public ValidRow subtract(String key) {
82+
return this.subtract(key, 1);
83+
}
84+
85+
public ValidRow subtract(String key, int count) {
86+
Integer currentCount = counter.get(key);
87+
if (null != currentCount) {
88+
currentCount = currentCount - count;
89+
counter.put(key, currentCount);
90+
}
91+
return this;
92+
}
93+
94+
public int counter(String key) {
95+
return counter.getOrDefault(key, 0);
96+
}
97+
4598
@Override
4699
public String toString() {
47100
if (valid) {

src/main/java/io/github/biezhi/excel/plus/utils/ExcelUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ public static void writeToField(Object item, int col, String value) {
140140
}
141141

142142
private static Object asObject(Field field, String value) {
143+
if(null == value || "".equals(value)){
144+
return null;
145+
}
143146
ExcelField excelField = field.getAnnotation(ExcelField.class);
144147
if (null != excelField && !excelField.convertType().equals(EmptyConverter.class)) {
145148
Converter converter = newInstance(excelField.convertType());

src/test/java/io/github/biezhi/excel/plus/Examples.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public void testReadFilter() throws ExcelException {
4545
@Test
4646
public void testReadValid() throws ExcelException {
4747
ExcelResult<CardSecret> excelResult = excelPlus.read(new File("卡密列表.xls"), CardSecret.class)
48+
.startRow(2)
4849
.valid(cardSecret -> {
4950
BigDecimal amount = cardSecret.getAmount();
5051
if (amount.doubleValue() < 20) {

0 commit comments

Comments
 (0)