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

seven.savewapper.wapperRef.SaveExcelObject Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
package seven.savewapper.wapperRef;
//=======================================================
//		          .----.
//		       _.'__    `.
//		   .--(^)(^^)---/!\
//		 .' @          /!!!\
//		 :         ,    !!!!
//		  `-..__.-' _.-\!!!/
//		        `;_:    `"'
//		      .'"""""`.
//		     /,  ya ,\\
//		    //狗神保佑 \\
//		    `-._______.-'
//		    ___`. | .'___
//		   (______|______)
//=======================================================

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import seven.callBack.*;
import seven.callBack.imp.DefaultDataFilter;
import seven.callBack.imp.DefaultDataProFilter;
import seven.savewapper.SaveExcel;
import seven.savewapper.cellStyle.CellStyle;
import seven.util.ExcelTool;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;

/**
 * [Zhihu]https://www.zhihu.com/people/Sweets07
 * [Github]https://github.com/MatrixSeven
 * Created by seven on 2016/11/30.
 */
public abstract class SaveExcelObject implements SaveExcel {
    protected List list;
    public static final String DEFAULT_TYPE = "xlsx";
    protected String path;
    protected List filterColByKey = new ArrayList<>();
    protected List anyColByKey = new ArrayList<>();
    protected DataFilterInterface filter = new DefaultDataFilter();
    protected DataFilterProcessInterface process = new DefaultDataProFilter();
    protected Comparator c = null;
    protected ResultSet resultSet = null;
    protected OutputStream stream = null;
    protected Workbook wk = null;
    protected HashMap convertTitle = new HashMap<>();
    protected HashMap cellStyle = new HashMap<>();
    private List cellStyleCallbackWrappers = new ArrayList<>();


    public SaveExcelObject(List list, String path) {
        this.list = list;
        this.path = path;
    }

    public SaveExcelObject(List list) {
        this.list = list;
    }

    public SaveExcelObject(ResultSet resultSet, String path) {
        this.resultSet = resultSet;
        this.path = path;
    }

    public SaveExcelObject(ResultSet resultSet) {
        this.resultSet = resultSet;
    }

    @Override
    public SaveExcelObject Filter(DataFilterInterface filter) {
        this.filter = filter;
        return this;
    }

    protected Workbook createWK() throws Exception {
        return wk != null ? wk : (wk = ExcelTool.newInstance(path.equals("") ? DEFAULT_TYPE : path, true));
    }

    @Override
    public SaveExcelObject Process(DataFilterProcessInterface process) {
        this.process = process;
        return this;
    }

    @Override
    public SaveExcelObject Sort(Comparator c) {
        this.c = c;
        return this;
    }

    @Override
    public SaveExcelObject FilterCol(Consumer> df) {
        df.accept(filterColByKey);
        return this;
    }

    protected void checkData() throws Exception {
        if (this.list.isEmpty()) {
            throw new Exception("数据为空,请检查数据源");
        }
    }

    protected OutputStream createStream() throws Exception {
        if (stream == null) {
            if (path == null || path.equals(""))
                throw new Exception("请输入路径");
            return new FileOutputStream(path);
        }
        return stream;
    }

    protected String convertTitle(String title) throws Exception {
        String title_ = convertTitle.get(title);
        return title_ == null ? title : title_;
    }

    @Override
    public SaveExcel SetOutputStream(OutputStream stream) throws Exception {
        this.stream = stream;
        return this;
    }

    @Override
    public void Flush() throws Exception {
        this.Save();
    }

    @Override
    public SaveExcel SetPath(String path) {
        this.path = path;
        return this;
    }

    @Override
    public SaveExcel ConvertName(String title, String newTitle) {
        convertTitle.put(title, newTitle);
        return this;
    }

    @Override
    public SaveExcel ConvertName(HashMap titleMapping) {
        convertTitle.putAll(titleMapping);
        return this;
    }

    @Override
    public SaveExcel ConvertName(HashMap titleMapping, Boolean isInit) {
        if (isInit) {
            convertTitle.clear();
        }
        return ConvertName(titleMapping);
    }

    @Override
    public SaveExcel SetCellStyle(String name, CellStyleInterface styleInterface) {
        if (wk == null) {
            cellStyleCallbackWrappers.add(new CellStyleCallbackWrapper(name, styleInterface));
            return this;
        }
        cellStyle.put(name, styleInterface.create(CellStyle.CreateStyle(wk.createCellStyle())));
        return this;
    }

    protected void tryCreateCellStyle() throws Exception {
        if (wk == null) {
            throw new Exception("请输入路径并且初始化WK对象");
        }
        for (CellStyleCallbackWrapper c : cellStyleCallbackWrappers) {
            c.create(wk, cellStyle);
        }
    }

    @Override
    public SaveExcel AnyCol(Consumer> df) {
        df.accept(this.anyColByKey);
        return this;
    }


    protected void initTitle(String[] title, Row row, org.apache.poi.ss.usermodel.CellStyle defStyle) throws Exception {
        for (short i = 0; i < title.length; i++) {
            Cell cell = row.createCell(i);
            cell.setCellStyle(defStyle);
            if (cellStyle.containsKey(title[i])) {
                cell.setCellStyle(cellStyle.get(title[i]).getRealyStyle());
            }
            cell.setCellValue(convertTitle(title[i]));
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy