简体   繁体   中英

How do you set the JRViewer export options?

Recently I updated my project with the newest jasper-reports lib. There is a new JRViewer class (net.sf.jasperreports.swing) which replaced the old one (net.sf.jasperreports.view). Now I can't figure out how to set the export options. The old code was like:

JRPdfSaveContributor pdf = new JRPdfSaveContributor(locale, resBundle);
JRRtfSaveContributor rtf = new JRRtfSaveContributor(locale, resBundle);
JRSingleSheetXlsSaveContributor xls = new JRSingleSheetXlsSaveContributor(locale, resBundle);
JRDocxSaveContributor docx = new JRDocxSaveContributor(locale, resBundle);
viewer.setSaveContributors(new JRSaveContributor[] { pdf, rtf, xls, docx });

You'll have to extend net.sf.jasperreports.swing.JRViewer and set the export contributors to JRViewerToolbar. Something like this:

public class MyJRViewer extends JRViewer {
    //define the constructor that you use
    public MyJRViewer(JasperPrint jasperPrint) {
        super(jasperPrint);
    }

    @Override
    protected JRViewerToolbar createToolbar() {
        JRViewerToolbar toolbar = super.createToolbar();

        Locale locale = viewerContext.getLocale();
        ResourceBundle resBundle = viewerContext.getResourceBundle();
        JRPdfSaveContributor pdf = new JRPdfSaveContributor(locale, resBundle);
        JRRtfSaveContributor rtf = new JRRtfSaveContributor(locale, resBundle);
        JRSingleSheetXlsSaveContributor xls = new JRSingleSheetXlsSaveContributor(locale, resBundle);
        JRDocxSaveContributor docx = new JRDocxSaveContributor(locale, resBundle);
        toolbar.setSaveContributors(new JRSaveContributor[] {pdf, rtf, xls, docx});

        return toolbar;
    }   
}

You can use this methods for having a modular choosing of extension.

First we define an enum that holds all extensions classes, all of these classes are the sub-classes of JRSaveContributor which is for saving extensions .

import net.sf.jasperreports.view.JRSaveContributor;
import net.sf.jasperreports.view.save.*;

public enum Extension {
    PDF(JRPdfSaveContributor.class),
    RTF(JRRtfSaveContributor.class),
    SINGLE_SHEET_XLS(JRSingleSheetXlsSaveContributor.class),
    MULTIPLE_SHEET_XLS(JRMultipleSheetsXlsSaveContributor.class),
    DOCX(JRDocxSaveContributor.class),
    ODT(JROdtSaveContributor.class),
    HTML(JRHtmlSaveContributor.class),
    XML(JRXmlSaveContributor.class),
    CSV(JRCsvSaveContributor.class),
    PRINT(JRPrintSaveContributor.class),
    EMBEDDED_IMAGES_XML(JREmbeddedImagesXmlSaveContributor.class);

    private Class<? extends JRSaveContributor> clazz;

    Extension(Class<? extends JRSaveContributor> clazz) {
        this.clazz = clazz;
    }

    public Class<? extends JRSaveContributor> getClazz() {
        return clazz;
    }
}

Next we need to write a class that override the createToolbar method and apply the config:

import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;
import net.sf.jasperreports.swing.JRViewerToolbar;
import net.sf.jasperreports.view.JRSaveContributor;

import static x.y.z.Extension.*;

import java.lang.reflect.Constructor;
import java.util.Locale;
import java.util.ResourceBundle;

public class MyJRViewer extends JRViewer {

    private static final Extension[] extensions;

    static {
        //HERE YOU CAN ADD WHATEVER EXTENSION YOU WANT
        extensions = new Extension[]{PDF, RTF, DOCX, ODT, HTML};
        //ADD THIS IF YOU WANT ALL
        //extensions = Extension.values();
    }


    public MyJRViewer(JasperPrint jasperPrint) {
        super(jasperPrint);

    }

    @Override
    protected JRViewerToolbar createToolbar() {
        JRViewerToolbar toolbar = super.createToolbar();
        Locale locale = viewerContext.getLocale();
        ResourceBundle resBundle = viewerContext.getResourceBundle();

        JRSaveContributor[] jrsc = new JRSaveContributor[extensions.length];
        Class[] type = {Locale.class, ResourceBundle.class};
        Object[] obj = {locale, resBundle};
        for (int i = 0; i < extensions.length; i++) {
            try {
                Constructor cons = extensions[i].getClazz().getConstructor(type);
                jrsc[i] = (JRSaveContributor) cons.newInstance(obj);
            } catch (Exception x) {
                x.printStackTrace();
            }
        }

        toolbar.setSaveContributors(jrsc);
        return toolbar;
    }
}

As you can see you can change the extensions in static block in order to have your desired extension. This class create the JRSaveContributor sub-classes using reflection.

Finally you can easily create a JFrame and add this to your JFrame. here is how we can do this :

public class MyLauncher {
    public static void main(String[] args) {
        try {
            JasperReport compileReport = JasperCompileManager.compileReport(MyLauncher.class.getResourceAsStream("/myReport.jrxml"));
            JasperPrint fillReport = JasperFillManager.fillReport(compileReport, myReportParameters, new JREmptyDataSource());

            final MyJRViewer mrj = new MyJRViewer(fillReport);
            SwingUtilities.invokeLater(new Runnable() {
               @Override
               public void run() {
                   JFrame frame = new JFrame("Overview");
                   frame.add(mrj);
                   frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                   frame.setUndecorated(true);
                   frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                   frame.setVisible(true);

               }
            });
        } catch (JRException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM