Search in sources :

Example 1 with WriteHandler

use of com.alibaba.excel.write.handler.WriteHandler in project easyexcel by alibaba.

the class AbstractWriteHolder method dealStyle.

private void dealStyle(List<WriteHandler> handlerList) {
    WriteHandler styleStrategy = new AbstractVerticalCellStyleStrategy() {

        @Override
        public int order() {
            return OrderConstant.ANNOTATION_DEFINE_STYLE;
        }

        @Override
        protected WriteCellStyle headCellStyle(CellWriteHandlerContext context) {
            Head head = context.getHeadData();
            if (head == null) {
                return null;
            }
            return WriteCellStyle.build(head.getHeadStyleProperty(), head.getHeadFontProperty());
        }

        @Override
        protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
            ExcelContentProperty excelContentProperty = context.getExcelContentProperty();
            return WriteCellStyle.build(excelContentProperty.getContentStyleProperty(), excelContentProperty.getContentFontProperty());
        }
    };
    handlerList.add(styleStrategy);
}
Also used : AbstractVerticalCellStyleStrategy(com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy) Head(com.alibaba.excel.metadata.Head) CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) WriteHandler(com.alibaba.excel.write.handler.WriteHandler) WorkbookWriteHandler(com.alibaba.excel.write.handler.WorkbookWriteHandler) RowWriteHandler(com.alibaba.excel.write.handler.RowWriteHandler) CellWriteHandler(com.alibaba.excel.write.handler.CellWriteHandler) SheetWriteHandler(com.alibaba.excel.write.handler.SheetWriteHandler) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty)

Example 2 with WriteHandler

use of com.alibaba.excel.write.handler.WriteHandler in project easyexcel by alibaba.

the class AbstractWriteHolder method sortAndClearUpHandler.

protected void sortAndClearUpHandler(List<WriteHandler> handlerList, boolean runOwn) {
    // sort
    Map<Integer, List<WriteHandler>> orderExcelWriteHandlerMap = new TreeMap<>();
    for (WriteHandler handler : handlerList) {
        int order = handler.order();
        if (orderExcelWriteHandlerMap.containsKey(order)) {
            orderExcelWriteHandlerMap.get(order).add(handler);
        } else {
            List<WriteHandler> tempHandlerList = new ArrayList<>();
            tempHandlerList.add(handler);
            orderExcelWriteHandlerMap.put(order, tempHandlerList);
        }
    }
    // clean up
    Set<String> alreadyExistedHandlerSet = new HashSet<>();
    List<WriteHandler> cleanUpHandlerList = new ArrayList<>();
    for (Map.Entry<Integer, List<WriteHandler>> entry : orderExcelWriteHandlerMap.entrySet()) {
        for (WriteHandler handler : entry.getValue()) {
            if (handler instanceof NotRepeatExecutor) {
                String uniqueValue = ((NotRepeatExecutor) handler).uniqueValue();
                if (alreadyExistedHandlerSet.contains(uniqueValue)) {
                    continue;
                }
                alreadyExistedHandlerSet.add(uniqueValue);
            }
            cleanUpHandlerList.add(handler);
        }
    }
    // build chain
    if (!runOwn) {
        this.writeHandlerList = new ArrayList<>();
    }
    for (WriteHandler writeHandler : cleanUpHandlerList) {
        buildChain(writeHandler, runOwn);
    }
}
Also used : ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) NotRepeatExecutor(com.alibaba.excel.event.NotRepeatExecutor) WriteHandler(com.alibaba.excel.write.handler.WriteHandler) WorkbookWriteHandler(com.alibaba.excel.write.handler.WorkbookWriteHandler) RowWriteHandler(com.alibaba.excel.write.handler.RowWriteHandler) CellWriteHandler(com.alibaba.excel.write.handler.CellWriteHandler) SheetWriteHandler(com.alibaba.excel.write.handler.SheetWriteHandler) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) HashSet(java.util.HashSet)

Example 3 with WriteHandler

use of com.alibaba.excel.write.handler.WriteHandler in project easyexcel by alibaba.

the class AbstractWriteHolder method dealColumnWidth.

private void dealColumnWidth(List<WriteHandler> handlerList) {
    WriteHandler columnWidthStyleStrategy = new AbstractHeadColumnWidthStyleStrategy() {

        @Override
        protected Integer columnWidth(Head head, Integer columnIndex) {
            if (head == null) {
                return null;
            }
            if (head.getColumnWidthProperty() != null) {
                return head.getColumnWidthProperty().getWidth();
            }
            return null;
        }
    };
    handlerList.add(columnWidthStyleStrategy);
}
Also used : AbstractHeadColumnWidthStyleStrategy(com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy) Head(com.alibaba.excel.metadata.Head) WriteHandler(com.alibaba.excel.write.handler.WriteHandler) WorkbookWriteHandler(com.alibaba.excel.write.handler.WorkbookWriteHandler) RowWriteHandler(com.alibaba.excel.write.handler.RowWriteHandler) CellWriteHandler(com.alibaba.excel.write.handler.CellWriteHandler) SheetWriteHandler(com.alibaba.excel.write.handler.SheetWriteHandler)

