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

com.mntviews.jreport.JRExportCSV Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
package com.mntviews.jreport;

import com.mntviews.jreport.exception.JRExportException;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.export.*;

import java.io.ByteArrayOutputStream;

public class JRExportCSV implements JRExportAction {
    static final String CONFIGURATION_VARIABLE = "confCsv";
    static final String EXTENSION = "csv";
    static final String MIME_TYPE = "text/csv";


    @Override
    public ByteArrayOutputStream export(JasperPrint jasperPrint, String configScript) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        JRCsvExporter jrCsvExporter = new JRCsvExporter();
        SimpleCsvReportConfiguration configuration = new SimpleCsvReportConfiguration();

        if (configScript != null) {
            Binding binding = new Binding();
            binding.setVariable(CONFIGURATION_VARIABLE, configuration);
            GroovyShell groovyShell = new GroovyShell(binding);
            groovyShell.evaluate(configScript);
        }

        jrCsvExporter.setConfiguration(configuration);
        jrCsvExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        jrCsvExporter.setExporterOutput(new SimpleWriterExporterOutput(outputStream));

        try {
            jrCsvExporter.exportReport();
        } catch (JRException e) {
            throw new JRExportException(e);
        }

        return outputStream;
    }

    @Override
    public String getExtension() {
        return EXTENSION;
    }

    @Override
    public String getMimeType() {
        return MIME_TYPE;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy