Search in sources :

Example 6 with ExcelContentProperty

use of com.alibaba.excel.metadata.property.ExcelContentProperty in project easyexcel by alibaba.

the class ExcelWriteAddExecutor method doAddBasicTypeToExcel.

private void doAddBasicTypeToExcel(RowData oneRowData, Head head, Row row, int rowIndex, int relativeRowIndex, int dataIndex, int columnIndex) {
    ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null, writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), head == null ? null : head.getFieldName());
    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(oneRowData.get(dataIndex));
    cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(cellWriteHandlerContext.getOriginalValue()));
    converterAndSet(cellWriteHandlerContext);
    WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
}
Also used : CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) Cell(org.apache.poi.ss.usermodel.Cell)

Example 7 with ExcelContentProperty

use of com.alibaba.excel.metadata.property.ExcelContentProperty 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)

Example 8 with ExcelContentProperty

use of com.alibaba.excel.metadata.property.ExcelContentProperty in project easyexcel by alibaba.

the class ClassUtils method declaredFieldContentMap.

private static Map<String, ExcelContentProperty> declaredFieldContentMap(Class<?> clazz) {
    if (clazz == null) {
        return null;
    }
    return CLASS_CONTENT_CACHE.computeIfAbsent(clazz, key -> {
        List<Field> tempFieldList = new ArrayList<>();
        Class<?> tempClass = clazz;
        while (tempClass != null) {
            Collections.addAll(tempFieldList, tempClass.getDeclaredFields());
            // Get the parent class and give it to yourself
            tempClass = tempClass.getSuperclass();
        }
        ContentStyle parentContentStyle = clazz.getAnnotation(ContentStyle.class);
        ContentFontStyle parentContentFontStyle = clazz.getAnnotation(ContentFontStyle.class);
        Map<String, ExcelContentProperty> fieldContentMap = MapUtils.newHashMapWithExpectedSize(tempFieldList.size());
        for (Field field : tempFieldList) {
            ExcelContentProperty excelContentProperty = new ExcelContentProperty();
            excelContentProperty.setField(field);
            ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
            if (excelProperty != null) {
                Class<? extends Converter<?>> convertClazz = excelProperty.converter();
                if (convertClazz != AutoConverter.class) {
                    try {
                        Converter<?> converter = convertClazz.getDeclaredConstructor().newInstance();
                        excelContentProperty.setConverter(converter);
                    } catch (Exception e) {
                        throw new ExcelCommonException("Can not instance custom converter:" + convertClazz.getName());
                    }
                }
            }
            ContentStyle contentStyle = field.getAnnotation(ContentStyle.class);
            if (contentStyle == null) {
                contentStyle = parentContentStyle;
            }
            excelContentProperty.setContentStyleProperty(StyleProperty.build(contentStyle));
            ContentFontStyle contentFontStyle = field.getAnnotation(ContentFontStyle.class);
            if (contentFontStyle == null) {
                contentFontStyle = parentContentFontStyle;
            }
            excelContentProperty.setContentFontProperty(FontProperty.build(contentFontStyle));
            excelContentProperty.setDateTimeFormatProperty(DateTimeFormatProperty.build(field.getAnnotation(DateTimeFormat.class)));
            excelContentProperty.setNumberFormatProperty(NumberFormatProperty.build(field.getAnnotation(NumberFormat.class)));
            fieldContentMap.put(field.getName(), excelContentProperty);
        }
        return fieldContentMap;
    });
}
Also used : ExcelProperty(com.alibaba.excel.annotation.ExcelProperty) ArrayList(java.util.ArrayList) ExcelCommonException(com.alibaba.excel.exception.ExcelCommonException) ContentStyle(com.alibaba.excel.annotation.write.style.ContentStyle) Field(java.lang.reflect.Field) ContentFontStyle(com.alibaba.excel.annotation.write.style.ContentFontStyle) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) ExcelCommonException(com.alibaba.excel.exception.ExcelCommonException)

Example 9 with ExcelContentProperty

use of com.alibaba.excel.metadata.property.ExcelContentProperty in project easyexcel by alibaba.

the class Issue2443Test method parseIntegerTest2.

@Test
public void parseIntegerTest2() throws ParseException {
    String string = "2.00";
    ExcelContentProperty contentProperty = null;
    int Int = NumberUtils.parseInteger(string, contentProperty);
    assertEquals(2, Int);
}
Also used : ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) Test(org.junit.Test)

Example 10 with ExcelContentProperty

use of com.alibaba.excel.metadata.property.ExcelContentProperty in project easyexcel by alibaba.

the class WriteContextImpl method addOneRowOfHeadDataToExcel.

private void addOneRowOfHeadDataToExcel(Row row, Integer rowIndex, Map<Integer, Head> headMap, int relativeRowIndex) {
    for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
        Head head = entry.getValue();
        int columnIndex = entry.getKey();
        ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null, currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), head.getFieldName());
        CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(this, row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.TRUE, excelContentProperty);
        WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);
        Cell cell = row.createCell(columnIndex);
        cellWriteHandlerContext.setCell(cell);
        WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);
        WriteCellData<String> writeCellData = new WriteCellData<>(head.getHeadNameList().get(relativeRowIndex));
        cell.setCellValue(writeCellData.getStringValue());
        cellWriteHandlerContext.setCellDataList(ListUtils.newArrayList(writeCellData));
        cellWriteHandlerContext.setFirstCellData(writeCellData);
        WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
    }
}
Also used : Head(com.alibaba.excel.metadata.Head) WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) Map(java.util.Map) Cell(org.apache.poi.ss.usermodel.Cell)

Aggregations

ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)10 CellWriteHandlerContext (com.alibaba.excel.write.handler.context.CellWriteHandlerContext)5 Map (java.util.Map)4 Head (com.alibaba.excel.metadata.Head)3 WriteCellData (com.alibaba.excel.metadata.data.WriteCellData)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Cell (org.apache.poi.ss.usermodel.Cell)3 ExcelProperty (com.alibaba.excel.annotation.ExcelProperty)2 ContentFontStyle (com.alibaba.excel.annotation.write.style.ContentFontStyle)2 ContentStyle (com.alibaba.excel.annotation.write.style.ContentStyle)2 ExcelCommonException (com.alibaba.excel.exception.ExcelCommonException)2 Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Optional (java.util.Optional)2 AllArgsConstructor (lombok.AllArgsConstructor)2 EqualsAndHashCode (lombok.EqualsAndHashCode)2 Getter (lombok.Getter)2 Setter (lombok.Setter)2