Skip to content

Commit 853ad49

Browse files
committed
增加直接的Class类型导出
1 parent 78a3908 commit 853ad49

6 files changed

Lines changed: 90 additions & 24 deletions

File tree

ExcelReads.jar

815 Bytes
Binary file not shown.

README.md

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
* 依赖POI,使用Maven构建
1111

1212
## 更新纪录
13+
### 更新2017/01/05
14+
* 修复据库查询的导出(Object)递归越栈问题
15+
* 增加新的xxx.Class定义类型导出,操作更新点
16+
* 导出注解支持
1317

1418
### 更新2017/01/04
1519
* 修复据库查询的导出(Map)空指针&下标越界问题
@@ -56,22 +60,45 @@
5660
|bar | bar | bar |
5761
|baz | baz | baz |
5862

63+
64+
## 数据库导出自定义Bean类型写法(xxx.Class类型)
65+
```java
66+
ExcelFactory.saveExcel(
67+
UNPOOLED_DATA_SOURCE.getConnection().
68+
prepareStatement("select * FROM users_info limit 1000").executeQuery(),
69+
"\u5317\u4eac__Excel.xlsx",
70+
AS.class)
71+
.FilterCol(() -> new String[]{"updatetime"})
72+
.Filter((AS o) ->o.getA().length() > 3)
73+
.Save();
74+
```
75+
## 数据库导出自定义Bean类型写法(自己实现包装)
76+
```java
77+
ExcelFactory.saveExcel(
78+
UNPOOLED_DATA_SOURCE.getConnection().
79+
prepareStatement("select * FROM users_info limit 1000").executeQuery(),
80+
"\u5317\u4eac__Excel.xlsx",
81+
res -> {
82+
AS a = new AS();
83+
a.setA(res.getString("name"));
84+
return a;
85+
})
86+
.FilterCol(() -> new String[]{"updatetime"})
87+
.Filter((AS o) ->o.getA().length() > 3)
88+
.Save();
89+
```
5990
## 数据库直接导出到Excel例子
6091
```java
61-
UnpooledDataSource UNPOOLED_DATA_SOURCE = new UnpooledDataSource("com.mysql.jdbc.Driver",
62-
"jdbc:mysql://127.0.0.1:3306/zhihuspider",
63-
"xxxx", "xxxxx"
64-
);
65-
ExcelFactory.saveExcel(
66-
UNPOOLED_DATA_SOURCE.getConnection().
67-
prepareStatement("select * FROM users_info limit 10000").
68-
executeQuery(), "知乎导出Excel.xlsx")
69-
//过滤字段
70-
.FilterCol(() -> new String[]{"updatetime"})
71-
//过滤数据条件
72-
.Filter((HashMap<String, String> o) ->
73-
o.get("address").equals("\u5317\u4eac"))
74-
.Save();
92+
ExcelFactory.saveExcel(
93+
UNPOOLED_DATA_SOURCE.getConnection().
94+
prepareStatement("select * FROM users_info limit 10000").
95+
executeQuery(), "知乎导出Excel.xlsx")
96+
//过滤字段
97+
.FilterCol(() -> new String[]{"updatetime"})
98+
//过滤数据条件
99+
.Filter((HashMap<String, String> o) ->
100+
o.get("address").equals("\u5317\u4eac"))
101+
.Save();
75102

76103
```
77104

src/main/java/seven/ExcelFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,8 @@ public static SaveExcel saveExcel(ResultSet resultSet, String FilePath) throws E
5757
public static SaveExcel saveExcel(ResultSet resultSet, String FilePath, PackageDataInterface packageDataInterface) throws Exception {
5858
return new ResExprotDBObj(resultSet,FilePath,packageDataInterface);
5959
}
60+
public static SaveExcel saveExcel(ResultSet resultSet, String FilePath, Class type) throws Exception {
61+
return new ResExprotDBObj(resultSet,FilePath,type);
62+
}
6063

