Skip to content

Commit f94beeb

Browse files
committed
Merge branch '1.0.x'
2 parents 711fbca + 7f7bd0f commit f94beeb

File tree

10 files changed

+314
-143
lines changed

10 files changed

+314
-143
lines changed

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

Lines changed: 183 additions & 63 deletions
Large diffs are not rendered by default.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class ExcelReadFieldMappingAttribute implements Serializable {
5959
private boolean required = false;
6060
private ExcelReadCellProcessor cellProcessor;
6161
private ExcelReadCellValueMapping valueMapping;
62+
private String linkField;
6263

6364
public ExcelReadFieldMappingAttribute setRequired(boolean required) {
6465
this.required = required;
@@ -75,6 +76,11 @@ public ExcelReadFieldMappingAttribute setValueMapping(ExcelReadCellValueMapping
7576
return this;
7677
}
7778

79+
public ExcelReadFieldMappingAttribute setLinkField(String linkField) {
80+
this.linkField = linkField;
81+
return this;
82+
}
83+
7884
public boolean isRequired() {
7985
return required;
8086
}
@@ -87,5 +93,9 @@ public ExcelReadCellValueMapping getValueMapping() {
8793
return valueMapping;
8894
}
8995

96+
public String getLinkField() {
97+
return linkField;
98+
}
99+
90100
}
91101
}

src/main/java/org/hellojavaer/poi/excel/utils/write/ExcelWriteFieldMapping.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Map;
2121
import java.util.concurrent.ConcurrentHashMap;
2222

23+
import org.apache.poi.common.usermodel.Hyperlink;
2324
import org.hellojavaer.poi.excel.utils.ExcelUtils;
2425
import org.springframework.util.Assert;
2526

