Search in sources :

Example 6 with Head

use of com.alibaba.excel.metadata.Head in project easyexcel by alibaba.

the class FillStyleAnnotatedTest method fillStyleHandler.

private void fillStyleHandler(File file, File template) throws Exception {
    EasyExcel.write(file, FillData.class).withTemplate(template).sheet().registerWriteHandler(new AbstractVerticalCellStyleStrategy() {

        @Override
        protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
            WriteCellStyle writeCellStyle = new WriteCellStyle();
            WriteFont writeFont = new WriteFont();
            writeCellStyle.setWriteFont(writeFont);
            writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
            writeFont.setBold(true);
            if (context.getColumnIndex() == 0) {
                writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
                writeFont.setColor(IndexedColors.DARK_YELLOW.getIndex());
            }
            if (context.getColumnIndex() == 1) {
                writeCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
                writeFont.setColor(IndexedColors.DARK_RED.getIndex());
            }
            if (context.getColumnIndex() == 2) {
                writeCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
                writeFont.setColor(IndexedColors.DARK_GREEN.getIndex());
            }
            if (context.getColumnIndex() == 3) {
                writeCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
                writeFont.setColor(IndexedColors.DARK_BLUE.getIndex());
            }
            if (context.getColumnIndex() == 4) {
                writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
                writeFont.setColor(IndexedColors.DARK_YELLOW.getIndex());
            }
            if (context.getColumnIndex() == 5) {
                writeCellStyle.setFillForegroundColor(IndexedColors.TEAL.getIndex());
                writeFont.setColor(IndexedColors.DARK_TEAL.getIndex());
            }
            return writeCellStyle;
        }

        @Override
        protected WriteCellStyle headCellStyle(Head head) {
            return null;
        }
    }).doFill(data());
}
Also used : AbstractVerticalCellStyleStrategy(com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy) Head(com.alibaba.excel.metadata.Head) CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) WriteFont(com.alibaba.excel.write.metadata.style.WriteFont)

Example 7 with Head

use of com.alibaba.excel.metadata.Head in project easyexcel by alibaba.

the class ExcelHeadProperty method initHeadRowNumber.

private void initHeadRowNumber() {
    headRowNumber = 0;
    for (Head head : headMap.values()) {
        List<String> list = head.getHeadNameList();
        if (list != null && list.size() > headRowNumber) {
            headRowNumber = list.size();
        }
    }
    for (Head head : headMap.values()) {
        List<String> list = head.getHeadNameList();
        if (list != null && !list.isEmpty() && list.size() < headRowNumber) {
            int lack = headRowNumber - list.size();
            int last = list.size() - 1;
            for (int i = 0; i < lack; i++) {
                list.add(list.get(last));
            }
        }
    }
}
Also used : Head(com.alibaba.excel.metadata.Head)

Example 8 with Head

use of com.alibaba.excel.metadata.Head in project easyexcel by alibaba.

the class ModelBuildEventListener method buildUserModel.

private Object buildUserModel(Map<Integer, ReadCellData<?>> cellDataMap, ReadSheetHolder readSheetHolder, AnalysisContext context) {
    ExcelReadHeadProperty excelReadHeadProperty = readSheetHolder.excelReadHeadProperty();
    Object resultModel;
    try {
        resultModel = excelReadHeadProperty.getHeadClazz().newInstance();
    } catch (Exception e) {
        throw new ExcelDataConvertException(context.readRowHolder().getRowIndex(), 0, new ReadCellData<>(CellDataTypeEnum.EMPTY), null, "Can not instance class: " + excelReadHeadProperty.getHeadClazz().getName(), e);
    }
    Map<Integer, Head> headMap = excelReadHeadProperty.getHeadMap();
    BeanMap dataMap = BeanMapUtils.create(resultModel);
    for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
        Integer index = entry.getKey();
        Head head = entry.getValue();
        String fieldName = head.getFieldName();
        if (!cellDataMap.containsKey(index)) {
            continue;
        }
        ReadCellData<?> cellData = cellDataMap.get(index);
        Object value = ConverterUtils.convertToJavaObject(cellData, head.getField(), ClassUtils.declaredExcelContentProperty(dataMap, readSheetHolder.excelReadHeadProperty().getHeadClazz(), fieldName), readSheetHolder.converterMap(), context, context.readRowHolder().getRowIndex(), index);
        if (value != null) {
            dataMap.put(fieldName, value);
        }
    }
    return resultModel;
}
Also used : Head(com.alibaba.excel.metadata.Head) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) ReadCellData(com.alibaba.excel.metadata.data.ReadCellData) BeanMap(org.springframework.cglib.beans.BeanMap) BeanMap(org.springframework.cglib.beans.BeanMap) Map(java.util.Map) ExcelReadHeadProperty(com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty)