6164
}

src/main/java/seven/callBack/PackageDataInterface.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@
2424
*/
2525
@FunctionalInterface
2626
public interface PackageDataInterface<T> {
27-
T PackageDataProcess(ResultSet res);
28-
}
27+
T PackageDataProcess(ResultSet res) throws Exception;}

src/main/java/seven/savewapper/wapperRef/sysWppers/ResExprotDBObj.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
//=======================================================
1717

1818
import seven.callBack.PackageDataInterface;
19+
import seven.savewapper.anno.ExcelAnno;
20+
import seven.util.ExcelTool;
1921

22+
import java.lang.reflect.Field;
2023
import java.sql.ResultSet;
2124
import java.util.ArrayList;
2225

@@ -25,12 +28,14 @@
2528
* [Github]https://github.com/MatrixSeven
2629
* Created by seven on 2017/1/1.
2730
*/
28-
public class ResExprotDBObj extends ResExprotObj{
31+
public class ResExprotDBObj extends ResExprotObj {
2932
protected PackageDataInterface dataInterface;
30-
// protected T type;
33+
protected Class clazz = null;
34+
35+
// protected T type;
3136
public ResExprotDBObj(ResultSet resultSet, String path, PackageDataInterface dataInterface) {
3237
super(resultSet, path);
33-
this.dataInterface=dataInterface;
38+
this.dataInterface = dataInterface;
3439
// Type sType = getClass().getGenericSuperclass();
3540
// Type[] generics = ((ParameterizedType) sType).getActualTypeArguments();
3641
// Class<T> mTClass = (Class<T>) (generics[0]);
@@ -42,17 +47,49 @@ public ResExprotDBObj(ResultSet resultSet, String path, PackageDataInterface dat
4247

4348
}
4449

50+
public ResExprotDBObj(ResultSet resultSet, String path, Class type) {
51+
super(resultSet, path);
52+
this.clazz = type;
53+
}
54+
4555
public ResExprotDBObj CreateList() throws Exception {
46-
this.list=new ArrayList<>();
47-
// T o= (T) type.getClass().newInstance();
48-
while (resultSet.next()){
56+
this.list = new ArrayList<>();
57+
// Object o;
58+
// Method[] methods= dataInterface.getClass().getDeclaredMethods();
59+
// Class<?>[] type= methods[0].getParameterTypes();
60+
// Class<?> clazz= type[type.length-1].getClass();
61+
if (clazz != null) {
62+
Field[] fields= ExcelTool.GetFilesDeep(clazz);
63+
int len =fields.length;
64+
String name[]=new String[len];
65+
ExcelAnno anno=null;
66+
for (int i = 0; i < len; i++) {
67+
fields[i].setAccessible(true);
68+
if((anno=fields[i].getAnnotation(ExcelAnno.class))!=null){
69+
name[i]=anno.value();
70+
continue;
71+
}
72+
name[i]=fields[i].getName();
73+
}
74+
Object o=null;
75+
while (resultSet.next()) {
76+
o=clazz.newInstance();
77+
for (int i = 0; i < len; i++) {
78+
fields[i].set(o, resultSet.getString(name[i]));
79+
}
80+
list.add(o);
81+
}
82+
return this;
83+
}
84+
while (resultSet.next()) {
4985
list.add(dataInterface.PackageDataProcess(resultSet));
5086
}
5187
return this;
5288
}
5389

5490
@Override
5591
public void Save() throws Exception {
56-
CreateList().Save();
92+
this.CreateList();
93+
super.Save();
5794
}
5895
}

src/main/java/seven/wapperInt/Wrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static class Config {
8282
Boolean is_loop_sheet = false;
8383
Boolean error_log = true;
8484
Integer Voc_size = 10000;
85-
String Require[] = null;
85+
String Require[] = new String[]{};
8686
Integer start_sheet = 0;
8787
Integer end_sheet = null;
8888

0 commit comments

Comments
 (0)