Skip to content

Commit d2f7ad7

Browse files
committed
更新,支持xlsx
1 parent 2ddad00 commit d2f7ad7

12 files changed

Lines changed: 277 additions & 139 deletions

ExcelReads.jar

283 Bytes
Binary file not shown.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
* 增加数据过滤、数据转换和数据排序回调接口
1515
* 采用链式set方式进行
1616

17+
### 更新2016/11/29
18+
* 增加CreateMap By Key
19+
* 去除无用泛型
20+
* 增加xlsx支持
21+
1722
## 其他
1823
* 自定义读取支持出简单的规范化数据格式,即典型的表头格式
1924
* 可以继承 WapperMap 和 WapperObj进行扩展

pom.xml

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,38 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>Seven.ExcelReads</groupId>
55
<artifactId>ExcelReads</artifactId>
6+
<packaging>pom</packaging>
67
<version>0.0.1-SNAPSHOT</version>
78
<dependencies>
8-
<dependency>
9-
<groupId>org.apache.poi</groupId>
10-
<artifactId>poi</artifactId>
11-
<version>3.14</version>
12-
</dependency>
13-
<dependency>
9+
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
10+
<dependency>
11+
<groupId>org.apache.poi</groupId>
12+
<artifactId>poi</artifactId>
13+
<version>3.15</version>
14+
</dependency>
15+
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
16+
<dependency>
17+
<groupId>org.apache.poi</groupId>
18+
<artifactId>poi-ooxml</artifactId>
19+
<version>3.15</version>
20+
</dependency>
21+
<dependency>
1422
<groupId>junit</groupId>
1523
<artifactId>junit</artifactId>
1624
<version>4.12</version>
25+
<scope>test</scope>
1726
</dependency>
1827
</dependencies>
28+
<build>
29+
<plugins>
30+
<plugin>
31+
<artifactId>maven-compiler-plugin</artifactId>
32+
<version>3.1</version>
33+
<configuration>
34+
<source>1.8</source>
35+
<target>1.8</target>
36+
</configuration>
37+
</plugin>
38+
</plugins>
39+
</build>
1940
</project>

seven/ExcelFactory.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package seven;
22

3-
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
43
import seven.wapperInt.Wrapper;
54
import seven.wapperInt.wapperRef.WrapperObj;
65

7-
import java.io.FileInputStream;
8-
96

107
//=======================================================
118
// .----.
@@ -33,8 +30,7 @@ private ExcelFactory() {
3330
}
3431

3532
public static Wrapper getBeans(String FilePath, WrapperObj r) throws Exception {
36-
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(FilePath));
37-
return r.init(fs);
33+
return r.init(FilePath);
3834
}
3935

4036
}

seven/wapperInt/Wrapper.java

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import org.apache.poi.hssf.usermodel.HSSFCell;
44
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
5+
import org.apache.poi.ss.usermodel.Cell;
6+
import seven.wapperInt.callBack.DataFilterColumnInterface;
57
import seven.wapperInt.callBack.DataFilterInterface;
6-
import seven.wapperInt.callBack.DataFiterColumnInterface;
78
import seven.wapperInt.callBack.DataProcessInterface;
89

