Skip to content

Commit d72af23

Browse files
committed
增加方法,完善异常体系,详见更新日志
1 parent 8ca2cf4 commit d72af23

8 files changed

Lines changed: 91 additions & 21 deletions

File tree

ExcelReads.jar

625 Bytes
Binary file not shown.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
###最近三次更新:
1414
#### 更新2017/01/09
1515
* 增加SetPath方法,随时切换保存路径
16+
* 增加ConvertName方法,方便自定义Excel列名称
1617
* 增加Flush方法,不在建议使用~~Save(@Deprecated)~~方法进行保存输出
1718
* 增加SetOutputStream方法,可以放入自定义流,用于支持网页Response输出
1819
* 处理一些小bug,完善异常提示信息
@@ -54,6 +55,16 @@ ExcelFactory.saveExcel(
5455
.FilterCol(() -> new String[]{"updatetime"})
5556
.Filter((AS o) ->o.getA().length() > 3)
5657
.Save();
58+
59+
60+
//ConvertName 转行列名
61+
ExcelFactory.saveExcel(ps.executeQuery()).SetPath("seven2.xlsx")
62+
.Process((HashMap<String,String> o)->o
63+
.put("address",o.get("address")
64+
.concat("seven")))
65+
.ConvertName("address","地址")
66+
.ConvertName("name","姓名")
67+
.Flush();
5768
```
5869
## 数据库导出自定义Bean类型写法(自己实现包装)
5970
```java

UPDATELOG.MD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### 更新2017/01/09
44
* 增加SetPath方法,随时切换保存路径
5+
* 增加ConvertName方法,方便自定义Excel列名称
56
* 增加Flush方法,不在建议使用~~Save(@Deprecated)~~方法进行保存输出
67
* 增加SetOutputStream方法,可以放入自定义流,用于支持网页Response输出
78
* 处理一些小bug,完善异常提示信息

src/main/java/seven/savewapper/SaveExcel.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.io.OutputStream;
2424
import java.util.Comparator;
25+
import java.util.HashMap;
2526

2627
/**
2728
* [Zhihu]https://www.zhihu.com/people/Sweets07
@@ -81,4 +82,26 @@ public interface SaveExcel extends ExcelSuperInterface {
8182
*/
8283
SaveExcel SetPath(String path);
8384

85+
/**
86+
* 转换字段名称
87+
* @param title
88+
* @param new_title
89+
* @return
90+
*/
91+
SaveExcel ConvertName(String title,String new_title);
92+
93+
/**
94+
* 转换字段
95+
* @param title_mapping
96+
* @return
97+
*/
98+
SaveExcel ConvertName(HashMap<String,String> title_mapping);
99+
100+
/**
101+
* 转换字段
102+
* @param title_mapping
103+
* @return
104+
*/
105+
SaveExcel ConvertName(HashMap<String,String> title_mapping,Boolean is_init);
106+
84107
}

src/main/java/seven/savewapper/wapperRef/SaveExcelObject.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.sql.ResultSet;
3030
import java.util.ArrayList;
3131
import java.util.Comparator;
32+
import java.util.HashMap;
3233
import java.util.List;
3334

