Search in sources :

Example 1 with WriteHolder

use of com.alibaba.excel.write.metadata.holder.WriteHolder in project easyexcel by alibaba.

the class ClassUtils method declaredFields.

/**
 * Parsing field in the class
 *
 * @param clazz             Need to parse the class
 * @param sortedAllFieldMap Complete the map of sorts
 * @param indexFieldMap     Use the index to sort fields
 * @param ignoreMap         You want to ignore field map
 * @param needIgnore        If you want to ignore fields need to ignore
 * @param holder            holder
 */
public static void declaredFields(Class<?> clazz, Map<Integer, Field> sortedAllFieldMap, Map<Integer, Field> indexFieldMap, Map<String, Field> ignoreMap, Boolean needIgnore, Holder holder) {
    FieldCache fieldCache = declaredFields(clazz);
    if (fieldCache == null) {
        return;
    }
    if (ignoreMap != null) {
        ignoreMap.putAll(fieldCache.getIgnoreMap());
    }
    Map<Integer, Field> tempIndexFieldMap = indexFieldMap;
    if (tempIndexFieldMap == null) {
        tempIndexFieldMap = MapUtils.newTreeMap();
    }
    tempIndexFieldMap.putAll(fieldCache.getIndexFieldMap());
    Map<Integer, Field> originSortedAllFieldMap = fieldCache.getSortedAllFieldMap();
    if (!needIgnore) {
        sortedAllFieldMap.putAll(originSortedAllFieldMap);
        return;
    }
    int index = 0;
    for (Map.Entry<Integer, Field> entry : originSortedAllFieldMap.entrySet()) {
        Integer key = entry.getKey();
        Field field = entry.getValue();
        // The current field needs to be ignored
        if (((WriteHolder) holder).ignore(entry.getValue().getName(), entry.getKey())) {
            if (ignoreMap != null) {
                ignoreMap.put(field.getName(), field);
            }
            tempIndexFieldMap.remove(index);
        } else {
            // Mandatory sorted fields
            if (tempIndexFieldMap.containsKey(key)) {
                sortedAllFieldMap.put(key, field);
            } else {
                // Check whether the current key is already in use
                while (sortedAllFieldMap.containsKey(index)) {
                    index++;
                }
                sortedAllFieldMap.put(index++, field);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) WriteHolder(com.alibaba.excel.write.metadata.holder.WriteHolder) BeanMap(org.springframework.cglib.beans.BeanMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap)

Example 2 with WriteHolder

use of com.alibaba.excel.write.metadata.holder.WriteHolder in project diboot by dibo-software.

the class FreezePaneWriteHandler method afterSheetCreate.

@Override
public void afterSheetCreate(SheetWriteHandlerContext context) {
    Sheet sheet = context.getWriteSheetHolder().getSheet();
    if (this.colSplit == null) {
        WriteHolder writeHolder = context.getWriteContext().currentWriteHolder();
        Map<Integer, Head> headMap = writeHolder.excelWriteHeadProperty().getHeadMap();
        sheet.createFreezePane(0, headMap.get(0).getHeadNameList().size());
    } else if (this.leftmostColumn == null) {
        sheet.createFreezePane(this.colSplit, this.rowSplit);
    } else {
        sheet.createFreezePane(this.colSplit, this.rowSplit, this.leftmostColumn, this.topRow);
    }
}
Also used : Head(com.alibaba.excel.metadata.Head) WriteHolder(com.alibaba.excel.write.metadata.holder.WriteHolder) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 3 with WriteHolder

use of com.alibaba.excel.write.metadata.holder.WriteHolder in project easyexcel by alibaba.

the class ExcelWriteAddExecutor method addJavaObjectToExcel.

private void addJavaObjectToExcel(Object oneRowData, Row row, int rowIndex, int relativeRowIndex, Map<Integer, Field> sortedAllFieldMap) {
    WriteHolder currentWriteHolder = writeContext.currentWriteHolder();
    BeanMap beanMap = BeanMapUtils.create(oneRowData);
    // Bean the contains of the Map Key method with poor performance,So to create a keySet here
    Set<String> beanKeySet = new HashSet<>(beanMap.keySet());
    Set<String> beanMapHandledSet = new HashSet<>();
    int maxCellIndex = -1;
    // If it's a class it needs to be cast by type
    if (HeadKindEnum.CLASS.equals(writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadKind())) {
        Map<Integer, Head> headMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadMap();
        for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
            int columnIndex = entry.getKey();
            Head head = entry.getValue();
            String name = head.getFieldName();
            if (!beanKeySet.contains(name)) {
                continue;
            }
            ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), name);
            CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(writeContext, row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty);
            WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);
            Cell cell = WorkBookUtil.createCell(row, columnIndex);
            cellWriteHandlerContext.setCell(cell);
            WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);
            cellWriteHandlerContext.setOriginalValue(beanMap.get(name));
            cellWriteHandlerContext.setOriginalFieldClass(head.getField().getType());
            converterAndSet(cellWriteHandlerContext);
            WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
            beanMapHandledSet.add(name);
            maxCellIndex = Math.max(maxCellIndex, columnIndex);
        }
    }
    // Finish
    if (beanMapHandledSet.size() == beanMap.size()) {
        return;
    }
    maxCellIndex++;
    Map<String, Field> ignoreMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getIgnoreMap();
    initSortedAllFieldMapFieldList(oneRowData.getClass(), sortedAllFieldMap);
    for (Map.Entry<Integer, Field> entry : sortedAllFieldMap.entrySet()) {
        Field field = entry.getValue();
        String fieldName = FieldUtils.resolveCglibFieldName(field);
        boolean uselessData = !beanKeySet.contains(fieldName) || beanMapHandledSet.contains(fieldName) || ignoreMap.containsKey(fieldName);
        if (uselessData) {
            continue;
        }
        Object value = beanMap.get(fieldName);
        ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), fieldName);
        CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(writeContext, row, rowIndex, null, maxCellIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty);
        WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);
        // fix https://github.com/alibaba/easyexcel/issues/1870
        // If there is data, it is written to the next cell
        Cell cell = WorkBookUtil.createCell(row, maxCellIndex);
        cellWriteHandlerContext.setCell(cell);
        WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);
        cellWriteHandlerContext.setOriginalValue(value);
        cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(beanMap, fieldName, value));
        converterAndSet(cellWriteHandlerContext);
        WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
        maxCellIndex++;
    }
}
Also used : Head(com.alibaba.excel.metadata.Head) WriteHolder(com.alibaba.excel.write.metadata.holder.WriteHolder) CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) Field(java.lang.reflect.Field) BeanMap(org.springframework.cglib.beans.BeanMap) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) BeanMap(org.springframework.cglib.beans.BeanMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Cell(org.apache.poi.ss.usermodel.Cell) HashSet(java.util.HashSet)

Aggregations

WriteHolder (com.alibaba.excel.write.metadata.holder.WriteHolder)3 Head (com.alibaba.excel.metadata.Head)2 Field (java.lang.reflect.Field)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 BeanMap (org.springframework.cglib.beans.BeanMap)2 ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)1 CellWriteHandlerContext (com.alibaba.excel.write.handler.context.CellWriteHandlerContext)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Cell (org.apache.poi.ss.usermodel.Cell)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1