ExcelWriteHeadProperty.java 8.2 KB
Newer Older
Z
zhuangjiaju 已提交
1 2 3 4
package com.alibaba.excel.write.property;

import java.lang.reflect.Field;
import java.util.ArrayList;
Z
zhuangjiaju 已提交
5
import java.util.HashSet;
Z
zhuangjiaju 已提交
6 7
import java.util.List;
import java.util.Map;
Z
zhuangjiaju 已提交
8
import java.util.Set;
Z
zhuangjiaju 已提交
9

Z
zhuangjiaju 已提交
10
import com.alibaba.excel.annotation.format.NumberFormat;
Z
zhuangjiaju 已提交
11
import com.alibaba.excel.annotation.write.style.ColumnWidth;
12 13
import com.alibaba.excel.annotation.write.style.ContentFontStyle;
import com.alibaba.excel.annotation.write.style.ContentLoopMerge;
Z
zhuangjiaju 已提交
14
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
15 16
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.annotation.write.style.HeadFontStyle;
Z
zhuangjiaju 已提交
17
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
18 19
import com.alibaba.excel.annotation.write.style.HeadStyle;
import com.alibaba.excel.annotation.write.style.OnceAbsoluteMerge;
Z
zhuangjiaju 已提交
20 21 22
import com.alibaba.excel.converters.ConverterKeyBuild;
import com.alibaba.excel.converters.DefaultConverterLoader;
import com.alibaba.excel.enums.CellDataTypeEnum;
Z
zhuangjiaju 已提交
23 24 25
import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.metadata.CellRange;
import com.alibaba.excel.metadata.Head;
26
import com.alibaba.excel.metadata.Holder;
Z
zhuangjiaju 已提交
27 28 29
import com.alibaba.excel.metadata.property.ColumnWidthProperty;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.metadata.property.ExcelHeadProperty;
30 31 32
import com.alibaba.excel.metadata.property.FontProperty;
import com.alibaba.excel.metadata.property.LoopMergeProperty;
import com.alibaba.excel.metadata.property.OnceAbsoluteMergeProperty;
Z
zhuangjiaju 已提交
33
import com.alibaba.excel.metadata.property.RowHeightProperty;
34
import com.alibaba.excel.metadata.property.StyleProperty;
Z
zhuangjiaju 已提交
35 36 37 38 39 40 41 42 43

/**
 * Define the header attribute of excel
 *
 * @author jipengfei
 */
public class ExcelWriteHeadProperty extends ExcelHeadProperty {
    private RowHeightProperty headRowHeightProperty;
    private RowHeightProperty contentRowHeightProperty;
44
    private OnceAbsoluteMergeProperty onceAbsoluteMergeProperty;
Z
zhuangjiaju 已提交
45

46 47
    public ExcelWriteHeadProperty(Holder holder, Class headClazz, List<List<String>> head, Boolean convertAllFiled) {
        super(holder, headClazz, head, convertAllFiled);
Z
zhuangjiaju 已提交
48 49 50 51 52 53 54
        if (getHeadKind() != HeadKindEnum.CLASS) {
            return;
        }
        this.headRowHeightProperty =
            RowHeightProperty.build((HeadRowHeight)headClazz.getAnnotation(HeadRowHeight.class));
        this.contentRowHeightProperty =
            RowHeightProperty.build((ContentRowHeight)headClazz.getAnnotation(ContentRowHeight.class));
55 56
        this.onceAbsoluteMergeProperty =
            OnceAbsoluteMergeProperty.build((OnceAbsoluteMerge)headClazz.getAnnotation(OnceAbsoluteMerge.class));
Z
zhuangjiaju 已提交
57 58

        ColumnWidth parentColumnWidth = (ColumnWidth)headClazz.getAnnotation(ColumnWidth.class);
59 60 61 62 63
        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);

Z
zhuangjiaju 已提交
64 65 66 67 68 69 70 71 72 73 74
        for (Map.Entry<Integer, ExcelContentProperty> entry : getContentPropertyMap().entrySet()) {
            Integer index = entry.getKey();
            ExcelContentProperty excelContentPropertyData = entry.getValue();
            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));

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
            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)));
Z
zhuangjiaju 已提交
100 101 102 103 104 105 106 107 108
            // 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)));
                }
            }
        }
Z
zhuangjiaju 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    }

    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;
    }

127 128 129 130 131 132 133 134
    public OnceAbsoluteMergeProperty getOnceAbsoluteMergeProperty() {
        return onceAbsoluteMergeProperty;
    }

    public void setOnceAbsoluteMergeProperty(OnceAbsoluteMergeProperty onceAbsoluteMergeProperty) {
        this.onceAbsoluteMergeProperty = onceAbsoluteMergeProperty;
    }

Z
zhuangjiaju 已提交
135 136 137 138 139 140 141
    /**
     * 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>();
Z
zhuangjiaju 已提交
142 143 144 145 146 147 148 149
        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;
Z
zhuangjiaju 已提交
150
                }
Z
zhuangjiaju 已提交
151
                alreadyRangeSet.add(i + "-" + j);
Z
zhuangjiaju 已提交
152
                String headName = headNameList.get(j);
Z
zhuangjiaju 已提交
153 154
                int lastCol = i;
                int lastRow = j;
Z
zhuangjiaju 已提交
155 156 157
                for (int k = i + 1; k < headList.size(); k++) {
                    if (headList.get(k).getHeadNameList().get(j).equals(headName)) {
                        alreadyRangeSet.add(k + "-" + j);
Z
zhuangjiaju 已提交
158
                        lastCol = k;
Z
zhuangjiaju 已提交
159 160 161 162 163 164 165
                    } else {
                        break;
                    }
                }
                Set<String> tempAlreadyRangeSet = new HashSet<String>();
                outer:
                for (int k = j + 1; k < headNameList.size(); k++) {
Z
zhuangjiaju 已提交
166
                    for (int l = i; l <= lastCol; l++) {
Z
zhuangjiaju 已提交
167
                        if (headList.get(l).getHeadNameList().get(k).equals(headName)) {
Z
zhuangjiaju 已提交
168
                            tempAlreadyRangeSet.add(l + "-" + k);
Z
zhuangjiaju 已提交
169 170 171 172
                        } else {
                            break outer;
                        }
                    }
Z
zhuangjiaju 已提交
173
                    lastRow = k;
Z
zhuangjiaju 已提交
174 175
                    alreadyRangeSet.addAll(tempAlreadyRangeSet);
                }
Z
zhuangjiaju 已提交
176
                if (j == lastRow && i == lastCol) {
Z
zhuangjiaju 已提交
177 178
                    continue;
                }
Z
zhuangjiaju 已提交
179
                cellRangeList.add(new CellRange(j, lastRow, i, lastCol));
Z
zhuangjiaju 已提交
180 181
            }
        }
Z
zhuangjiaju 已提交
182
        return cellRangeList;
Z
zhuangjiaju 已提交
183 184
    }
}