Skip to content

Commit 77c5e39

Browse files
committed
增加方法,突破cellStyle绑定wk约束
1 parent d72af23 commit 77c5e39

11 files changed

Lines changed: 190 additions & 63 deletions

File tree

ExcelReads.jar

4.16 KB
Binary file not shown.

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
## [更新日志详见:UpdateLogs.md](UPDATELOG.MD)
1313
###最近三次更新:
14+
15+
#### 更新2017/01/11
16+
* 增加AnyCol来对应FilterCol方法,只保留AnyCol类列
17+
* 增加SetCellStyle,突破CellStye绑定wk约束,链式设置列单元格风格(非常狗血)
18+
1419
#### 更新2017/01/09
1520
* 增加SetPath方法,随时切换保存路径
1621
* 增加ConvertName方法,方便自定义Excel列名称
@@ -21,8 +26,6 @@
2126
#### 更新2017/01/06
2227
* 整合注解,导出和读取使用同一个ExcelAnno注解
2328
* 统一编码为UTF-8
24-
25-
#### 更新2017/01/05
2629
* 修复据库查询的导出(Object)递归越栈问题
2730
* 增加新的xxx.Class定义类型导出,操作更简单
2831
* 导出注解支持(自己使用seven.savewapper.anno.ExcelAnno类型注解)
@@ -44,7 +47,26 @@
4447
|bar | bar | bar |
4548
|baz | baz | baz |
4649

