Skip to content

Commit 711fbca

Browse files
committed
Merge remote-tracking branch 'origin/1.0.x'
2 parents eb24f67 + 0ec5163 commit 711fbca

24 files changed

+985
-642
lines changed

pom.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,11 @@
151151
<artifactId>fastjson</artifactId>
152152
<version>1.2.7</version>
153153
</dependency>
154-
<dependency>
155-
<groupId>org.springframework</groupId>
156-
<artifactId>spring-core</artifactId>
157-
<version>4.2.2.RELEASE</version>
158-
</dependency>
159154
<dependency>
160155
<groupId>org.springframework</groupId>
161156
<artifactId>spring-beans</artifactId>
162157
<version>4.2.2.RELEASE</version>
163158
</dependency>
164-
<dependency>
165-
<groupId>commons-lang</groupId>
166-
<artifactId>commons-lang</artifactId>
167-
<version>2.6</version>
168-
</dependency>
169159
<dependency>
170160
<groupId>junit</groupId>
171161
<artifactId>junit</artifactId>

src/main/java/org/hellojavaer/poi/excel/utils/ExcelUtils.java

Lines changed: 323 additions & 257 deletions
Large diffs are not rendered by default.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2015-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.hellojavaer.poi.excel.utils.common;
17+
18+
/**
19+
*
20+
* @author <a href="mailto:[email protected]">zoukaiming</a> 2016年4月2日 下午8:28:11
21+
*/
22+
public class Assert {
23+
24+
public static void notNull(Object object) {
25+
notNull(object, "[Assertion failed] - the object argument must be null");
26+
}
27+
28+
public static void notNull(Object object, String message) {
29+
if (object == null) {
30+
throw new IllegalArgumentException(message);
31+
}
32+
}
33+
34+
public static void isTrue(boolean expression) {
35+
isTrue(expression, "[Assertion failed] - this expression must be true");
36+
}
37+
38+
public static void isTrue(boolean expression, String message) {
39+
if (!expression) {
40+
throw new IllegalArgumentException(message);
41+
}
42+
}
43+
}

src/main/java/org/hellojavaer/poi/excel/utils/read/ExcelCellValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,9 @@ public java.sql.Timestamp getTimestamp() {
8787
public Object getOriginalValue() {
8888
return originalValue;
8989
}
90+
91+
@Override
92+
public String toString() {
93+
return TypeUtils.castToString(originalValue);
94+
}
9095
}

