Search in sources :

Example 11 with Head

use of com.alibaba.excel.metadata.Head 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 12 with Head

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

the class OptionWriteHandler method beforeCellCreate.

/**
 * 设置单元格下拉框选项
 */
@Override
public void beforeCellCreate(CellWriteHandlerContext context) {
    Sheet sheet = context.getWriteSheetHolder().getSheet();
    if (sheet.getLastRowNum() != 0) {
        return;
    }
    Head head = context.getHeadData();
    ExcelOption option = AnnotationUtils.getAnnotation(head.getField(), ExcelOption.class);
    if (option == null) {
        return;
    }
    String[] options = null;
    String dictType = option.dict();
    if (S.isNotEmpty(dictType)) {
        options = getDictOptions(dictType);
    }
    if (V.isEmpty(options)) {
        options = option.options();
    }
    // 下拉框选为空时不添加 单元格验证(单元下拉选项)
    if (V.isEmpty(options)) {
        return;
    }
    // 单元格范围地址
    int col = context.getColumnIndex();
    int rows = option.rows();
    int firstRow = -1;
    int lastRow = rows > 0 ? (firstRow = head.getHeadNameList().size()) - 1 + rows : -1;
    CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, col, col);
    // 设置数据验证
    DataValidationHelper helper = sheet.getDataValidationHelper();
    DataValidationConstraint constraint = helper.createExplicitListConstraint(options);
    DataValidation dataValidation = helper.createValidation(constraint, addressList);
    dataValidation.setErrorStyle(option.errorStyle());
    // 处理Excel兼容性问题
    if (dataValidation instanceof XSSFDataValidation) {
        dataValidation.setSuppressDropDownArrow(true);
        dataValidation.setShowErrorBox(true);
    } else {
        dataValidation.setSuppressDropDownArrow(false);
    }
    sheet.addValidationData(dataValidation);
}
Also used : XSSFDataValidation(org.apache.poi.xssf.usermodel.XSSFDataValidation) Head(com.alibaba.excel.metadata.Head) ExcelOption(com.diboot.file.excel.annotation.ExcelOption) DataValidationConstraint(org.apache.poi.ss.usermodel.DataValidationConstraint) CellRangeAddressList(org.apache.poi.ss.util.CellRangeAddressList) DataValidationHelper(org.apache.poi.ss.usermodel.DataValidationHelper) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFDataValidation(org.apache.poi.xssf.usermodel.XSSFDataValidation) DataValidation(org.apache.poi.ss.usermodel.DataValidation) DataValidationConstraint(org.apache.poi.ss.usermodel.DataValidationConstraint)

Example 13 with Head

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

the class ExcelWriteAddExecutor method addBasicTypeToExcel.

private void addBasicTypeToExcel(RowData oneRowData, Row row, int rowIndex, int relativeRowIndex) {
    if (oneRowData.isEmpty()) {
        return;
    }
    Map<Integer, Head> headMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadMap();
    int dataIndex = 0;
    int maxCellIndex = -1;
    for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
        if (dataIndex >= oneRowData.size()) {
            return;
        }
        int columnIndex = entry.getKey();
        Head head = entry.getValue();
        doAddBasicTypeToExcel(oneRowData, head, row, rowIndex, relativeRowIndex, dataIndex++, columnIndex);
        maxCellIndex = Math.max(maxCellIndex, columnIndex);
    }
    // Finish
    if (dataIndex >= oneRowData.size()) {
        return;
    }
    // fix https://github.com/alibaba/easyexcel/issues/1702
    // If there is data, it is written to the next cell
    maxCellIndex++;
    int size = oneRowData.size() - dataIndex;
    for (int i = 0; i < size; i++) {
        doAddBasicTypeToExcel(oneRowData, null, row, rowIndex, relativeRowIndex, dataIndex++, maxCellIndex++);
    }
}
Also used : Head(com.alibaba.excel.metadata.Head) BeanMap(org.springframework.cglib.beans.BeanMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 14 with Head

use of com.alibaba.excel.metadata.Head 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 15 with Head

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

the class DefaultAnalysisEventProcessor method buildHead.

private void buildHead(AnalysisContext analysisContext, Map<Integer, ReadCellData<?>> cellDataMap) {
    if (!HeadKindEnum.CLASS.equals(analysisContext.currentReadHolder().excelReadHeadProperty().getHeadKind())) {
        return;
    }
    Map<Integer, String> dataMap = ConverterUtils.convertToStringMap(cellDataMap, analysisContext);
    ExcelReadHeadProperty excelHeadPropertyData = analysisContext.readSheetHolder().excelReadHeadProperty();
    Map<Integer, Head> headMapData = excelHeadPropertyData.getHeadMap();
    Map<Integer, Head> tmpHeadMap = new HashMap<Integer, Head>(headMapData.size() * 4 / 3 + 1);
    for (Map.Entry<Integer, Head> entry : headMapData.entrySet()) {
        Head headData = entry.getValue();
        if (headData.getForceIndex() || !headData.getForceName()) {
            tmpHeadMap.put(entry.getKey(), headData);
            continue;
        }
        List<String> headNameList = headData.getHeadNameList();
        String headName = headNameList.get(headNameList.size() - 1);
        for (Map.Entry<Integer, String> stringEntry : dataMap.entrySet()) {
            if (stringEntry == null) {
                continue;
            }
            String headString = stringEntry.getValue();
            Integer stringKey = stringEntry.getKey();
            if (StringUtils.isEmpty(headString)) {
                continue;
            }
            if (analysisContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
                headString = headString.trim();
            }
            if (headName.equals(headString)) {
                headData.setColumnIndex(stringKey);
                tmpHeadMap.put(stringKey, headData);
                break;
            }
        }
    }
    excelHeadPropertyData.setHeadMap(tmpHeadMap);
}
Also used : Head(com.alibaba.excel.metadata.Head) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ExcelReadHeadProperty(com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty)

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