910
import java.io.Serializable;
@@ -32,10 +33,10 @@
3233
* @author Seven<p>
3334
* @date 2016年4月12日-下午4:08:08
3435
*/
35-
public abstract class Wrapper<T> implements Serializable{
36+
public abstract class Wrapper implements Serializable{
3637
protected Config config=new Config();
3738
protected DecimalFormat df = new DecimalFormat("0");
38-
protected String getCellFormatValue(HSSFCell cell) {
39+
protected String getCellFormatValue(Cell cell) {
3940
String cellvalue = "";
4041
if (cell != null) {
4142
switch (cell.getCellType()) {
@@ -135,7 +136,6 @@ public Config(Integer title_row, Integer content_row_start, Integer content_row_
135136
/**
136137
* 内容开始行号
137138
*
138-
* @param content_row_start
139139
*/
140140
public Integer getTitle_row() {
141141
return title_row;
@@ -144,7 +144,6 @@ public Integer getTitle_row() {
144144
/**
145145
* 标题行号
146146
*
147-
* @param content_row_start
148147
*/
149148
public void setTitle_row(Integer title_row) {
150149
this.title_row = title_row;
@@ -153,7 +152,6 @@ public void setTitle_row(Integer title_row) {
153152
/**
154153
* 内容开始行号
155154
*
156-
* @param content_row_start
157155
*/
158156
public Integer getContent_row_start() {
159157
return content_row_start;
@@ -170,8 +168,7 @@ public void setContent_row_start(Integer content_row_start) {
170168

171169
/**
172170
* 内容结束行号
173-
*
174-
* @param content_row_start
171+
* @return
175172
*/
176173
public Integer getContent_row_end() {
177174
return content_row_end;
@@ -180,7 +177,6 @@ public Integer getContent_row_end() {
180177
/**
181178
* 内容结束行号
182179
*
183-
* @param content_row_start
184180
*/
185181
public void setContent_row_end(Integer content_row_end) {
186182
this.content_row_end = content_row_end;
@@ -211,10 +207,45 @@ public void setEnd_sheet(Integer end_sheet) {
211207
}
212208

213209
}
214-
public abstract List<T> Create() throws Exception;
215-
public abstract Wrapper Filter(DataFilterInterface<T> filter);
216-
public abstract Wrapper Process(DataProcessInterface<T> process);
217-
public abstract Wrapper Sort(Comparator<? super T> c);
218-
public abstract Wrapper FiterCol(DataFiterColumnInterface df);
210+
211+
/**
212+
* 生成数据包,返回打包好的数据
213+
* @return
214+
* @throws Exception
215+
*/
216+
public abstract <T>List<T> Create() throws Exception;
217+
218+
/**
219+
* 对要包装的数据进行过滤,对应实体Bean\n
220+
* 如果返回false将放弃此条数据
221+
* @param filter {@link DataFilterInterface}
222+
* @return
223+
*/
224+
public abstract Wrapper Filter(DataFilterInterface<?> filter);
225+
226+
/**
227+
*此处传入每一行打包好的数据。对应一个实体\n
228+
* 在process方法里可对属性进行处理加工
229+
* @param process {@link DataProcessInterface}
230+
* @return
231+
*/
232+
public abstract Wrapper Process(DataProcessInterface<?> process);
233+
234+
/**
235+
* 对结果的List进行排序
236+
* @param c
237+
* @return
238+
*/
239+
public abstract Wrapper Sort(Comparator<?> c);
240+
241+
/**
242+
* 此处过滤Excel的列数据(列名)\n
243+
* 如果加入后,将不对实体进行赋值
244+
* @param df {@link DataFilterColumnInterface}
245+
*/
246+
public abstract Wrapper FilterCol(DataFilterColumnInterface df);
247+
248+
public abstract <T> T CreateMap(String key) throws Exception;
249+
219250
// public abstract Wrapper AsString(Comparator<? super T> c);
220251
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package seven.wapperInt.callBack;
2+
3+
/**
4+
* [Github]https://github.com/MatrixSeven
5+
* [Bolg]https://matrixseven.github.io/
6+
* Created by seven on 2016/10/19.
7+
* 此处过滤Excel的列数据(列名)
8+
* 如果加入后,将不对实体进行赋值
9+
*/
10+
public interface DataFilterColumnInterface {
11+
/**
12+
* 此处过滤Excel的列数据(列名)
13+
* 如果加入后,将不对实体进行赋值
14+
* @return
15+
*/
16+
public String[] Filter();
17+
}

seven/wapperInt/callBack/DataFilterInterface.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44
* [Github]https://github.com/MatrixSeven
55
* [Bolg]https://matrixseven.github.io/
66
* Created by seven on 2016/10/18.
7+
* 对要包装的数据进行过滤,对应实体Bean,如果返回false将放弃此条数据
8+
* T为实体Bean类型
79
*/
810
@FunctionalInterface
911
public interface DataFilterInterface<T> {
12+
/**
13+
* 对要包装的数据进行过滤,对应实体Bean\n
14+
* 如果返回false将放弃此条数据
15+
* @param t 实体Bean类型
16+
* @return
17+
*/
1018
Boolean filter(T t);
1119
}

seven/wapperInt/callBack/DataProcessInterface.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
* [Github]https://github.com/MatrixSeven
77
* [Bolg]https://matrixseven.github.io/
88
* Created by seven on 2016/10/18.
9+
* 此处传入每一行打包好的数据。对应一个实体
10+
* T为实体Bean类型
911
*/
10-
@SuppressWarnings("unchecked")
1112
public interface DataProcessInterface<T>{
13+
/***
14+
* 此处传入每一行打包好的数据。对应一个实体,
15+
* 在process方法里可对属性进行处理加工
16+
* @param t 实体类型
17+
*/
1218
void process(T t);
1319
}

seven/wapperInt/callBack/imp/DefaultDateColFiter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package seven.wapperInt.callBack.imp;
22

3-
import seven.wapperInt.callBack.DataFiterColumnInterface;
3+
import seven.wapperInt.callBack.DataFilterColumnInterface;
44

55
/**
66
* [Github]https://github.com/MatrixSeven
77
* [Bolg]https://matrixseven.github.io/
88
* Created by seven on 2016/10/19.
99
*/
10-
public class DefaultDateColFiter implements DataFiterColumnInterface {
10+
public class DefaultDateColFiter implements DataFilterColumnInterface {
1111

1212
@Override
1313
public String[] Filter() {

seven/wapperInt/wapperRef/WrapperObj.java

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,36 @@
1818
// (______|______)
1919
//=======================================================
2020

21+
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
2122
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
23+
import org.apache.poi.ss.usermodel.Workbook;
24+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
2225
import seven.wapperInt.Wrapper;
2326
import seven.wapperInt.callBack.DataFilterInterface;
24-
import seven.wapperInt.callBack.DataFiterColumnInterface;
27+
import seven.wapperInt.callBack.DataFilterColumnInterface;
2528
import seven.wapperInt.callBack.DataProcessInterface;
2629
import seven.wapperInt.callBack.imp.DefaultDataFiter;
2730
import seven.wapperInt.callBack.imp.DefaultProcess;
2831

29-
import java.util.ArrayList;
30-
import java.util.Comparator;
31-
import java.util.List;
32-
import java.util.Map;
32+
import java.io.File;
33+
import java.util.*;
3334

3435
/**
3536
* @author Seven<p>
3637
* @date 2016年4月12日-下午4:07:57
3738
*/
38-
public abstract class WrapperObj<T> extends Wrapper<T> {
39+
public abstract class WrapperObj<T> extends Wrapper {
3940
protected DataFilterInterface filter=new DefaultDataFiter<Object>();
4041
protected DataProcessInterface process=new DefaultProcess<Object>();
4142
protected List<String> filterColBy_key=new ArrayList<>();
4243
protected List<String> filterColBy_value=new ArrayList<>();
43-
protected Comparator<? super T> c;
44-
protected POIFSFileSystem fs;
45-
protected abstract List<T> RefResWapper(POIFSFileSystem fs) throws Exception;
44+
protected Comparator<? super Object> c;
45+
protected String fs;
46+
protected static final boolean isMap=false;
47+
48+
protected abstract <T> T RefResWapper(String fs,boolean isMap,String key) throws Exception;
49+
50+
4651
protected boolean isNull(Map<String, String> map) {
4752
boolean isN = true;
4853
for (Map.Entry<String, String> s : map.entrySet()) {
@@ -51,35 +56,54 @@ protected boolean isNull(Map<String, String> map) {
5156
return isN;
5257
}
5358

54-
public Wrapper FiterCol(DataFiterColumnInterface df) {
59+
60+
public Wrapper FilterCol(DataFilterColumnInterface df) {
5561
for (String s:df.Filter() ) {
5662
filterColBy_key.add(s);
5763
}
5864
return this;
5965
}
6066

67+
protected Workbook newInstance (String type) throws Exception{
68+
File f=new File(type);
69+
if(!f.isFile()){
70+
throw new Exception("请填写正确路径");
71+
}
72+
type=type.substring(type.lastIndexOf(".")+1);
73+
if(type.equals("xls")){
74+
return new HSSFWorkbook(new POIFSFileSystem(f));
75+
}
76+
return new XSSFWorkbook(f);
77+
}
6178

62-
public Wrapper init(POIFSFileSystem fs){
63-
this.c=new Comparator<T>() {
79+
public Wrapper init(String FilePath){
80+
this.c=new Comparator<Object>() {
6481
@Override
65-
public int compare(T o1, T o2) {
82+
public int compare(Object o1,Object o2) {
6683
return 0;
6784
}
6885
};
69-
this.fs=fs;
86+
87+
this.fs=FilePath;
7088
return this;
7189
}
7290

7391
public Wrapper Sort(Comparator c) {
7492
this.c = c;return this;
7593
}
7694
public List<T> Create() throws Exception{
77-
return RefResWapper(fs);
95+
return RefResWapper(fs,isMap,null);
96+
}
97+
98+
@Override
99+
public <T> T CreateMap(String key)throws Exception {
100+
return RefResWapper(fs,!isMap,key);
78101
}
79-
public Wrapper Filter(DataFilterInterface<T> filter) {
102+
103+
public Wrapper Filter(DataFilterInterface<?> filter) {
80104
this.filter = filter;return this;
81105
}
82-
public Wrapper Process(DataProcessInterface<T> process) {
106+
public Wrapper Process(DataProcessInterface<?> process) {
83107
this.process = process;return this;
84108
}
85109
}

0 commit comments

Comments
 (0)