Example 9 with Head

use of com.alibaba.excel.metadata.Head in project diboot by dibo-software.

the class ReadExcelListener method invokeHead.

/**
 * excel表头数据
 */
@Override
public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
    this.headMap.clear();
    fieldHeadMap.clear();
    fieldNameMap.clear();
    headNameMap.clear();
    ExcelReadHeadProperty excelReadHeadProperty = context.currentReadHolder().excelReadHeadProperty();
    for (Map.Entry<Integer, Head> entry : excelReadHeadProperty.getHeadMap().entrySet()) {
        Integer index = entry.getKey();
        Head head = entry.getValue();
        String fieldName = head.getFieldName();
        List<String> headNameList = head.getHeadNameList();
        String name = headNameList.get(headNameList.size() - 1);
        this.headMap.put(index, name);
        fieldHeadMap.put(fieldName, name);
        fieldNameMap.put(index, fieldName);
        headNameMap.put(index, headNameList);
    }
}
Also used : Head(com.alibaba.excel.metadata.Head) ExcelReadHeadProperty(com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty)

Example 10 with Head

use of com.alibaba.excel.metadata.Head in project diboot by dibo-software.

the class CommentWriteHandler method afterCellCreate.

/**
 * 头部批注处理
 */
@Override
public void afterCellCreate(CellWriteHandlerContext context) {
    Head head = context.getHeadData();
    List<String> headNames = head.getHeadNameList();
    int lastHeadIndex = headNames.size() - 1;
    Integer rowIndex = context.getRowIndex();
    if (!context.getHead() || rowIndex != lastHeadIndex && !headNames.get(rowIndex).equals(headNames.get(lastHeadIndex))) {
        return;
    }
    ExcelComment comment = AnnotationUtils.getAnnotation(head.getField(), ExcelComment.class);
    // 空批注不写入
    if (comment == null || V.isEmpty(comment.value())) {
        return;
    }
    Sheet sheet = context.getWriteSheetHolder().getSheet();
    Cell cell = context.getCell();
    cell.setCellComment(buildComment(sheet.createDrawingPatriarch(), cell.getColumnIndex(), sheet.getLastRowNum(), comment.value()));
}
Also used : ExcelComment(com.diboot.file.excel.annotation.ExcelComment) Head(com.alibaba.excel.metadata.Head) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Aggregations

Head (com.alibaba.excel.metadata.Head)19 CellWriteHandlerContext (com.alibaba.excel.write.handler.context.CellWriteHandlerContext)5 AbstractVerticalCellStyleStrategy (com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy)5 Map (java.util.Map)5 WriteCellStyle (com.alibaba.excel.write.metadata.style.WriteCellStyle)4 WriteFont (com.alibaba.excel.write.metadata.style.WriteFont)4 ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)3 ExcelReadHeadProperty (com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty)3 Cell (org.apache.poi.ss.usermodel.Cell)3 Sheet (org.apache.poi.ss.usermodel.Sheet)3 BeanMap (org.springframework.cglib.beans.BeanMap)3 CellWriteHandler (com.alibaba.excel.write.handler.CellWriteHandler)2 RowWriteHandler (com.alibaba.excel.write.handler.RowWriteHandler)2 SheetWriteHandler (com.alibaba.excel.write.handler.SheetWriteHandler)2 WorkbookWriteHandler (com.alibaba.excel.write.handler.WorkbookWriteHandler)2 WriteHandler (com.alibaba.excel.write.handler.WriteHandler)2 WriteHolder (com.alibaba.excel.write.metadata.holder.WriteHolder)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 TreeMap (java.util.TreeMap)2