package com.alibaba.excel.metadata; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.excel.annotation.ExcelColumnNum; import com.alibaba.excel.annotation.ExcelProperty; /** * 表头信息 * * @author jipengfei */ public class ExcelHeadProperty { /** * 表头数据对应的Class */ private Class headClazz; /** * 表头名称 */ private List> head = new ArrayList>(); /** * Excel每列表头数据 */ private List columnPropertyList = new ArrayList(); /** * key:Excel列号,value:表头数据 */ private Map excelColumnPropertyMap1 = new HashMap(); public ExcelHeadProperty(Class headClazz, List> head) { this.headClazz = headClazz; this.head = head; initColumnProperties(); } /** * 初始化每列 */ private void initColumnProperties() { if (this.headClazz != null) { Field[] fields = this.headClazz.getDeclaredFields(); List> headList = new ArrayList>(); for (Field f : fields) { initOneColumnProperty(f); } //对列排序 Collections.sort(columnPropertyList); if (head == null || head.size() == 0) { for (ExcelColumnProperty excelColumnProperty : columnPropertyList) { headList.add(excelColumnProperty.getHead()); } this.head = headList; } } } /** * 初始化一列 * * @param f */ private void initOneColumnProperty(Field f) { ExcelProperty p = f.getAnnotation(ExcelProperty.class); ExcelColumnProperty excelHeadProperty = null; if (p != null) { excelHeadProperty = new ExcelColumnProperty(); excelHeadProperty.setField(f); excelHeadProperty.setHead(Arrays.asList(p.value())); excelHeadProperty.setIndex(p.index()); excelHeadProperty.setFormat(p.format()); excelColumnPropertyMap1.put(p.index(), excelHeadProperty); } else { ExcelColumnNum columnNum = f.getAnnotation(ExcelColumnNum.class); if (columnNum != null) { excelHeadProperty = new ExcelColumnProperty(); excelHeadProperty.setField(f); excelHeadProperty.setIndex(columnNum.value()); excelHeadProperty.setFormat(columnNum.format()); excelColumnPropertyMap1.put(columnNum.value(), excelHeadProperty); } } if (excelHeadProperty != null) { this.columnPropertyList.add(excelHeadProperty); } } /** * 将表头的一行数据,转换为一列一列形式,组成表头 * * @param row 表头中的一行数据 */ public void appendOneRow(List row) { for (int i = 0; i < row.size(); i++) { List list; if (head.size() <= i) { list = new ArrayList(); head.add(list); } else { list = head.get(0); } list.add(row.get(i)); } } /** * 根据Excel中的列号,获取Excel的表头信息 * * @param columnNum 列号 * @return ExcelColumnProperty */ public ExcelColumnProperty getExcelColumnProperty(int columnNum) { ExcelColumnProperty excelColumnProperty = excelColumnPropertyMap1.get(columnNum); if (excelColumnProperty == null) { if (head != null && head.size() > columnNum) { List columnHead = head.get(columnNum); for (ExcelColumnProperty columnProperty : columnPropertyList) { if (headEquals(columnHead, columnProperty.getHead())) { return columnProperty; } } } } return excelColumnProperty; } /** * 根据Excel中的列号,获取Excel的表头信息 * * @param columnNum 列号 * @return ExcelColumnProperty */ public ExcelColumnProperty getExcelColumnProperty1(int columnNum) { return excelColumnPropertyMap1.get(columnNum); } /** * 判断表头是否相同 * * @param columnHead * @param head * @return */ private boolean headEquals(List columnHead, List head) { boolean result = true; if (columnHead == null || head == null || columnHead.size() != head.size()) { return false; } else { for (int i = 0; i < head.size(); i++) { if (!head.get(i).equals(columnHead.get(i))) { result = false; break; } } } return result; } public Class getHeadClazz() { return headClazz; } public void setHeadClazz(Class headClazz) { this.headClazz = headClazz; } public List> getHead() { return this.head; } public void setHead(List> head) { this.head = head; } public List getColumnPropertyList() { return columnPropertyList; } public void setColumnPropertyList(List columnPropertyList) { this.columnPropertyList = columnPropertyList; } public List getCellRangeModels() { List rangs = new ArrayList(); for (int i = 0; i < head.size(); i++) { List columnvalues = head.get(i); for (int j = 0; j < columnvalues.size(); j++) { int lastRow = getLastRangRow(j, columnvalues.get(j), columnvalues); int lastColumn = getLastRangColumn(columnvalues.get(j), getHeadByRowNum(j), i); if (lastRow >= 0 && lastColumn >= 0 && (lastRow > j || lastColumn > i)) { rangs.add(new CellRange(j, lastRow, i, lastColumn)); } } } return rangs; } public List getHeadByRowNum(int rowNum) { List l = new ArrayList(head.size()); for (List list : head) { if (list.size() > rowNum) { l.add(list.get(rowNum)); } else { l.add(list.get(list.size() - 1)); } } return l; } /** * @param value * @param headByRowNum * @param i * @return */ private int getLastRangColumn(String value, List headByRowNum, int i) { if (headByRowNum.indexOf(value) < i) { return -1; } else { return headByRowNum.lastIndexOf(value); } } private int getLastRangRow(int j, String value, List columnvalue) { if (columnvalue.indexOf(value) < j) { return -1; } if (value != null && value.equals(columnvalue.get(columnvalue.size() - 1))) { return getRowNum() - 1; } else { return columnvalue.lastIndexOf(value); } } public int getRowNum() { int headRowNum = 0; for (List list : head) { if (list != null && list.size() > 0) { if (list.size() > headRowNum) { headRowNum = list.size(); } } } return headRowNum; } }