ExcelHeadProperty.java
8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.taover.easyexcel.metadata.property;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.taover.easyexcel.annotation.ExcelProperty;
import com.taover.easyexcel.annotation.format.DateTimeFormat;
import com.taover.easyexcel.annotation.format.NumberFormat;
import com.taover.easyexcel.converters.AutoConverter;
import com.taover.easyexcel.converters.Converter;
import com.taover.easyexcel.enums.HeadKindEnum;
import com.taover.easyexcel.exception.ExcelCommonException;
import com.taover.easyexcel.metadata.Head;
import com.taover.easyexcel.metadata.Holder;
import com.taover.easyexcel.util.ClassUtils;
import com.taover.easyexcel.util.CollectionUtils;
import com.taover.easyexcel.util.StringUtils;
import com.taover.easyexcel.write.metadata.holder.AbstractWriteHolder;
/**
* Define the header attribute of excel
*
* @author jipengfei
*/
public class ExcelHeadProperty {
private static final Logger LOGGER = LoggerFactory.getLogger(ExcelHeadProperty.class);
/**
* Custom class
*/
private Class headClazz;
/**
* The types of head
*/
private HeadKindEnum headKind;
/**
* The number of rows in the line with the most rows
*/
private int headRowNumber;
/**
* Configuration header information
*/
private Map<Integer, Head> headMap;
/**
* Configuration column information
*/
private Map<Integer, ExcelContentProperty> contentPropertyMap;
/**
* Configuration column information
*/
private Map<String, ExcelContentProperty> fieldNameContentPropertyMap;
/**
* Fields ignored
*/
private Map<String, Field> ignoreMap;
public ExcelHeadProperty(Holder holder, Class headClazz, List<List<String>> head, Boolean convertAllFiled) {
this.headClazz = headClazz;
headMap = new TreeMap<Integer, Head>();
contentPropertyMap = new TreeMap<Integer, ExcelContentProperty>();
fieldNameContentPropertyMap = new HashMap<String, ExcelContentProperty>();
ignoreMap = new HashMap<String, Field>(16);
headKind = HeadKindEnum.NONE;
headRowNumber = 0;
if (head != null && !head.isEmpty()) {
int headIndex = 0;
for (int i = 0; i < head.size(); i++) {
if (holder instanceof AbstractWriteHolder) {
if (((AbstractWriteHolder) holder).ignore(null, i)) {
continue;
}
}
headMap.put(headIndex, new Head(headIndex, null, head.get(i), Boolean.FALSE, Boolean.TRUE));
contentPropertyMap.put(headIndex, null);
headIndex++;
}
headKind = HeadKindEnum.STRING;
}
// convert headClazz to head
initColumnProperties(holder, convertAllFiled);
initHeadRowNumber();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("The initialization sheet/table 'ExcelHeadProperty' is complete , head kind is {}", headKind);
}
}
private void initHeadRowNumber() {
headRowNumber = 0;
for (Head head : headMap.values()) {
List<String> list = head.getHeadNameList();
if (list != null && list.size() > headRowNumber) {
headRowNumber = list.size();
}
}
for (Head head : headMap.values()) {
List<String> list = head.getHeadNameList();
if (list != null && !list.isEmpty() && list.size() < headRowNumber) {
int lack = headRowNumber - list.size();
int last = list.size() - 1;
for (int i = 0; i < lack; i++) {
list.add(list.get(last));
}
}
}
}
private void initColumnProperties(Holder holder, Boolean convertAllFiled) {
if (headClazz == null) {
return;
}
// Declared fields
Map<Integer, Field> sortedAllFiledMap = new TreeMap<Integer, Field>();
Map<Integer, Field> indexFiledMap = new TreeMap<Integer, Field>();
boolean needIgnore = (holder instanceof AbstractWriteHolder) && (
!CollectionUtils.isEmpty(((AbstractWriteHolder) holder).getExcludeColumnFiledNames()) || !CollectionUtils
.isEmpty(((AbstractWriteHolder) holder).getExcludeColumnIndexes()) || !CollectionUtils
.isEmpty(((AbstractWriteHolder) holder).getIncludeColumnFiledNames()) || !CollectionUtils
.isEmpty(((AbstractWriteHolder) holder).getIncludeColumnIndexes()));
ClassUtils.declaredFields(headClazz, sortedAllFiledMap, indexFiledMap, ignoreMap, convertAllFiled, needIgnore,
holder);
for (Map.Entry<Integer, Field> entry : sortedAllFiledMap.entrySet()) {
initOneColumnProperty(entry.getKey(), entry.getValue(), indexFiledMap.containsKey(entry.getKey()));
}
headKind = HeadKindEnum.CLASS;
}
/**
* Initialization column property
*
* @param index
* @param field
* @param forceIndex
* @return Ignore current field
*/
private void initOneColumnProperty(int index, Field field, Boolean forceIndex) {
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
List<String> tmpHeadList = new ArrayList<String>();
boolean notForceName = excelProperty == null || excelProperty.value().length <= 0
|| (excelProperty.value().length == 1 && StringUtils.isEmpty((excelProperty.value())[0]));
if (headMap.containsKey(index)) {
tmpHeadList.addAll(headMap.get(index).getHeadNameList());
} else {
if (notForceName) {
tmpHeadList.add(field.getName());
} else {
Collections.addAll(tmpHeadList, excelProperty.value());
}
}
Head head = new Head(index, field.getName(), tmpHeadList, forceIndex, !notForceName);
ExcelContentProperty excelContentProperty = new ExcelContentProperty();
if (excelProperty != null) {
Class<? extends Converter> convertClazz = excelProperty.converter();
if (convertClazz != AutoConverter.class) {
try {
Converter converter = convertClazz.newInstance();
excelContentProperty.setConverter(converter);
} catch (Exception e) {
throw new ExcelCommonException("Can not instance custom converter:" + convertClazz.getName());
}
}
}
excelContentProperty.setHead(head);
excelContentProperty.setField(field);
excelContentProperty
.setDateTimeFormatProperty(DateTimeFormatProperty.build(field.getAnnotation(DateTimeFormat.class)));
excelContentProperty
.setNumberFormatProperty(NumberFormatProperty.build(field.getAnnotation(NumberFormat.class)));
headMap.put(index, head);
contentPropertyMap.put(index, excelContentProperty);
fieldNameContentPropertyMap.put(field.getName(), excelContentProperty);
}
public Class getHeadClazz() {
return headClazz;
}
public void setHeadClazz(Class headClazz) {
this.headClazz = headClazz;
}
public HeadKindEnum getHeadKind() {
return headKind;
}
public void setHeadKind(HeadKindEnum headKind) {
this.headKind = headKind;
}
public boolean hasHead() {
return headKind != HeadKindEnum.NONE;
}
public int getHeadRowNumber() {
return headRowNumber;
}
public void setHeadRowNumber(int headRowNumber) {
this.headRowNumber = headRowNumber;
}
public Map<Integer, Head> getHeadMap() {
return headMap;
}
public void setHeadMap(Map<Integer, Head> headMap) {
this.headMap = headMap;
}
public Map<Integer, ExcelContentProperty> getContentPropertyMap() {
return contentPropertyMap;
}
public void setContentPropertyMap(Map<Integer, ExcelContentProperty> contentPropertyMap) {
this.contentPropertyMap = contentPropertyMap;
}
public Map<String, ExcelContentProperty> getFieldNameContentPropertyMap() {
return fieldNameContentPropertyMap;
}
public void setFieldNameContentPropertyMap(Map<String, ExcelContentProperty> fieldNameContentPropertyMap) {
this.fieldNameContentPropertyMap = fieldNameContentPropertyMap;
}
public Map<String, Field> getIgnoreMap() {
return ignoreMap;
}
public void setIgnoreMap(Map<String, Field> ignoreMap) {
this.ignoreMap = ignoreMap;
}
}