Search in sources :

Example 1 with ExcelWriterSheetBuilder

use of com.alibaba.excel.write.builder.ExcelWriterSheetBuilder in project excel-spring-boot-starter by pig-mesh.

the class AbstractSheetWriteHandler method sheet.

/**
 * 获取 WriteSheet 对象
 * @param sheet sheet annotation info
 * @param dataClass 数据类型
 * @param template 模板
 * @param bookHeadEnhancerClass 自定义头处理器
 * @return WriteSheet
 */
public WriteSheet sheet(Sheet sheet, Class<?> dataClass, String template, Class<? extends HeadGenerator> bookHeadEnhancerClass) {
    // Sheet 编号和名称
    Integer sheetNo = sheet.sheetNo() >= 0 ? sheet.sheetNo() : null;
    String sheetName = sheet.sheetName();
    // 是否模板写入
    ExcelWriterSheetBuilder writerSheetBuilder = StringUtils.hasText(template) ? EasyExcel.writerSheet(sheetNo) : EasyExcel.writerSheet(sheetNo, sheetName);
    // 头信息增强 1. 优先使用 sheet 指定的头信息增强 2. 其次使用 @ResponseExcel 中定义的全局头信息增强
    Class<? extends HeadGenerator> headGenerateClass = null;
    if (isNotInterface(sheet.headGenerateClass())) {
        headGenerateClass = sheet.headGenerateClass();
    } else if (isNotInterface(bookHeadEnhancerClass)) {
        headGenerateClass = bookHeadEnhancerClass;
    }
    // 定义头信息增强则使用其生成头信息,否则使用 dataClass 来自动获取
    if (headGenerateClass != null) {
        fillCustomHeadInfo(dataClass, bookHeadEnhancerClass, writerSheetBuilder);
    } else if (dataClass != null) {
        writerSheetBuilder.head(dataClass);
        if (sheet.excludes().length > 0) {
            writerSheetBuilder.excludeColumnFiledNames(Arrays.asList(sheet.excludes()));
        }
        if (sheet.includes().length > 0) {
            writerSheetBuilder.includeColumnFiledNames(Arrays.asList(sheet.includes()));
        }
    }
    // sheetBuilder 增强
    writerSheetBuilder = excelWriterBuilderEnhance.enhanceSheet(writerSheetBuilder, sheetNo, sheetName, dataClass, template, headGenerateClass);
    return writerSheetBuilder.build();
}
Also used : ExcelWriterSheetBuilder(com.alibaba.excel.write.builder.ExcelWriterSheetBuilder)

Example 2 with ExcelWriterSheetBuilder

use of com.alibaba.excel.write.builder.ExcelWriterSheetBuilder in project ballcat by ballcat-projects.

the class AbstractSheetWriteHandler method sheet.

/**
 * 获取 WriteSheet 对象
 * @param sheet sheet annotation info
 * @param dataClass 数据类型
 * @param template 模板
 * @param bookHeadEnhancerClass 自定义头处理器
 * @return WriteSheet
 */
public WriteSheet sheet(Sheet sheet, Class<?> dataClass, String template, Class<? extends HeadGenerator> bookHeadEnhancerClass) {
    // Sheet 编号和名称
    Integer sheetNo = sheet.sheetNo() >= 0 ? sheet.sheetNo() : null;
    String sheetName = sheet.sheetName();
    // 是否模板写入
    ExcelWriterSheetBuilder writerSheetBuilder = StringUtils.hasText(template) ? EasyExcel.writerSheet(sheetNo) : EasyExcel.writerSheet(sheetNo, sheetName);
    // 头信息增强 1. 优先使用 sheet 指定的头信息增强 2. 其次使用 @ResponseExcel 中定义的全局头信息增强
    Class<? extends HeadGenerator> headGenerateClass = null;
    if (isNotInterface(sheet.headGenerateClass())) {
        headGenerateClass = sheet.headGenerateClass();
    } else if (isNotInterface(bookHeadEnhancerClass)) {
        headGenerateClass = bookHeadEnhancerClass;
    }
    // 定义头信息增强则使用其生成头信息,否则使用 dataClass 来自动获取
    if (headGenerateClass != null) {
        fillCustomHeadInfo(dataClass, bookHeadEnhancerClass, writerSheetBuilder);
    } else if (dataClass != null) {
        writerSheetBuilder.head(dataClass);
        if (sheet.excludes().length > 0) {
            writerSheetBuilder.excludeColumnFiledNames(Arrays.asList(sheet.excludes()));
        }
        if (sheet.includes().length > 0) {
            writerSheetBuilder.includeColumnFiledNames(Arrays.asList(sheet.includes()));
        }
    }
    // sheetBuilder 增强
    writerSheetBuilder = excelWriterBuilderEnhance.enhanceSheet(writerSheetBuilder, sheetNo, sheetName, dataClass, template, headGenerateClass);
    return writerSheetBuilder.build();
}
Also used : ExcelWriterSheetBuilder(com.alibaba.excel.write.builder.ExcelWriterSheetBuilder)

Example 3 with ExcelWriterSheetBuilder

use of com.alibaba.excel.write.builder.ExcelWriterSheetBuilder 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)

Example 4 with ExcelWriterSheetBuilder

use of com.alibaba.excel.write.builder.ExcelWriterSheetBuilder in project diboot by dibo-software.

the class ExcelHelper method writeDynamicData.

/**
 * 简单将数据写入excel文件
 * <p>默认列宽自适应数据长度, 可自定义</p>
 *
 * @param filePath
 * @param sheetName
 * @param dataList
 * @param writeHandlers
 * @return
 */
@Deprecated
public static boolean writeDynamicData(String filePath, String sheetName, List<List<String>> dataList, WriteHandler... writeHandlers) throws Exception {
    try {
        ExcelWriterBuilder write = EasyExcel.write(filePath);
        write = write.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy());
        for (WriteHandler handler : writeHandlers) {
            write = write.registerWriteHandler(handler);
        }
        ExcelWriterSheetBuilder sheet = write.sheet(sheetName);
        sheet.doWrite(dataList);
        return true;
    } catch (Exception e) {
        log.error("数据写入excel文件失败", e);
        return false;
    }
}
Also used : ExcelWriterBuilder(com.alibaba.excel.write.builder.ExcelWriterBuilder) ExcelWriterSheetBuilder(com.alibaba.excel.write.builder.ExcelWriterSheetBuilder) 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)

Aggregations

ExcelWriterSheetBuilder (com.alibaba.excel.write.builder.ExcelWriterSheetBuilder)4 WriteHandler (com.alibaba.excel.write.handler.WriteHandler)2 LongestMatchColumnWidthStyleStrategy (com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy)2 CommentWriteHandler (com.diboot.file.excel.write.CommentWriteHandler)2 OptionWriteHandler (com.diboot.file.excel.write.OptionWriteHandler)2 ExcelWriterBuilder (com.alibaba.excel.write.builder.ExcelWriterBuilder)1 BusinessException (com.diboot.core.exception.BusinessException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1