@@ -62,6 +63,8 @@ public class ExcelWriteFieldMappingAttribute implements Serializable {
6263
private ExcelWriteCellProcessor cellProcessor;
6364
private ExcelWriteCellValueMapping valueMapping;
6465
private String head;
66+
private String linkField;
67+
private int linkType;
6568

6669
@SuppressWarnings("rawtypes")
6770
public ExcelWriteFieldMappingAttribute setCellProcessor(ExcelWriteCellProcessor cellProcessor) {
@@ -79,6 +82,18 @@ public ExcelWriteFieldMappingAttribute setHead(String head) {
7982
return this;
8083
}
8184

85+
public ExcelWriteFieldMappingAttribute setLinkField(String linkField) {
86+
this.linkField = linkField;
87+
this.linkType = Hyperlink.LINK_URL;
88+
return this;
89+
}
90+
91+
public ExcelWriteFieldMappingAttribute setLink(String linkFieldName, int linkType) {
92+
this.linkField = linkFieldName;
93+
this.linkType = Hyperlink.LINK_URL;
94+
return this;
95+
}
96+
8297
@SuppressWarnings("rawtypes")
8398
public ExcelWriteCellProcessor getCellProcessor() {
8499
return cellProcessor;
@@ -92,5 +107,13 @@ public String getHead() {
92107
return head;
93108
}
94109

110+
public String getLinkField() {
111+
return linkField;
112+
}
113+
114+
public int getLinkType() {
115+
return linkType;
116+
}
117+
95118
}
96119
}

src/main/java/org/hellojavaer/poi/excel/utils/write/ExcelWriteSheetProcessor.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public abstract class ExcelWriteSheetProcessor<T> {
2626

2727
public abstract void beforeProcess(ExcelWriteContext<T> context);
2828

29-
public abstract List<T> getDataList(ExcelWriteContext<T> context);
30-
3129
public abstract void onException(ExcelWriteContext<T> context, RuntimeException e);
3230

3331
public abstract void afterProcess(ExcelWriteContext<T> context);
@@ -41,6 +39,8 @@ public abstract class ExcelWriteSheetProcessor<T> {
4139
private ExcelWriteRowProcessor<T> rowProcessor;
4240
private boolean trimSpace = false;
4341
private Integer headRowIndex;
42+
private List<T> dataList;
43+
private Integer theme;
4444

4545
public Integer getSheetIndex() {
4646
return sheetIndex;
@@ -112,4 +112,20 @@ public void setHeadRowIndex(Integer headRowIndex) {
112112
this.headRowIndex = headRowIndex;
113113
}
114114

115+
public List<T> getDataList() {
116+
return dataList;
117+
}
118+
119+
public void setDataList(List<T> dataList) {
120+
this.dataList = dataList;
121+
}
122+
123+
public Integer getTheme() {
124+
return theme;
125+
}
126+
127+
public void setTheme(Integer theme) {
128+
this.theme = theme;
129+
}
130+
115131
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.write;
17+
18+
/**
19+
* @author <a href="mailto:[email protected]">zoukaiming</a>
20+
*/
21+
public class ExcelWriteTheme {
22+
23+
public static final int BASE = 0;
24+
25+
}

src/test/java/org/hellojavaer/poi/excel/utils/TestBean.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class TestBean implements Serializable {
2121
private Date dateField;
2222
private String enumField1;
2323
private String enumField2;
24+
private String url;
2425

2526
public Byte getByteField() {
2627
return byteField;
@@ -110,4 +111,12 @@ public void setEnumField2(String enumField2) {
110111
this.enumField2 = enumField2;
111112
}
112113

114+
public String getUrl() {
115+
return url;
116+
}
117+
118+
public void setUrl(String url) {
119+
this.url = url;
120+
}
121+
113122
}

src/test/java/org/hellojavaer/poi/excel/utils/read/ReadDemo1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void afterProcess(ExcelReadContext<TestBean> context) {
6161
}
6262
};
6363
ExcelReadFieldMapping fieldMapping = new ExcelReadFieldMapping();
64-
fieldMapping.put("A", "byteField").setRequired(true);
64+
fieldMapping.put("A", "byteField").setRequired(true).setLinkField("url");
6565
fieldMapping.put("B", "shortField");
6666
fieldMapping.put("C", "intField");
6767
fieldMapping.put("D", "longField");

src/test/java/org/hellojavaer/poi/excel/utils/write/WriteDemo1.java

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,6 @@ public void beforeProcess(ExcelWriteContext<TestBean> context) {
3535
System.out.println("write excel start!");
3636
}
3737

38-
@Override
39-
public List<TestBean> getDataList(ExcelWriteContext<TestBean> context) {
40-
if (b.get() == false) {
41-
b.set(true);
42-
List<TestBean> list = new ArrayList<TestBean>();
43-
TestBean bean = new TestBean();
44-
bean.setByteField((byte) 1);
45-
bean.setShortField((short) 2);
46-
bean.setIntField(3);
47-
bean.setLongField(4L);
48-
bean.setFloatField(5.1f);
49-
bean.setDoubleField(6.23);
50-
bean.setBoolField(true);
51-
bean.setEnumField1("enumField1");
52-
bean.setEnumField2("enumField2");
53-
bean.setDateField(new Date());
54-
bean.setStringField("test");
55-
56-
list.add(bean);
57-
list.add(bean);
58-
list.add(bean);
59-
return list;
60-
} else {
61-
return null;
62-
}
63-
}
64-
6538
@Override
6639
public void onException(ExcelWriteContext<TestBean> context, RuntimeException e) {
6740
if (e instanceof ExcelWriteException) {
@@ -107,7 +80,29 @@ public void afterProcess(ExcelWriteContext<TestBean> context) {
10780
sheetProcessor.setFieldMapping(fieldMapping);// required
10881
sheetProcessor.setHeadRowIndex(0);
10982
// sheetProcessor.setTemplateRowIndex(1);
83+
sheetProcessor.setDataList(getDateList());
11084

11185
ExcelUtils.write(ExcelType.XLSX, output, sheetProcessor);
11286
}
87+
88+
private static List<TestBean> getDateList() {
89+
List<TestBean> list = new ArrayList<TestBean>();
90+
TestBean bean = new TestBean();
91+
bean.setByteField((byte) 1);
92+
bean.setShortField((short) 2);
93+
bean.setIntField(3);
94+
bean.setLongField(4L);
95+
bean.setFloatField(5.1f);
96+
bean.setDoubleField(6.23);
97+
bean.setBoolField(true);
98+
bean.setEnumField1("enumField1");
99+
bean.setEnumField2("enumField2");
100+
bean.setDateField(new Date());
101+
bean.setStringField("test");
102+
103+
list.add(bean);
104+
list.add(bean);
105+
list.add(bean);
106+
return list;
107+
}
113108
}

src/test/java/org/hellojavaer/poi/excel/utils/write/WriteDemo2.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,6 @@ public void beforeProcess(ExcelWriteContext<TestBean> context) {
4848
System.out.println("write excel start!");
4949
}
5050

51-
@Override
52-
public List<TestBean> getDataList(ExcelWriteContext<TestBean> context) {
53-
int pageSize = 10;
54-
List<TestBean> list = pageQuery(rowIndex.longValue(), pageSize);
55-
rowIndex.getAndAdd(pageSize);
56-
return list;
57-
}
58-
5951
@Override
6052
public void onException(ExcelWriteContext<TestBean> context, RuntimeException e) {
6153
if (e instanceof ExcelWriteException) {
@@ -117,26 +109,12 @@ public void process(ExcelWriteContext<TestBean> context, Object obj, Cell cell)
117109
// Row row) {
118110
// }
119111
// });
112+
sheetProcessor.setDataList(getDataList());
120113

121114
ExcelUtils.write(excelTemplate, output, sheetProcessor);
122115
}
123116

124-
private static List<TestBean> pageQuery(long rowIndex, int pageSize) {
125-
if (testDataCache == null) {
126-
testDataCache = getInputData();
127-
}
128-
if (rowIndex >= testDataCache.size()) {
129-
return null;
130-
} else {
131-
int endIndex = (int) (rowIndex + pageSize);
132-
if (endIndex > testDataCache.size()) {
133-
endIndex = testDataCache.size();
134-
}
135-
return testDataCache.subList((int) rowIndex, endIndex);
136-
}
137-
}
138-
139-
private static List<TestBean> getInputData() {
117+
private static List<TestBean> getDataList() {
140118
final List<TestBean> re = new ArrayList<TestBean>();
141119
InputStream in = WriteDemo2.class.getResourceAsStream("/excel/xlsx/data_file2.xlsx");
142120
ExcelReadSheetProcessor<TestBean> sheetProcessor = new ExcelReadSheetProcessor<TestBean>() {

src/test/java/org/hellojavaer/poi/excel/utils/write/WriteDemo3.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,6 @@ public void beforeProcess(ExcelWriteContext<Map> context) {
3636
System.out.println("write excel start!");
3737
}
3838

39-
@Override
40-
public List<Map> getDataList(ExcelWriteContext<Map> context) {
41-
if (b.get() == false) {
42-
b.set(true);
43-
List<Map> list = new ArrayList<Map>();
44-
Map<String, Object> map = new HashMap<String, Object>();
45-
map.put("byteField", (byte) 1);
46-
map.put("shortField", (short) 2);
47-
map.put("intField", 3);
48-
map.put("longField", 4L);
49-
map.put("floatField", 5.1f);
50-
map.put("doubleField", 6.23);
51-
map.put("boolField", true);
52-
map.put("dateField", new Date());
53-
map.put("enumField1", "enumField1");
54-
map.put("enumField2", "enumField2");
55-
map.put("stringField", "map_test");
56-
list.add(map);
57-
list.add(map);
58-
list.add(map);
59-
return list;
60-
} else {
61-
return null;
62-
}
63-
}
64-
6539
@Override
6640
public void onException(ExcelWriteContext<Map> context, RuntimeException e) {
6741
if (e instanceof ExcelWriteException) {
@@ -107,7 +81,28 @@ public void afterProcess(ExcelWriteContext<Map> context) {
10781
sheetProcessor.setFieldMapping(fieldMapping);// required
10882
sheetProcessor.setHeadRowIndex(0);
10983
// sheetProcessor.setTemplateRowIndex(1);
84+
sheetProcessor.setDataList(getDateList());
11085

11186
ExcelUtils.write(ExcelType.XLSX, output, sheetProcessor);
11287
}
88+
89+
private static List<Map> getDateList() {
90+
List<Map> list = new ArrayList<Map>();
91+
Map<String, Object> map = new HashMap<String, Object>();
92+
map.put("byteField", (byte) 1);
93+
map.put("shortField", (short) 2);
94+
map.put("intField", 3);
95+
map.put("longField", 4L);
96+
map.put("floatField", 5.1f);
97+
map.put("doubleField", 6.23);
98+
map.put("boolField", true);
99+
map.put("dateField", new Date());
100+
map.put("enumField1", "enumField1");
101+
map.put("enumField2", "enumField2");
102+
map.put("stringField", "map_test");
103+
list.add(map);
104+
list.add(map);
105+
list.add(map);
106+
return list;
107+
}
113108
}

0 commit comments

Comments
 (0)