3435
/**
@@ -38,7 +39,7 @@
3839
*/
3940
public abstract class SaveExcelObject<T> implements SaveExcel {
4041
protected List<T> list;
41-
public static final String DEFAULT_TYPE="xlsx";
42+
public static final String DEFAULT_TYPE = "xlsx";
4243
protected String path;
4344
protected List<String> filterColBy_key = new ArrayList<>();
4445
protected DataFilterInterface filter = new DefaultDataFilter<Object>();
@@ -47,7 +48,8 @@ public abstract class SaveExcelObject<T> implements SaveExcel {
4748
protected ResultSet resultSet = null;
4849
protected OutputStream stream = null;
4950
protected Boolean isResponse = false;
50-
protected Workbook wk=null;
51+
protected Workbook wk = null;
52+
protected HashMap<String, String> convert_title = new HashMap<>();
5153

5254
public SaveExcelObject(List<T> list, String path) {
5355
this.list = list;
@@ -72,8 +74,9 @@ public SaveExcelObject Filter(DataFilterInterface<?> filter) {
7274
this.filter = filter;
7375
return this;
7476
}
75-
protected Workbook createWK() throws Exception{
76-
return wk = ExcelTool.newInstance(path.equals("")?DEFAULT_TYPE:path, true);
77+
78+
protected Workbook createWK() throws Exception {
79+
return wk = ExcelTool.newInstance(path.equals("") ? DEFAULT_TYPE : path, true);
7780
}
7881

7982
@Override
@@ -104,13 +107,23 @@ protected void checkData() throws Exception {
104107

105108
protected OutputStream createStream() throws Exception {
106109
if (stream == null) {
107-
if (path == null|| path.equals(""))
110+
if (path == null || path.equals(""))
108111
throw new Exception("请输入路径");
109112
return new FileOutputStream(path);
110113
}
111114
return stream;
112115
}
113116

117+
/**
118+
* TempName
119+
*/
120+
private String title_=null;
121+
protected String convertTitle(String title) throws Exception {
122+
title_=null;
123+
title_=convert_title.get(title);
124+
return title_==null?title:title_;
125+
}
126+
114127
@Override
115128
public SaveExcel SetOutputStream(OutputStream stream) throws Exception {
116129
this.stream = stream;
@@ -127,4 +140,24 @@ public SaveExcel SetPath(String path) {
127140
this.path = path;
128141
return this;
129142
}
143+
144+
@Override
145+
public SaveExcel ConvertName(String title, String new_title) {
146+
convert_title.put(title, new_title);
147+
return this;
148+
}
149+
150+
@Override
151+
public SaveExcel ConvertName(HashMap<String, String> title_mapping) {
152+
convert_title.putAll(title_mapping);
153+
return this;
154+
}
155+
156+
@Override
157+
public SaveExcel ConvertName(HashMap<String, String> title_mapping, Boolean is_init) {
158+
if (is_init) {
159+
convert_title.clear();
160+
}
161+
return ConvertName(title_mapping);
162+
}
130163
}

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,32 @@ public ResExprotDBObj(ResultSet resultSet, String path, Class type) {
4141
super(resultSet, path);
4242
this.clazz = type;
4343
}
44-
public ResExprotDBObj(ResultSet resultSet,Class type) {
44+
45+
public ResExprotDBObj(ResultSet resultSet, Class type) {
4546
super(resultSet);
4647
this.clazz = type;
4748
}
4849

4950
public ResExprotDBObj CreateList() throws Exception {
5051
this.list = new ArrayList<>();
5152
if (clazz != null) {
52-
Field[] fields= ExcelTool.GetFilesDeep(clazz);
53-
int len =fields.length;
54-
String name[]=new String[len];
55-
ExcelAnno anno=null;
53+
Field[] fields = ExcelTool.GetFilesDeep(clazz);
54+
int len = fields.length;
55+
String name[] = new String[len];
56+
String f_name[] = new String[len];
57+
ExcelAnno anno = null;
5658
for (int i = 0; i < len; i++) {
5759
fields[i].setAccessible(true);
58-
if((anno=fields[i].getAnnotation(ExcelAnno.class))!=null&&!anno.Value().equals("Null")){
59-
name[i]=anno.Value();
60+
if ((anno = fields[i].getAnnotation(ExcelAnno.class)) != null && !anno.Value().equals("Null")) {
61+
name[i] = anno.Value();
62+
f_name[i] = fields[i].getName();
6063
continue;
6164
}
62-
name[i]=fields[i].getName();
65+
f_name[i] = name[i] = fields[i].getName();
6366
}
64-
Object o=null;
67+
Object o = null;
6568
while (resultSet.next()) {
66-
o=clazz.newInstance();
69+
o = clazz.newInstance();
6770
for (int i = 0; i < len; i++) {
6871
fields[i].set(o, resultSet.getString(name[i]));
6972
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public void Save() throws Exception {
6666
Iterator<String> it = set_title.iterator();
6767
String t;
6868
String[] title;
69-
List<String> l;
70-
l = new ArrayList();
69+
List<String> l = new ArrayList();
7170
while (it.hasNext()) {
7271
t = it.next();
7372
if (!filterColBy_key.contains(t)) {
@@ -80,7 +79,7 @@ public void Save() throws Exception {
8079
for (short i = 0; i < title.length; i++) {
8180
Cell cell = row.createCell(i);
8281
cell.setCellStyle(style);
83-
cell.setCellValue(title[i]);
82+
cell.setCellValue(convertTitle(title[i]));
8483
}
8584
int index = 0;
8685
for (Map<String, String> o : list) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,11 @@ public void Save() throws Exception {
6363
continue;
6464
}
6565
ea = fields[i].getAnnotation(ExcelAnno.class);
66+
title[i] = fields[i].getName();
6667
if (ea != null&&!ea.Value().equals("Null")) {
67-
title[i] = ea.Value();
6868
align[i] = ea.Align();
6969
continue;
7070
}
71-
title[i] = fields[i].getName();
7271
align[i] = 0x2;
7372
}
7473

@@ -90,7 +89,7 @@ public void Save() throws Exception {
9089
for (short i = 0; i < title.length; i++) {
9190
Cell cell = row.createCell(i);
9291
cell.setCellStyle(style);
93-
cell.setCellValue(title[i]);
92+
cell.setCellValue(convertTitle(title[i]));
9493
}
9594
int index = 0;
9695
Object object=null;
@@ -106,6 +105,7 @@ public void Save() throws Exception {
106105
cell.setCellStyle(style);
107106
object=fields[i].get(o);
108107
cell.setCellValue(object==null?"":object.toString());
108+
109109
}
110110
}
111111

0 commit comments

Comments
 (0)