diff --git a/README.md b/README.md index 14dc9d5..25e4723 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Add maven dependency io.github.biezhi excel-plus - 0.0.1 + 0.0.2 ``` diff --git a/docs/README.md b/docs/README.md index 1c5d476..564a763 100644 --- a/docs/README.md +++ b/docs/README.md @@ -35,7 +35,7 @@ io.github.biezhi excel-plus - 0.0.1 + 0.0.2 ``` @@ -226,6 +226,13 @@ excelPlus.export(Exporter.create(cardSecrets).byTemplate("tpl.xls")).writeAsFile # 版本更新 +v0.0.2 + +- 1. 添加验证行记录各项指标 +- 2. 区分验证成功和失败行 +- 3. 可配置是否添加到验证成功行 +- 4. 取消 `columnName` 选项为必选 + v0.0.1 - 发布第一个版本 diff --git a/pom.xml b/pom.xml index 720faf4..97b9718 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.biezhi excel-plus - 0.0.1 + 0.0.2 excel-plus https://biezhi.github.io/excel-plus excel read and write framework diff --git a/src/main/java/io/github/biezhi/excel/plus/annotation/ExcelField.java b/src/main/java/io/github/biezhi/excel/plus/annotation/ExcelField.java index 408aaf9..a68fb67 100644 --- a/src/main/java/io/github/biezhi/excel/plus/annotation/ExcelField.java +++ b/src/main/java/io/github/biezhi/excel/plus/annotation/ExcelField.java @@ -32,7 +32,7 @@ * * @return excel header column title */ - String columnName(); + String columnName() default ""; /** * When a field is a Date or a Time type, diff --git a/src/main/java/io/github/biezhi/excel/plus/converter/Converter.java b/src/main/java/io/github/biezhi/excel/plus/converter/Converter.java index 62d2430..4b90a7f 100644 --- a/src/main/java/io/github/biezhi/excel/plus/converter/Converter.java +++ b/src/main/java/io/github/biezhi/excel/plus/converter/Converter.java @@ -22,6 +22,8 @@ public interface Converter { * @param value * @return */ - T read(String value); + default T read(String value) { + return null; + } } \ No newline at end of file diff --git a/src/main/java/io/github/biezhi/excel/plus/reader/ExcelReader.java b/src/main/java/io/github/biezhi/excel/plus/reader/ExcelReader.java index e726eaa..06587c3 100644 --- a/src/main/java/io/github/biezhi/excel/plus/reader/ExcelReader.java +++ b/src/main/java/io/github/biezhi/excel/plus/reader/ExcelReader.java @@ -28,7 +28,7 @@ public class ExcelReader { private Class type; private Predicate filter; private Function validFunction; - private int startRowIndex = 1; + private int startRowIndex = 1; public ExcelReader(Workbook workbook, Class type) { this.workbook = workbook; @@ -38,28 +38,28 @@ public ExcelReader(Workbook workbook, Class type) { public ExcelResult asResult() { ExcelResult excelResult = new ExcelResult<>(); - Stream> stream = this.asStream(); + List> collect = this.asStream().collect(Collectors.toList()); + Stream listStream = collect.stream().map(Pair::getV); - if (null != this.validFunction) { + if (null != this.filter) { + listStream = listStream.filter(this.filter); + } + + List rows = listStream.collect(Collectors.toList()); + excelResult.rows(rows); - stream = stream.filter(pair -> { + if (null != this.validFunction) { + collect.forEach(pair -> { Integer rowNum = pair.getK(); T item = pair.getV(); ValidRow validRow = validFunction.apply(item); - if (!validRow.valid()) { - validRow.rowNum(rowNum); - excelResult.addError(validRow); - return false; + validRow.rowNum(rowNum); + if (validRow.isValid() && validRow.allowAdd()) { + excelResult.addSuccessRow(item); } - return true; + excelResult.addValidRow(validRow); }); } - - Stream listStream = stream.map(Pair::getV); - if (null != this.filter) { - listStream = listStream.filter(this.filter); - } - excelResult.rows(listStream.collect(Collectors.toList())); return excelResult; } diff --git a/src/main/java/io/github/biezhi/excel/plus/reader/ExcelResult.java b/src/main/java/io/github/biezhi/excel/plus/reader/ExcelResult.java index a4acc6f..ddca612 100644 --- a/src/main/java/io/github/biezhi/excel/plus/reader/ExcelResult.java +++ b/src/main/java/io/github/biezhi/excel/plus/reader/ExcelResult.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** * Excel valid result @@ -11,27 +12,61 @@ */ public class ExcelResult { - private List errors = new ArrayList<>(); - private List rows; + private List validRows = new ArrayList<>(); + private List successRows = new ArrayList<>(); + private List rows = new ArrayList<>(); + private int totalRows; + private Boolean isValid = true; - public boolean isValid() { - return errors.size() == 0; + public void addValidRow(ValidRow validRow) { + validRows.add(validRow); + if (validRow.isValidFail()) { + isValid = false; + } } - public void addError(ValidRow validRow) { - errors.add(validRow); + public void addSuccessRow(T item) { + successRows.add(item); } public void rows(List rows) { this.rows = rows; + this.totalRows = rows.size(); } public List rows() { return rows; } + public List successRows() { + return successRows; + } + + public String popMsg() { + List errors = errors(); + if (null != errors && errors.size() > 0) { + return errors.get(0).msg(); + } + return null; + } + public List errors() { - return errors; + return validRows.stream() + .filter(ValidRow::isValidFail) + .filter(ValidRow::hasMsg) + .collect(Collectors.toList()); + } + + public int totalRows() { + return totalRows; + } + + public int counter(String key) { + return validRows.stream().map(v -> v.counter(key)).reduce(0, (x, y) -> x + y); + } + + public boolean isValid() { + return isValid; } } diff --git a/src/main/java/io/github/biezhi/excel/plus/reader/ValidRow.java b/src/main/java/io/github/biezhi/excel/plus/reader/ValidRow.java index 94e08f9..f68832c 100644 --- a/src/main/java/io/github/biezhi/excel/plus/reader/ValidRow.java +++ b/src/main/java/io/github/biezhi/excel/plus/reader/ValidRow.java @@ -1,5 +1,8 @@ package io.github.biezhi.excel.plus.reader; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + /** * Verify that the row data is passed and the row number * and error messages are stored internally. @@ -11,25 +14,45 @@ public class ValidRow { private String msg; private boolean valid; + private boolean allowAdd; private int rowNum; + private Map counter = new ConcurrentHashMap<>(); - public ValidRow(boolean valid, String msg) { + public ValidRow(boolean valid, boolean allowAdd, String msg) { this.valid = valid; + this.allowAdd = allowAdd; this.msg = msg; } public static ValidRow ok() { - return new ValidRow(true, null); + return new ValidRow(true, true, null); } public static ValidRow fail(String msg) { - return new ValidRow(false, msg); + return new ValidRow(false, false, msg); } - boolean valid() { + boolean isValid() { return valid; } + boolean isValidFail() { + return !valid; + } + + boolean hasMsg() { + return null != msg; + } + + boolean allowAdd() { + return this.allowAdd; + } + + public ValidRow allowAdd(boolean allowAdd) { + this.allowAdd = allowAdd; + return this; + } + public String msg() { return this.msg; } @@ -42,6 +65,36 @@ public int rowNum() { return this.rowNum; } + public ValidRow addCounter(String key) { + return this.addCounter(key, 1); + } + + public ValidRow addCounter(String key, int count) { + Integer currentCount = counter.get(key); + if (null == currentCount) { + currentCount = count; + } + counter.put(key, currentCount); + return this; + } + + public ValidRow subtract(String key) { + return this.subtract(key, 1); + } + + public ValidRow subtract(String key, int count) { + Integer currentCount = counter.get(key); + if (null != currentCount) { + currentCount = currentCount - count; + counter.put(key, currentCount); + } + return this; + } + + public int counter(String key) { + return counter.getOrDefault(key, 0); + } + @Override public String toString() { if (valid) { diff --git a/src/main/java/io/github/biezhi/excel/plus/utils/ExcelUtils.java b/src/main/java/io/github/biezhi/excel/plus/utils/ExcelUtils.java index 8aa4542..5175c35 100644 --- a/src/main/java/io/github/biezhi/excel/plus/utils/ExcelUtils.java +++ b/src/main/java/io/github/biezhi/excel/plus/utils/ExcelUtils.java @@ -140,6 +140,9 @@ public static void writeToField(Object item, int col, String value) { } private static Object asObject(Field field, String value) { + if(null == value || "".equals(value)){ + return null; + } ExcelField excelField = field.getAnnotation(ExcelField.class); if (null != excelField && !excelField.convertType().equals(EmptyConverter.class)) { Converter converter = newInstance(excelField.convertType()); diff --git a/src/test/java/io/github/biezhi/excel/plus/Examples.java b/src/test/java/io/github/biezhi/excel/plus/Examples.java index 9a291df..63dbcee 100644 --- a/src/test/java/io/github/biezhi/excel/plus/Examples.java +++ b/src/test/java/io/github/biezhi/excel/plus/Examples.java @@ -45,6 +45,7 @@ public void testReadFilter() throws ExcelException { @Test public void testReadValid() throws ExcelException { ExcelResult excelResult = excelPlus.read(new File("卡密列表.xls"), CardSecret.class) + .startRow(2) .valid(cardSecret -> { BigDecimal amount = cardSecret.getAmount(); if (amount.doubleValue() < 20) {