src/main/java/org/hellojavaer/poi/excel/utils/read/ExcelReadCellValueMapping.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
*/
3333
public class ExcelReadCellValueMapping extends HashMap<String, Object> {
3434

35-
private static final long serialVersionUID = 1L;
35+
private static final long serialVersionUID = 1L;
3636

37-
private static final Object DEFAULT_INPUT = new Object();
38-
private boolean setDefaultValue = false;
39-
private Object defaultValue = null;
37+
private static final Object DEFAULT_INPUT = new Object();
38+
private boolean settedDefaultValue = false;
39+
private Object defaultValue = null;
4040
private ExcelReadCellProcessor defaultProcessor;
4141

4242
public Object getDefaultValue() {
@@ -45,23 +45,25 @@ public Object getDefaultValue() {
4545

4646
public void setDefaultValue(Object val) {
4747
this.defaultValue = val;
48+
this.settedDefaultValue = true;
4849
}
4950

5051
public void setDefaultValueWithDefaultInput() {
5152
this.defaultValue = DEFAULT_INPUT;
53+
this.settedDefaultValue = true;
5254
}
5355

5456
public void resetDefaultValue() {
5557
this.defaultValue = null;
56-
this.setDefaultValue = false;
58+
this.settedDefaultValue = false;
5759
}
5860

59-
public boolean isSetDefaultValue() {
60-
return setDefaultValue;
61+
public boolean isSettedDefaultValue() {
62+
return settedDefaultValue;
6163
}
6264

63-
public boolean isSetDefaultValueWithDefaultInput() {
64-
return setDefaultValue && (defaultValue == DEFAULT_INPUT);
65+
public boolean isSettedDefaultValueWithDefaultInput() {
66+
return settedDefaultValue && (defaultValue == DEFAULT_INPUT);
6567
}
6668

6769
public ExcelReadCellProcessor getDefaultProcessor() {

src/main/java/org/hellojavaer/poi/excel/utils/read/ExcelReadException.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ public class ExcelReadException extends RuntimeException {
2424

2525
private static final long serialVersionUID = 1L;
2626

27-
public static final int CODE_OF_PROCESS_EXCEPTION = 0;
28-
public static final int CODE_OF_CELL_VALUE_REQUIRED = 1;
29-
public static final int CODE_OF_CELL_VALUE_NOT_MATCHED = 2;
30-
public static final int CODE_OF_CELL_ERROR = 3;
27+
public static final int CODE_OF_SHEET_NOT_EXSIT = 0;
28+
public static final int CODE_OF_PROCESS_EXCEPTION = 1;
29+
public static final int CODE_OF_CELL_VALUE_REQUIRED = 2;
30+
public static final int CODE_OF_CELL_VALUE_NOT_MATCHED = 3;
31+
public static final int CODE_OF_CELL_ERROR = 4;
3132

3233
private Integer rowIndex = null;
3334
private String colStrIndex = null;

src/main/java/org/hellojavaer/poi/excel/utils/read/ExcelReadFieldMapping.java

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -18,95 +18,74 @@
1818
import java.io.Serializable;
1919
import java.util.LinkedHashMap;
2020
import java.util.Map;
21-
import java.util.Map.Entry;
22-
import java.util.Set;
2321
import java.util.concurrent.ConcurrentHashMap;
2422

25-
import org.hellojavaer.poi.excel.utils.ExcelUtils;
23+
import org.hellojavaer.poi.excel.utils.common.Assert;
2624

2725
/**
2826
* @author <a href="mailto:[email protected]">zoukaiming</a>
2927
*/
3028
public class ExcelReadFieldMapping implements Serializable {
3129

32-
private static final boolean DEFAULT_REQUIRE = true;
3330
private static final long serialVersionUID = 1L;
3431

35-
private Map<Integer, Map<String, InnerReadCellProcessorWrapper>> fieldMapping = new LinkedHashMap<Integer, Map<String, InnerReadCellProcessorWrapper>>();
32+
private Map<String, Map<String, ExcelReadFieldMappingAttribute>> fieldMapping = new LinkedHashMap<String, Map<String, ExcelReadFieldMappingAttribute>>();
3633

37-
public void put(int colIndex, String fieldName) {
38-
put(colIndex, fieldName, null, null, DEFAULT_REQUIRE);
39-
}
40-
41-
public void put(int colIndex, String fieldName, boolean required) {
42-
put(colIndex, fieldName, null, null, required);
43-
}
44-
45-
public void put(int colIndex, String fieldName, ExcelReadCellProcessor proccessor) {
46-
put(colIndex, fieldName, null, proccessor, DEFAULT_REQUIRE);
47-
}
48-
49-
public void put(int colIndex, String fieldName, ExcelReadCellValueMapping valueMapping) {
50-
put(colIndex, fieldName, valueMapping, null, DEFAULT_REQUIRE);
51-
}
52-
53-
public void put(int colIndex, String fieldName, ExcelReadCellProcessor proccessor, boolean required) {
54-
put(colIndex, fieldName, null, proccessor, required);
34+
public ExcelReadFieldMappingAttribute put(String colIndexOrColName, String fieldName) {
35+
Assert.notNull(colIndexOrColName);
36+
Assert.notNull(fieldName);
37+
Map<String, ExcelReadFieldMappingAttribute> map = fieldMapping.get(colIndexOrColName);
38+
if (map == null) {
39+
synchronized (fieldMapping) {
40+
if (fieldMapping.get(colIndexOrColName) == null) {
41+
map = new ConcurrentHashMap<String, ExcelReadFieldMappingAttribute>();
42+
fieldMapping.put(colIndexOrColName, map);
43+
}
44+
}
45+
}
46+
ExcelReadFieldMappingAttribute attribute = new ExcelReadFieldMappingAttribute();
47+
map.put(fieldName, attribute);
48+
return attribute;
5549
}
5650

57-
public void put(int colIndex, String fieldName, ExcelReadCellValueMapping valueMapping, boolean required) {
58-
put(colIndex, fieldName, valueMapping, null, required);
51+
public Map<String, Map<String, ExcelReadFieldMappingAttribute>> export() {
52+
return fieldMapping;
5953
}
6054

61-
public void put(String colIndex, String fieldName) {
62-
put(colIndex, fieldName, null, null, DEFAULT_REQUIRE);
63-
}
55+
public class ExcelReadFieldMappingAttribute implements Serializable {
6456

65-
public void put(String colIndex, String fieldName, boolean required) {
66-
put(colIndex, fieldName, null, null, required);
67-
}
57+
private static final long serialVersionUID = 1L;
6858

69-
public void put(String colIndex, String fieldName, ExcelReadCellValueMapping valueMapping) {
70-
put(colIndex, fieldName, valueMapping, null, DEFAULT_REQUIRE);
71-
}
59+
private boolean required = false;
60+
private ExcelReadCellProcessor cellProcessor;
61+
private ExcelReadCellValueMapping valueMapping;
7262

73-
public void put(String colIndex, String fieldName, ExcelReadCellProcessor proccessor) {
74-
put(colIndex, fieldName, null, proccessor, DEFAULT_REQUIRE);
75-
}
63+
public ExcelReadFieldMappingAttribute setRequired(boolean required) {
64+
this.required = required;
65+
return this;
66+
}
7667

77-
public void put(String colIndex, String fieldName, ExcelReadCellValueMapping valueMapping, boolean required) {
78-
put(colIndex, fieldName, valueMapping, null, required);
79-
}
68+
public ExcelReadFieldMappingAttribute setCellProcessor(ExcelReadCellProcessor cellProcessor) {
69+
this.cellProcessor = cellProcessor;
70+
return this;
71+
}
8072

81-
public void put(String colIndex, String fieldName, ExcelReadCellProcessor proccessor, boolean required) {
82-
put(colIndex, fieldName, null, proccessor, required);
83-
}
73+
public ExcelReadFieldMappingAttribute setValueMapping(ExcelReadCellValueMapping valueMapping) {
74+
this.valueMapping = valueMapping;
75+
return this;
76+
}
8477

85-
private void put(int colIndex, String fieldName, ExcelReadCellValueMapping valueMapping,
86-
ExcelReadCellProcessor proccessor, boolean required) {
87-
Map<String, InnerReadCellProcessorWrapper> map = fieldMapping.get(colIndex);
88-
if (map == null) {
89-
synchronized (fieldMapping) {
90-
if (fieldMapping.get(colIndex) == null) {
91-
map = new ConcurrentHashMap<String, InnerReadCellProcessorWrapper>();
92-
fieldMapping.put(colIndex, map);
93-
}
94-
}
78+
public boolean isRequired() {
79+
return required;
9580
}
96-
map.put(fieldName, new InnerReadCellProcessorWrapper(valueMapping, proccessor, required));
97-
}
9881

99-
private void put(String colIndex, String fieldName, ExcelReadCellValueMapping valueMapping,
100-
ExcelReadCellProcessor proccessor, boolean required) {
101-
put(ExcelUtils.convertColCharIndexToIntIndex(colIndex), fieldName, valueMapping, proccessor, required);
102-
}
82+
public ExcelReadCellProcessor getCellProcessor() {
83+
return cellProcessor;
84+
}
10385

104-
public boolean isEmpty() {
105-
return fieldMapping.isEmpty();
106-
}
86+
public ExcelReadCellValueMapping getValueMapping() {
87+
return valueMapping;
88+
}
10789

108-
public Set<Entry<Integer, Map<String, InnerReadCellProcessorWrapper>>> entrySet() {
109-
return fieldMapping.entrySet();
11090
}
111-
11291
}

src/main/java/org/hellojavaer/poi/excel/utils/read/ExcelReadSheetProcessor.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ public abstract class ExcelReadSheetProcessor<T> {
2525
private Integer sheetIndex;
2626
private String sheetName;
2727
private Class<T> targetClass;
28-
private int rowStartIndex = 0;
29-
private Integer rowEndIndex;
28+
private int startRowIndex = 0;
29+
private Integer endRowIndex;
3030
private Integer pageSize;
3131
private ExcelReadFieldMapping fieldMapping;
3232
private ExcelReadRowProcessor<T> rowProcessor;
33-
private boolean skipEmptyRow = false;
3433
private boolean trimSpace = false;
34+
private Integer headRowIndex;
3535

3636
public abstract void beforeProcess(ExcelReadContext<T> context);
3737

3838
public abstract void process(ExcelReadContext<T> context, List<T> list);
3939

40-
public abstract void onExcepton(ExcelReadContext<T> context, RuntimeException e);
40+
public abstract void onException(ExcelReadContext<T> context, RuntimeException e);
4141

4242
public abstract void afterProcess(ExcelReadContext<T> context);
4343

@@ -74,16 +74,16 @@ public void setSheetName(String sheetName) {
7474
*
7575
* @param sheetIndex
7676
*/
77-
public int getRowStartIndex() {
78-
return rowStartIndex;
77+
public int getStartRowIndex() {
78+
return startRowIndex;
7979
}
8080

8181
/**
8282
*
8383
* @param startRowIndex
8484
*/
85-
public void setRowStartIndex(int rowStartIndex) {
86-
this.rowStartIndex = rowStartIndex;
85+
public void setStartRowIndex(int startRowIndex) {
86+
this.startRowIndex = startRowIndex;
8787
}
8888

8989
public Integer getPageSize() {
@@ -131,24 +131,16 @@ public void setTargetClass(Class<T> targetClass) {
131131
this.targetClass = targetClass;
132132
}
133133

134-
public Integer getRowEndIndex() {
135-
return rowEndIndex;
134+
public Integer getEndRowIndex() {
135+
return endRowIndex;
136136
}
137137

138138
/**
139139
*
140140
* @param rowEndIndex
141141
*/
142-
public void setRowEndIndex(Integer rowEndIndex) {
143-
this.rowEndIndex = rowEndIndex;
144-
}
145-
146-
public boolean isSkipEmptyRow() {
147-
return skipEmptyRow;
148-
}
149-
150-
public void setSkipEmptyRow(boolean skipEmptyRow) {
151-
this.skipEmptyRow = skipEmptyRow;
142+
public void setEndRowIndex(Integer endRowIndex) {
143+
this.endRowIndex = endRowIndex;
152144
}
153145

154146
public boolean isTrimSpace() {
@@ -159,4 +151,12 @@ public void setTrimSpace(boolean trimSpace) {
159151
this.trimSpace = trimSpace;
160152
}
161153

154+
public Integer getHeadRowIndex() {
155+
return headRowIndex;
156+
}
157+
158+
public void setHeadRowIndex(Integer headRowIndex) {
159+
this.headRowIndex = headRowIndex;
160+
}
161+
162162
}

0 commit comments

Comments
 (0)