All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.houbb.iexcel.hutool.bs.ExcelBs Maven / Gradle / Ivy

There is a newer version: 1.6.0
Show newest version
package com.github.houbb.iexcel.hutool.bs;

import com.github.houbb.heaven.util.common.ArgUtil;
import com.github.houbb.heaven.util.util.CollectionUtil;
import com.github.houbb.iexcel.hutool.core.DefaultExcelCore;
import com.github.houbb.iexcel.hutool.core.ExcelCore;
import com.github.houbb.iexcel.hutool.core.ExcelCoreContext;
import com.github.houbb.iexcel.hutool.support.read.DefaultMapToBeanReader;
import com.github.houbb.iexcel.hutool.support.read.MapToBeanReader;
import com.github.houbb.iexcel.hutool.support.sax.DefaultSaxReadConvert;
import com.github.houbb.iexcel.hutool.support.sax.SaxReadConvert;
import com.github.houbb.iexcel.hutool.support.sax.SaxReadHandler;
import com.github.houbb.iexcel.hutool.support.write.BeanToMapWriter;
import com.github.houbb.iexcel.hutool.support.write.DefaultBeanToMapWriter;

import java.io.File;
import java.util.List;

/**
 * 引导类
 *
 * @param  泛型
 */
public class ExcelBs {
    /**
     * 核心实现
     */
    private ExcelCore excelCore = new DefaultExcelCore<>();

    private MapToBeanReader mapToBeanReader = new DefaultMapToBeanReader<>();

    private BeanToMapWriter beanToMapWriter = new DefaultBeanToMapWriter();

    private SaxReadConvert saxReadConvert = new DefaultSaxReadConvert<>();

    public ExcelBs excelCore(ExcelCore excelCore) {
        ArgUtil.notNull(excelCore, "excelCore");

        this.excelCore = excelCore;
        return this;
    }

    public ExcelBs mapToBeanReader(MapToBeanReader mapToBeanReader) {
        ArgUtil.notNull(mapToBeanReader, "mapToBeanReader");

        this.mapToBeanReader = mapToBeanReader;
        return this;
    }

    public ExcelBs beanToMapWriter(BeanToMapWriter beanToMapWriter) {
        ArgUtil.notNull(beanToMapWriter, "beanToMapWriter");

        this.beanToMapWriter = beanToMapWriter;
        return this;
    }

    public ExcelBs saxReadConvert(SaxReadConvert saxReadConvert) {
        ArgUtil.notNull(saxReadConvert, "saxReadConvert");

        this.saxReadConvert = saxReadConvert;
        return this;
    }

    /**
     * 通过 sax 方式读取
     * @param file 文件
     * @param tClass 类
     * @param saxReadHandler 行处理类
     */
    public void readBySax(Class tClass, SaxReadHandler saxReadHandler, File file) {
        ExcelCoreContext context = new ExcelCoreContext<>();
        context.setFile(file);
        context.setClazz(tClass);
        context.setMapToBeanReader(mapToBeanReader);
        context.setBeanToMapWriter(beanToMapWriter);
        context.setSaxReadConvert(saxReadConvert);
        context.setSaxReadHandler(saxReadHandler);

        excelCore.readBySax(context);
    }

    /**
     * 写列表
     * @param list 列表
     * @param file 目标文件
     */
    public void writeList(List list, File file) {
        if(CollectionUtil.isEmpty(list)) {
            return;
        }

        T first = list.get(0);
        Class tClass = (Class) first.getClass();

        ExcelCoreContext context = new ExcelCoreContext<>();
        context.setFile(file);
        context.setClazz(tClass);
        context.setMapToBeanReader(mapToBeanReader);
        context.setBeanToMapWriter(beanToMapWriter);
        context.setSaxReadConvert(saxReadConvert);
        context.setSaxReadHandler(null);
        context.setRecordList(list);

        excelCore.writeList(context);
    }

    /**
     * 读列表
     * @param file 文件路径
     * @param tClass 类
     * @return 结果
     */
    public List readList(Class tClass, File file) {
        ExcelCoreContext context = new ExcelCoreContext<>();
        context.setFile(file);
        context.setClazz(tClass);
        context.setMapToBeanReader(mapToBeanReader);
        context.setBeanToMapWriter(beanToMapWriter);
        context.setSaxReadConvert(saxReadConvert);
        context.setSaxReadHandler(null);

        return excelCore.readList(context);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy