AbstractVerticalCellStyleStrategy.java
2.91 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
package com.taover.easyexcel.write.style;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
import com.taover.easyexcel.metadata.Head;
import com.taover.easyexcel.util.StyleUtil;
import com.taover.easyexcel.write.metadata.style.WriteCellStyle;
/**
* Use the same style for the column
*
* @author Jiaju Zhuang
*/
public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyleStrategy {
private Workbook workbook;
private Map<Integer, CellStyle> headCellStyleCache = new HashMap<Integer, CellStyle>();
private Map<Integer, CellStyle> contentCellStyleCache = new HashMap<Integer, CellStyle>();
@Override
protected void initCellStyle(Workbook workbook) {
this.workbook = workbook;
}
@Override
protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
if (head == null) {
return;
}
int columnIndex = head.getColumnIndex();
if (headCellStyleCache.containsKey(columnIndex)) {
CellStyle cellStyle = headCellStyleCache.get(columnIndex);
if (cellStyle != null) {
cell.setCellStyle(cellStyle);
}
return;
}
WriteCellStyle headCellStyle = headCellStyle(head);
if (headCellStyle == null) {
headCellStyleCache.put(columnIndex, null);
} else {
CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, headCellStyle);
headCellStyleCache.put(columnIndex, cellStyle);
cell.setCellStyle(cellStyle);
}
}
@Override
protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
if (head == null) {
return;
}
int columnIndex = head.getColumnIndex();
if (contentCellStyleCache.containsKey(columnIndex)) {
CellStyle cellStyle = contentCellStyleCache.get(columnIndex);
if (cellStyle != null) {
cell.setCellStyle(cellStyle);
}
return;
}
WriteCellStyle contentCellStyle = contentCellStyle(head);
if (contentCellStyle == null) {
contentCellStyleCache.put(columnIndex, null);
} else {
CellStyle cellStyle = StyleUtil.buildContentCellStyle(workbook, contentCellStyle);
contentCellStyleCache.put(columnIndex, cellStyle);
cell.setCellStyle(cellStyle);
}
}
/**
* Returns the column width corresponding to each column head
*
* @param head Nullable
* @return
*/
protected abstract WriteCellStyle headCellStyle(Head head);
/**
* Returns the column width corresponding to each column head
*
* @param head Nullable
* @return
*/
protected abstract WriteCellStyle contentCellStyle(Head head);
}