Search in sources :

Example 1 with ExcelWriterBuilder

use of com.alibaba.excel.write.builder.ExcelWriterBuilder in project easyexcel by alibaba.

the class EasyExcelFactory method write.

/**
 * Build excel the write
 *
 * @param file
 *            File to write
 * @param head
 *            Annotate the class for configuration information
 * @return Excel writer builder
 */
public static ExcelWriterBuilder write(File file, Class head) {
    ExcelWriterBuilder excelWriterBuilder = new ExcelWriterBuilder();
    excelWriterBuilder.file(file);
    if (head != null) {
        excelWriterBuilder.head(head);
    }
    return excelWriterBuilder;
}
Also used : ExcelWriterBuilder(com.alibaba.excel.write.builder.ExcelWriterBuilder)

Example 2 with ExcelWriterBuilder

use of com.alibaba.excel.write.builder.ExcelWriterBuilder in project easyexcel by alibaba.

the class EasyExcelFactory method write.

/**
 * Build excel the write
 *
 * @param pathName
 *            File path to write
 * @param head
 *            Annotate the class for configuration information
 * @return Excel writer builder
 */
public static ExcelWriterBuilder write(String pathName, Class head) {
    ExcelWriterBuilder excelWriterBuilder = new ExcelWriterBuilder();
    excelWriterBuilder.file(pathName);
    if (head != null) {
        excelWriterBuilder.head(head);
    }
    return excelWriterBuilder;
}
Also used : ExcelWriterBuilder(com.alibaba.excel.write.builder.ExcelWriterBuilder)

Example 3 with ExcelWriterBuilder

use of com.alibaba.excel.write.builder.ExcelWriterBuilder 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 4 with ExcelWriterBuilder

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

Example 5 with ExcelWriterBuilder

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

the class AbstractSheetWriteHandler method getExcelWriter.

/**
 * 通用的获取ExcelWriter方法
 * @param response HttpServletResponse
 * @param responseExcel ResponseExcel注解
 * @return ExcelWriter
 */
@SneakyThrows(IOException.class)
public ExcelWriter getExcelWriter(HttpServletResponse response, ResponseExcel responseExcel) {
    ExcelWriterBuilder writerBuilder = EasyExcel.write(response.getOutputStream()).registerConverter(LocalDateStringConverter.INSTANCE).registerConverter(LocalDateTimeStringConverter.INSTANCE).autoCloseStream(true).excelType(responseExcel.suffix()).inMemory(responseExcel.inMemory());
    if (StringUtils.hasText(responseExcel.password())) {
        writerBuilder.password(responseExcel.password());
    }
    if (responseExcel.include().length != 0) {
        writerBuilder.includeColumnFiledNames(Arrays.asList(responseExcel.include()));
    }
    if (responseExcel.exclude().length != 0) {
        writerBuilder.excludeColumnFiledNames(Arrays.asList(responseExcel.exclude()));
    }
    if (responseExcel.writeHandler().length != 0) {
        for (Class<? extends WriteHandler> clazz : responseExcel.writeHandler()) {
            writerBuilder.registerWriteHandler(BeanUtils.instantiateClass(clazz));
        }
    }
    // 开启国际化头信息处理
    if (responseExcel.i18nHeader() && i18nHeaderCellWriteHandler != null) {
        writerBuilder.registerWriteHandler(i18nHeaderCellWriteHandler);
    }
    // 自定义注入的转换器
    registerCustomConverter(writerBuilder);
    if (responseExcel.converter().length != 0) {
        for (Class<? extends Converter> clazz : responseExcel.converter()) {
            writerBuilder.registerConverter(BeanUtils.instantiateClass(clazz));
        }
    }
    String templatePath = configProperties.getTemplatePath();
    if (StringUtils.hasText(responseExcel.template())) {
        ClassPathResource classPathResource = new ClassPathResource(templatePath + File.separator + responseExcel.template());
        InputStream inputStream = classPathResource.getInputStream();
        writerBuilder.withTemplate(inputStream);
    }
    writerBuilder = excelWriterBuilderEnhance.enhanceExcel(writerBuilder, response, responseExcel, templatePath);
    return writerBuilder.build();
}
Also used : InputStream(java.io.InputStream) ExcelWriterBuilder(com.alibaba.excel.write.builder.ExcelWriterBuilder) ClassPathResource(org.springframework.core.io.ClassPathResource) SneakyThrows(lombok.SneakyThrows)

Aggregations

ExcelWriterBuilder (com.alibaba.excel.write.builder.ExcelWriterBuilder)7 WriteHandler (com.alibaba.excel.write.handler.WriteHandler)2 LongestMatchColumnWidthStyleStrategy (com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy)2 BusinessException (com.diboot.core.exception.BusinessException)2 CommentWriteHandler (com.diboot.file.excel.write.CommentWriteHandler)2 OptionWriteHandler (com.diboot.file.excel.write.OptionWriteHandler)2 InputStream (java.io.InputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 SneakyThrows (lombok.SneakyThrows)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 ExcelWriterSheetBuilder (com.alibaba.excel.write.builder.ExcelWriterSheetBuilder)1