Example 4 with WriteHandler

use of com.alibaba.excel.write.handler.WriteHandler in project diboot by dibo-software.

the class ExcelHelper method writeData.

/**
 * 简单将数据写入excel文件
 * <p>默认列宽自适应数据长度、写入单元格下拉选项, 可自定义</p>
 *
 * @param filePath
 * @param sheetName
 * @param dataList
 * @param <T>
 * @return
 */
@Deprecated
public static <T extends BaseExcelModel> boolean writeData(String filePath, String sheetName, List<T> dataList, WriteHandler... writeHandlers) throws Exception {
    try {
        if (V.isEmpty(dataList)) {
            return writeDynamicData(filePath, sheetName, Collections.emptyList());
        }
        Class<T> tClass = (Class<T>) dataList.get(0).getClass();
        ExcelWriterBuilder write = EasyExcel.write(filePath, tClass);
        write = write.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy());
        for (WriteHandler handler : writeHandlers) {
            write = write.registerWriteHandler(handler);
        }
        write.sheet(sheetName).doWrite(dataList);
        return true;
    } catch (Exception e) {
        log.error("数据写入excel文件失败", e);
        return false;
    }
}
Also used : ExcelWriterBuilder(com.alibaba.excel.write.builder.ExcelWriterBuilder) WriteHandler(com.alibaba.excel.write.handler.WriteHandler) OptionWriteHandler(com.diboot.file.excel.write.OptionWriteHandler) CommentWriteHandler(com.diboot.file.excel.write.CommentWriteHandler) LongestMatchColumnWidthStyleStrategy(com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy) BusinessException(com.diboot.core.exception.BusinessException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with WriteHandler

use of com.alibaba.excel.write.handler.WriteHandler in project diboot by dibo-software.

the class ExcelHelper method buildWriteSheet.

/**
 * 构建WriteSheet
 * <p>
 * 默认:自列适应宽、单元格下拉选项(验证)写入,批注写入
 *
 * @param sheetName      可指定sheetName
 * @param columnNameList 需要导出的ExcelModel列字段名称列表,为空时导出所有列
 * @param consumer
 * @param writeHandlers
 */
public static <T> void buildWriteSheet(String sheetName, Collection<String> columnNameList, BiConsumer<CommentWriteHandler, WriteSheet> consumer, WriteHandler... writeHandlers) {
    ExcelWriterSheetBuilder writerSheet = EasyExcel.writerSheet().sheetName(sheetName);
    CommentWriteHandler commentWriteHandler = new CommentWriteHandler();
    writerSheet.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy());
    writerSheet.registerWriteHandler(new OptionWriteHandler());
    writerSheet.registerWriteHandler(commentWriteHandler);
    for (WriteHandler handler : writeHandlers) {
        writerSheet.registerWriteHandler(handler);
    }
    if (V.notEmpty(columnNameList)) {
        writerSheet.includeColumnFiledNames(columnNameList);
    }
    consumer.accept(commentWriteHandler, writerSheet.build());
}
Also used : ExcelWriterSheetBuilder(com.alibaba.excel.write.builder.ExcelWriterSheetBuilder) OptionWriteHandler(com.diboot.file.excel.write.OptionWriteHandler) WriteHandler(com.alibaba.excel.write.handler.WriteHandler) OptionWriteHandler(com.diboot.file.excel.write.OptionWriteHandler) CommentWriteHandler(com.diboot.file.excel.write.CommentWriteHandler) CommentWriteHandler(com.diboot.file.excel.write.CommentWriteHandler) LongestMatchColumnWidthStyleStrategy(com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy)

Aggregations

WriteHandler (com.alibaba.excel.write.handler.WriteHandler)6 CellWriteHandler (com.alibaba.excel.write.handler.CellWriteHandler)3 RowWriteHandler (com.alibaba.excel.write.handler.RowWriteHandler)3 SheetWriteHandler (com.alibaba.excel.write.handler.SheetWriteHandler)3 WorkbookWriteHandler (com.alibaba.excel.write.handler.WorkbookWriteHandler)3 LongestMatchColumnWidthStyleStrategy (com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy)3 CommentWriteHandler (com.diboot.file.excel.write.CommentWriteHandler)3 OptionWriteHandler (com.diboot.file.excel.write.OptionWriteHandler)3 Head (com.alibaba.excel.metadata.Head)2 ExcelWriterBuilder (com.alibaba.excel.write.builder.ExcelWriterBuilder)2 ExcelWriterSheetBuilder (com.alibaba.excel.write.builder.ExcelWriterSheetBuilder)2 BusinessException (com.diboot.core.exception.BusinessException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 NotRepeatExecutor (com.alibaba.excel.event.NotRepeatExecutor)1 ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)1 CellWriteHandlerContext (com.alibaba.excel.write.handler.context.CellWriteHandlerContext)1 AbstractVerticalCellStyleStrategy (com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy)1 AbstractHeadColumnWidthStyleStrategy (com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1