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

com.poiji.save.FileSaverFactory Maven / Gradle / Ivy

package com.poiji.save;

import com.poiji.exception.InvalidExcelFileExtension;
import com.poiji.exception.PoijiExcelType;
import com.poiji.option.PoijiOptions;
import java.io.File;
import java.io.OutputStream;

public final class FileSaverFactory {

    private final Class entity;
    private final PoijiOptions options;

    public FileSaverFactory(final Class entity, final PoijiOptions options) {
        this.entity = entity;
        this.options = options;
    }

    public FileSaver toFile(final File file, final PoijiExcelType excelType) {
        final MappedFields mappedFields = new MappedFields(entity, options).parseEntity();
        final WorkbookSaver workbookSaver = new FileWorkbookSaver(file, mappedFields, options);
        switch (excelType) {
            case XLSX:
                return new XlsxFileSaver(workbookSaver, options);
            case XLS:
                return new XlsFileSaver(workbookSaver, options);
            case CSV:
                return new CsvFileSaver(mappedFields, options, new CsvFileWriter(file));
            default:
                throw new InvalidExcelFileExtension(
                    excelType + " has unsupported extension. 'xlsx' and 'xls' and 'csv' are supported only.");
        }
    }

    public FileSaver toOutputStream(final OutputStream outputStream, final PoijiExcelType excelType) {
        final MappedFields mappedFields = new MappedFields(entity, options).parseEntity();
        final WorkbookSaver workbookSaver = new OutputStreamWorkbookSaver(outputStream, mappedFields, options);
        switch (excelType) {
            case XLSX:
                return new XlsxFileSaver(workbookSaver, options);
            case XLS:
                return new XlsFileSaver(workbookSaver, options);
            case CSV:
                return new CsvFileSaver(mappedFields, options, new CsvOutputStreamWriter(outputStream));
            default:
                throw new InvalidExcelFileExtension(
                    excelType + " has unsupported extension. 'xlsx' and 'xls' and 'csv' are supported only.");
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy