Skip to content

Commit

Permalink
Merge pull request #61 from liaochong/feature/2.3.0.release
Browse files Browse the repository at this point in the history
Feature/2.3.0.release
  • Loading branch information
liaochong authored May 18, 2019
2 parents 42ae0b1 + 5354f6b commit ffe9eb0
Show file tree
Hide file tree
Showing 28 changed files with 1,059 additions and 241 deletions.
7 changes: 6 additions & 1 deletion example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>com.github.liaochong</groupId>
<artifactId>myexcel</artifactId>
<version>2.2.1</version>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
Expand All @@ -60,6 +60,11 @@
<artifactId>beetl</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2019 liaochong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.liaochong.example.controller;

import com.github.liaochong.example.pojo.ArtCrowd;
import com.github.liaochong.myexcel.core.DefaultExcelReader;
import com.github.liaochong.myexcel.core.SaxExcelReader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
* sax读取excel
*
* @author liaochong
* @version 1.0
*/
@RestController
public class SaxExcelReaderExampleController {

@GetMapping("/sax/excel/read/example")
public List<ArtCrowd> read() throws Exception {
URL htmlToExcelEampleURL = this.getClass().getResource("/templates/read_example.xlsx");
Path path = Paths.get(htmlToExcelEampleURL.toURI());

List<ArtCrowd> result = SaxExcelReader.of(ArtCrowd.class).beanFilter(bean -> bean.isDance()).rowFilter(row -> row.getRowNum() > 0).read(path.toFile());
return result;
}

@GetMapping("/sax/excel/readThen/example")
public List<ArtCrowd> readThen() throws Exception {
URL htmlToExcelEampleURL = this.getClass().getResource("/templates/read_example.xlsx");
Path path = Paths.get(htmlToExcelEampleURL.toURI());

List<ArtCrowd> container = new ArrayList<>();
SaxExcelReader.of(ArtCrowd.class).sheet(0).rowFilter(row -> row.getRowNum() > 0)
.readThen(path.toFile(), d -> {
container.add(d);
System.out.println(d.getName());
});
return container;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
@ExcelTable(sheetName = "艺术生", rowAccessWindowSize = 100, useFieldNameAsTitle = true)
public class ArtCrowd extends People {

@ExcelColumn(order = 5, index = 3, width = 20)
@ExcelColumn(order = 3, index = 3, width = 20)
private String paintingLevel;

@ExcelColumn(order = 4, title = "是否会跳舞", width = 9, groups = {People.class, String.class}, index = 4)
private boolean dance;

@ExcelColumn(order = 6, title = "考核时间", width = 10, dateFormatPattern = "yyyy-MM-dd HH:mm:ss", groups = {People.class, String.class})
@ExcelColumn(order = 5, title = "考核时间", width = 10, dateFormatPattern = "yyyy-MM-dd HH:mm:ss", groups = {People.class, String.class}, index = 5)
private LocalDateTime assessmentTime;

@ExcelColumn(order = 7, defaultValue = "---")
@ExcelColumn(order = 6, defaultValue = "---")
private String hobby;

public String getPaintingLevel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
*/
public class People {

@ExcelColumn(order = 1, title = "姓名", index = 0)
@ExcelColumn(order = 0, title = "姓名", index = 0)
private String name;

@ExcelColumn(order = 2, title = "年龄")
@ExcelColumn(order = 1, title = "年龄", index = 1)
private Integer age;

@ExcelColumn(order = 3, title = "性别")
@ExcelColumn(order = 2, title = "性别", index = 2)
private String gender;

public String getName() {
Expand Down
Binary file modified example/src/main/resources/templates/read_example.xlsx
Binary file not shown.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>com.github.liaochong</groupId>
<artifactId>myexcel</artifactId>
<version>2.2.1</version>
<version>2.3.0</version>
<packaging>jar</packaging>

<name>myexcel</name>
Expand All @@ -22,8 +22,8 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<poi.version>4.1.0</poi.version>
<jsoup.version>1.11.3</jsoup.version>
<lombok.version>1.18.6</lombok.version>
<jsoup.version>1.12.1</jsoup.version>
<lombok.version>1.18.8</lombok.version>
<slf4j-api.version>1.7.26</slf4j-api.version>
<beetl.version>2.9.9</beetl.version>
<freemarker.version>2.3.23</freemarker.version>
Expand Down Expand Up @@ -137,7 +137,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
*/
package com.github.liaochong.myexcel.core;

import com.github.liaochong.myexcel.core.annotation.ExcelColumn;
import com.github.liaochong.myexcel.core.container.ParallelContainer;
import com.github.liaochong.myexcel.core.converter.ReadConverterContext;
import com.github.liaochong.myexcel.core.reflect.ClassFieldContainer;
import com.github.liaochong.myexcel.utils.ReflectUtil;
import com.github.liaochong.myexcel.utils.StringUtil;
import lombok.NonNull;
Expand All @@ -35,7 +33,6 @@
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand All @@ -60,6 +57,8 @@ public class DefaultExcelReader<T> {

private Predicate<Row> rowFilter = row -> true;

private Predicate<T> beanFilter = bean -> true;

private boolean parallelRead;

private DefaultExcelReader(Class<T> dataType) {
Expand All @@ -84,6 +83,11 @@ public DefaultExcelReader<T> rowFilter(@NonNull Predicate<Row> rowFilter) {
return this;
}

public DefaultExcelReader<T> beanFilter(@NonNull Predicate<T> beanFilter) {
this.beanFilter = beanFilter;
return this;
}

public DefaultExcelReader<T> parallelRead() {
this.parallelRead = true;
return this;
Expand All @@ -94,7 +98,7 @@ public List<T> read(@NonNull InputStream fileInputStream) throws Exception {
}

public List<T> read(@NonNull InputStream fileInputStream, String password) throws Exception {
Map<Integer, Field> fieldMap = getFieldMap();
Map<Integer, Field> fieldMap = ReflectUtil.getFieldMapOfExcelColumn(dataType);
if (fieldMap.isEmpty()) {
return Collections.emptyList();
}
Expand All @@ -110,7 +114,7 @@ public List<T> read(@NonNull File file, String password) throws Exception {
if (!file.getName().endsWith(".xlsx") && !file.getName().endsWith(".xls")) {
throw new IllegalArgumentException("Support only. xls and. xlsx suffix files");
}
Map<Integer, Field> fieldMap = getFieldMap();
Map<Integer, Field> fieldMap = ReflectUtil.getFieldMapOfExcelColumn(dataType);
if (fieldMap.isEmpty()) {
return Collections.emptyList();
}
Expand All @@ -123,7 +127,7 @@ public void readThen(@NonNull InputStream fileInputStream, Consumer<T> consumer)
}

public void readThen(@NonNull InputStream fileInputStream, String password, Consumer<T> consumer) throws Exception {
Map<Integer, Field> fieldMap = getFieldMap();
Map<Integer, Field> fieldMap = ReflectUtil.getFieldMapOfExcelColumn(dataType);
if (fieldMap.isEmpty()) {
return;
}
Expand All @@ -140,7 +144,7 @@ public void readThen(@NonNull File file, String password, Consumer<T> consumer)
if (!file.getName().endsWith(".xlsx") && !file.getName().endsWith(".xls")) {
throw new IllegalArgumentException("Support only. xls and. xlsx suffix files");
}
Map<Integer, Field> fieldMap = getFieldMap();
Map<Integer, Field> fieldMap = ReflectUtil.getFieldMapOfExcelColumn(dataType);
if (fieldMap.isEmpty()) {
return;
}
Expand Down Expand Up @@ -168,28 +172,6 @@ private Sheet getSheetOfFile(@NonNull File file, String password) throws IOExcep
return wb.getSheetAt(sheetIndex);
}

private Map<Integer, Field> getFieldMap() {
ClassFieldContainer classFieldContainer = ReflectUtil.getAllFieldsOfClass(dataType);
List<Field> fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class);
if (fields.isEmpty()) {
throw new IllegalStateException("There is no field with @ExcelColumn");
}
Map<Integer, Field> fieldMap = new HashMap<>(fields.size());
for (Field field : fields) {
ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class);
int index = excelColumn.index();
if (index < 0) {
continue;
}
Field f = fieldMap.get(index);
if (Objects.nonNull(f)) {
throw new IllegalStateException("Index cannot be repeated. Please check it.");
}
fieldMap.put(index, field);
}
return fieldMap;
}

private List<T> getDataFromFile(Sheet sheet, Map<Integer, Field> fieldMap) {
long startTime = System.currentTimeMillis();
final int firstRowNum = sheet.getFirstRowNum();
Expand Down Expand Up @@ -217,6 +199,9 @@ private List<T> getDataFromFile(Sheet sheet, Map<Integer, Field> fieldMap) {
return null;
}
T obj = instanceObj(fieldMap, formatter, row);
if (!beanFilter.test(obj)) {
return null;
}
return new ParallelContainer<>(rowNum, obj);
}).filter(Objects::nonNull).collect(Collectors.toList());
log.info("Reading excel takes {} milliseconds", System.currentTimeMillis() - startTime);
Expand All @@ -239,7 +224,9 @@ private List<T> getDataFromFile(Sheet sheet, Map<Integer, Field> fieldMap) {
continue;
}
T obj = instanceObj(fieldMap, formatter, row);
result.add(obj);
if (beanFilter.test(obj)) {
result.add(obj);
}
}
log.info("Reading excel takes {} milliseconds", System.currentTimeMillis() - startTime);
return result;
Expand Down Expand Up @@ -272,7 +259,9 @@ private void readThenConsume(Sheet sheet, Map<Integer, Field> fieldMap, Consumer
continue;
}
T obj = instanceObj(fieldMap, formatter, row);
consumer.accept(obj);
if (beanFilter.test(obj)) {
consumer.accept(obj);
}
}
log.info("Reading excel takes {} milliseconds", System.currentTimeMillis() - startTime);
}
Expand All @@ -290,7 +279,6 @@ private T instanceObj(Map<Integer, Field> fieldMap, DataFormatter formatter, Row
return;
}
String content = formatter.formatCellValue(cell);
field.setAccessible(true);
ReadConverterContext.convert(content, field, obj);
});
return obj;
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/github/liaochong/myexcel/core/Row.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2019 liaochong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.liaochong.myexcel.core;

import lombok.AccessLevel;
import lombok.Data;
import lombok.experimental.FieldDefaults;

/**
* 行
*
* @author liaochong
* @version 1.0
*/
@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Row {

int rowNum;

public Row(int rowNum) {
this.rowNum = rowNum;
}
}
Loading

0 comments on commit ffe9eb0

Please sign in to comment.