ExcelWriteHeadProperty.java
8.55 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
package com.taover.easyexcel.write.property;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.taover.easyexcel.annotation.format.NumberFormat;
import com.taover.easyexcel.annotation.write.style.ColumnWidth;
import com.taover.easyexcel.annotation.write.style.ContentFontStyle;
import com.taover.easyexcel.annotation.write.style.ContentLoopMerge;
import com.taover.easyexcel.annotation.write.style.ContentRowHeight;
import com.taover.easyexcel.annotation.write.style.ContentStyle;
import com.taover.easyexcel.annotation.write.style.HeadFontStyle;
import com.taover.easyexcel.annotation.write.style.HeadRowHeight;
import com.taover.easyexcel.annotation.write.style.HeadStyle;
import com.taover.easyexcel.annotation.write.style.OnceAbsoluteMerge;
import com.taover.easyexcel.converters.ConverterKeyBuild;
import com.taover.easyexcel.converters.DefaultConverterLoader;
import com.taover.easyexcel.enums.CellDataTypeEnum;
import com.taover.easyexcel.enums.HeadKindEnum;
import com.taover.easyexcel.metadata.CellRange;
import com.taover.easyexcel.metadata.Head;
import com.taover.easyexcel.metadata.Holder;
import com.taover.easyexcel.metadata.property.ColumnWidthProperty;
import com.taover.easyexcel.metadata.property.ExcelContentProperty;
import com.taover.easyexcel.metadata.property.ExcelHeadProperty;
import com.taover.easyexcel.metadata.property.FontProperty;
import com.taover.easyexcel.metadata.property.LoopMergeProperty;
import com.taover.easyexcel.metadata.property.OnceAbsoluteMergeProperty;
import com.taover.easyexcel.metadata.property.RowHeightProperty;
import com.taover.easyexcel.metadata.property.StyleProperty;
/**
* Define the header attribute of excel
*
* @author jipengfei
*/
public class ExcelWriteHeadProperty extends ExcelHeadProperty {
private RowHeightProperty headRowHeightProperty;
private RowHeightProperty contentRowHeightProperty;
private OnceAbsoluteMergeProperty onceAbsoluteMergeProperty;
public ExcelWriteHeadProperty(Holder holder, Class headClazz, List<List<String>> head, Boolean convertAllFiled) {
super(holder, headClazz, head, convertAllFiled);
if (getHeadKind() != HeadKindEnum.CLASS) {
return;
}
this.headRowHeightProperty =
RowHeightProperty.build((HeadRowHeight) headClazz.getAnnotation(HeadRowHeight.class));
this.contentRowHeightProperty =
RowHeightProperty.build((ContentRowHeight) headClazz.getAnnotation(ContentRowHeight.class));
this.onceAbsoluteMergeProperty =
OnceAbsoluteMergeProperty.build((OnceAbsoluteMerge) headClazz.getAnnotation(OnceAbsoluteMerge.class));
ColumnWidth parentColumnWidth = (ColumnWidth) headClazz.getAnnotation(ColumnWidth.class);
HeadStyle parentHeadStyle = (HeadStyle) headClazz.getAnnotation(HeadStyle.class);
HeadFontStyle parentHeadFontStyle = (HeadFontStyle) headClazz.getAnnotation(HeadFontStyle.class);
ContentStyle parentContentStyle = (ContentStyle) headClazz.getAnnotation(ContentStyle.class);
ContentFontStyle parentContentFontStyle = (ContentFontStyle) headClazz.getAnnotation(ContentFontStyle.class);
for (Map.Entry<Integer, ExcelContentProperty> entry : getContentPropertyMap().entrySet()) {
Integer index = entry.getKey();
ExcelContentProperty excelContentPropertyData = entry.getValue();
if (excelContentPropertyData == null) {
throw new IllegalArgumentException(
"Passing in the class and list the head, the two must be the same size.");
}
Field field = excelContentPropertyData.getField();
Head headData = getHeadMap().get(index);
ColumnWidth columnWidth = field.getAnnotation(ColumnWidth.class);
if (columnWidth == null) {
columnWidth = parentColumnWidth;
}
headData.setColumnWidthProperty(ColumnWidthProperty.build(columnWidth));
HeadStyle headStyle = field.getAnnotation(HeadStyle.class);
if (headStyle == null) {
headStyle = parentHeadStyle;
}
headData.setHeadStyleProperty(StyleProperty.build(headStyle));
HeadFontStyle headFontStyle = field.getAnnotation(HeadFontStyle.class);
if (headFontStyle == null) {
headFontStyle = parentHeadFontStyle;
}
headData.setHeadFontProperty(FontProperty.build(headFontStyle));
ContentStyle contentStyle = field.getAnnotation(ContentStyle.class);
if (contentStyle == null) {
contentStyle = parentContentStyle;
}
headData.setContentStyleProperty(StyleProperty.build(contentStyle));
ContentFontStyle contentFontStyle = field.getAnnotation(ContentFontStyle.class);
if (contentFontStyle == null) {
contentFontStyle = parentContentFontStyle;
}
headData.setContentFontProperty(FontProperty.build(contentFontStyle));
headData.setLoopMergeProperty(LoopMergeProperty.build(field.getAnnotation(ContentLoopMerge.class)));
// If have @NumberFormat, 'NumberStringConverter' is specified by default
if (excelContentPropertyData.getConverter() == null) {
NumberFormat numberFormat = field.getAnnotation(NumberFormat.class);
if (numberFormat != null) {
excelContentPropertyData.setConverter(DefaultConverterLoader.loadAllConverter()
.get(ConverterKeyBuild.buildKey(field.getType(), CellDataTypeEnum.STRING)));
}
}
}
}
public RowHeightProperty getHeadRowHeightProperty() {
return headRowHeightProperty;
}
public void setHeadRowHeightProperty(RowHeightProperty headRowHeightProperty) {
this.headRowHeightProperty = headRowHeightProperty;
}
public RowHeightProperty getContentRowHeightProperty() {
return contentRowHeightProperty;
}
public void setContentRowHeightProperty(RowHeightProperty contentRowHeightProperty) {
this.contentRowHeightProperty = contentRowHeightProperty;
}
public OnceAbsoluteMergeProperty getOnceAbsoluteMergeProperty() {
return onceAbsoluteMergeProperty;
}
public void setOnceAbsoluteMergeProperty(OnceAbsoluteMergeProperty onceAbsoluteMergeProperty) {
this.onceAbsoluteMergeProperty = onceAbsoluteMergeProperty;
}
/**
* Calculate all cells that need to be merged
*
* @return cells that need to be merged
*/
public List<CellRange> headCellRangeList() {
List<CellRange> cellRangeList = new ArrayList<CellRange>();
Set<String> alreadyRangeSet = new HashSet<String>();
List<Head> headList = new ArrayList<Head>(getHeadMap().values());
for (int i = 0; i < headList.size(); i++) {
Head head = headList.get(i);
List<String> headNameList = head.getHeadNameList();
for (int j = 0; j < headNameList.size(); j++) {
if (alreadyRangeSet.contains(i + "-" + j)) {
continue;
}
alreadyRangeSet.add(i + "-" + j);
String headName = headNameList.get(j);
int lastCol = i;
int lastRow = j;
for (int k = i + 1; k < headList.size(); k++) {
if (headList.get(k).getHeadNameList().get(j).equals(headName)) {
alreadyRangeSet.add(k + "-" + j);
lastCol = k;
} else {
break;
}
}
Set<String> tempAlreadyRangeSet = new HashSet<String>();
outer:
for (int k = j + 1; k < headNameList.size(); k++) {
for (int l = i; l <= lastCol; l++) {
if (headList.get(l).getHeadNameList().get(k).equals(headName)) {
tempAlreadyRangeSet.add(l + "-" + k);
} else {
break outer;
}
}
lastRow = k;
alreadyRangeSet.addAll(tempAlreadyRangeSet);
}
if (j == lastRow && i == lastCol) {
continue;
}
cellRangeList
.add(new CellRange(j, lastRow, head.getColumnIndex(), headList.get(lastCol).getColumnIndex()));
}
}
return cellRangeList;
}
}