47-
50+
### 设置导出列风格
51+
```java
52+
ExcelFactory.saveExcel(ps.executeQuery())
53+
.SetPath("seven007.xlsx")
54+
.ConvertName("name", "姓名")
55+
.ConvertName("address", "地址")
56+
.ConvertName("sex", "性别")
57+
.AnyCol(() -> new String[]{"name", "address", "sex"})
58+
.SetCellStyle("name", cellStyle ->
59+
cellStyle.setAlignment(HorizontalAlignment.CENTER)
60+
.setFillBackgroundColor(HSSFColor.RED.index))
61+
.SetCellStyle("address", cellStyle -> cellStyle
62+
.setFillPattern(FillPatternType.BRICKS)
63+
.setAlignment(HorizontalAlignment.RIGHT)
64+
.setFillForegroundColor(HSSFColor.WHITE.index)
65+
.setBottomBorderColor(HSSFColor.RED.index)
66+
.setFillBackgroundColor(HSSFColor.GOLD.index)
67+
.setRightBorderColor(HSSFColor.INDIGO.index)
68+
).Flush();
69+
```
4870
## 数据库导出自定义Bean类型写法(xxx.Class类型)
4971
```java
5072
ExcelFactory.saveExcel(

UPDATELOG.MD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## 更新纪录
22

3+
### 更新2017/01/11
4+
* 增加AnyCol来对应FilterCol方法,只保留AnyCol类列
5+
* 增加SetCellStyle,支持方法链式设置列单元格风格
6+
37
### 更新2017/01/09
48
* 增加SetPath方法,随时切换保存路径
59
* 增加ConvertName方法,方便自定义Excel列名称

src/main/java/seven/ExcelSuperInterface.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@
2121
* Created by seven on 2016/12/1.
2222
*/
2323
public interface ExcelSuperInterface {
24+
2425
}

src/main/java/seven/callBack/DataFiterColumnInterface.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
*/
88
@FunctionalInterface
99
public interface DataFiterColumnInterface {
10-
public String[] Filter();
10+
public String[] filter();
1111
}

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

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

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

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

1818
import seven.ExcelSuperInterface;
19+
import seven.callBack.CellStyleInterface;
1920
import seven.callBack.DataFilterColumnInterface;
2021
import seven.callBack.DataFilterInterface;
2122
import seven.callBack.DataFilterProcessInterface;
@@ -31,26 +32,36 @@
3132
*/
3233
public interface SaveExcel extends ExcelSuperInterface {
3334

35+
/**
36+
* 请使用Flush
37+
*
38+
* @throws Exception
39+
*/
3440
@Deprecated
35-
void Save() throws Exception;
41+
void Save() throws Exception;
42+
3643
/**
3744
* 对要包装的数据进行过滤,对应实体Bean\n
3845
* 如果返回false将放弃此条数据
46+
*
3947
* @param filter {@link DataFilterInterface}
4048
* @return
4149
*/
4250
SaveExcel Filter(DataFilterInterface<?> filter);
4351

4452
/**
45-
*此处传入每一行打包好的数据。对应一个实体\n
53+
* 此处传入每一行打包好的数据。对应一个实体\n
4654
* 在process方法里可对属性进行处理加工
55+
*
4756
* @param process {@link DataFilterProcessInterface}
4857
* @return
4958
*/
5059
SaveExcel Process(DataFilterProcessInterface<?> process);
5160

61+
5262
/**
53-
* 对结果的List进行排序
63+
* 对结果的List进行排序
64+
*
5465
* @param c
5566
* @return
5667
*/
@@ -59,49 +70,71 @@ public interface SaveExcel extends ExcelSuperInterface {
5970
/**
6071
* 此处过滤Excel的列数据(列名)\n
6172
* 如果加入后,将不对实体进行赋值
73+
*
6274
* @param df {@link DataFilterColumnInterface}
6375
*/
6476
SaveExcel FilterCol(DataFilterColumnInterface df);
6577

78+
/**
79+
* 此处过滤Excel的列数据(列名)\n
80+
* 如果加入后,只要这些列
81+
*
82+
* @param df {@link DataFilterColumnInterface}
83+
*/
84+
SaveExcel AnyCol(DataFilterColumnInterface df);
85+
6686
/**
6787
* 网页输出流
88+
*
6889
* @param stream
6990
* @return
7091
*/
7192
SaveExcel SetOutputStream(OutputStream stream) throws Exception;
7293

7394
/**
74-
*
7595
* @throws Exception
7696
*/
77-
void Flush() throws Exception ;
97+
void Flush() throws Exception;
7898

7999
/**
80100
* 设置保存路径
101+
*
81102
* @param path
82103
*/
83104
SaveExcel SetPath(String path);
84105

85106
/**
86107
* 转换字段名称
108+
*
87109
* @param title
88110
* @param new_title
89111
* @return
90112
*/
91-
SaveExcel ConvertName(String title,String new_title);
113+
SaveExcel ConvertName(String title, String new_title);
92114

93115
/**
94116
* 转换字段
117+
*
95118
* @param title_mapping
96119
* @return
97120
*/
98-
SaveExcel ConvertName(HashMap<String,String> title_mapping);
121+
SaveExcel ConvertName(HashMap<String, String> title_mapping);
99122

100123
/**
101124
* 转换字段
125+
*
102126
* @param title_mapping
103127
* @return
104128
*/
105-
SaveExcel ConvertName(HashMap<String,String> title_mapping,Boolean is_init);
129+
SaveExcel ConvertName(HashMap<String, String> title_mapping, Boolean is_init);
106130

131+
/**
132+
* 设置风格.必须保证wk不能为null。
133+
*
134+
* @param name
135+
* @param cellStyle
136+
* @return
137+
*/
138+
SaveExcel SetCellStyle(String name, CellStyleInterface cellStyle);
107139
}
140+

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

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99
// `;_: `"'
1010
// .'"""""`.
1111
// /, ya ,\\
12-
// //狗神保佑\\
12+
// //狗神保佑 \\
1313
// `-._______.-'
1414
// ___`. | .'___
1515
// (______|______)
1616
//=======================================================
1717

18+
import org.apache.poi.ss.usermodel.Cell;
19+
import org.apache.poi.ss.usermodel.Row;
1820
import org.apache.poi.ss.usermodel.Workbook;
19-
import seven.callBack.DataFilterColumnInterface;
20-
import seven.callBack.DataFilterInterface;
21-
import seven.callBack.DataFilterProcessInterface;
21+
import seven.callBack.*;
2222
import seven.callBack.imp.DefaultDataFilter;
2323
import seven.callBack.imp.DefaultDataProFilter;
2424
import seven.savewapper.SaveExcel;
25+
import seven.savewapper.cellStyle.CellStyle;
2526
import seven.util.ExcelTool;
2627

2728
import java.io.FileOutputStream;
@@ -42,6 +43,7 @@ public abstract class SaveExcelObject<T> implements SaveExcel {
4243
public static final String DEFAULT_TYPE = "xlsx";
4344
protected String path;
4445
protected List<String> filterColBy_key = new ArrayList<>();
46+
protected List<String> anyColBy_key = new ArrayList<>();
4547
protected DataFilterInterface filter = new DefaultDataFilter<Object>();
4648
protected DataFilterProcessInterface process = new DefaultDataProFilter<Object>();
4749
protected Comparator<? super Object> c = null;
@@ -50,6 +52,9 @@ public abstract class SaveExcelObject<T> implements SaveExcel {
5052
protected Boolean isResponse = false;
5153
protected Workbook wk = null;
5254
protected HashMap<String, String> convert_title = new HashMap<>();
55+
protected HashMap<String, CellStyle> cell_style = new HashMap<>();
56+
protected List<CellStyleCallbackInterface> cellStyleCallbackInterfaces=new ArrayList<>();
57+
5358

5459
public SaveExcelObject(List<T> list, String path) {
5560
this.list = list;
@@ -76,6 +81,9 @@ public SaveExcelObject Filter(DataFilterInterface<?> filter) {
7681
}
7782

7883
protected Workbook createWK() throws Exception {
84+
if(wk!=null){
85+
return wk;
86+
}
7987
return wk = ExcelTool.newInstance(path.equals("") ? DEFAULT_TYPE : path, true);
8088
}
8189

@@ -117,11 +125,12 @@ protected OutputStream createStream() throws Exception {
117125
/**
118126
* TempName
119127
*/
120-
private String title_=null;
128+
private String title_ = null;
129+
121130
protected String convertTitle(String title) throws Exception {
122-
title_=null;
123-
title_=convert_title.get(title);
124-
return title_==null?title:title_;
131+
title_ = null;
132+
title_ = convert_title.get(title);
133+
return title_ == null ? title : title_;
125134
}
126135

127136
@Override
@@ -160,4 +169,45 @@ public SaveExcel ConvertName(HashMap<String, String> title_mapping, Boolean is_i
160169
}
161170
return ConvertName(title_mapping);
162171
}
172+
173+
@Override
174+
public SaveExcel SetCellStyle(String name, CellStyleInterface styleInterface){
175+
if(wk==null){
176+
cellStyleCallbackInterfaces.add(new CellStyleCallbackInterface(name,styleInterface));
177+
return this;
178+
}
179+
cell_style.put(name, styleInterface.create(CellStyle.CreateStyle(wk.createCellStyle())));
180+
return this;
181+
}
182+
183+
protected void tryCreateCellStyle() throws Exception{
184+
if(wk==null){
185+
throw new Exception("请输入路径并且初始化WK对象");
186+
}
187+
for (CellStyleCallbackInterface c:cellStyleCallbackInterfaces){
188+
c.create(wk,cell_style);
189+
}
190+
}
191+
192+
@Override
193+
public SaveExcel AnyCol(DataFilterColumnInterface df) {
194+
for (String s : df.filter()) {
195+
anyColBy_key.add(s);
196+
}
197+
return this;
198+
}
199+
200+
201+
202+
protected void initTitle(String[] title, Row row, org.apache.poi.ss.usermodel.CellStyle defStyle) throws Exception{
203+
for (short i = 0; i < title.length; i++) {
204+
Cell cell = row.createCell(i);
205+
cell.setCellStyle(defStyle);
206+
if(cell_style.containsKey(title[i])){
207+
cell.setCellStyle(cell_style.get(title[i]).getRealyStyle());
208+
}
209+
cell.setCellValue(convertTitle(title[i]));
210+
}
211+
}
212+
163213
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public ResExprotDBObj CreateList() throws Exception {
7575
return this;
7676
}
7777
while (resultSet.next()) {
78-
list.add(dataInterface.PackageDataProcess(resultSet));
78+
list.add(dataInterface.packageDataProcess(resultSet));
7979
}
8080
return this;
8181
}

0 commit comments

Comments
 (0)