简体   繁体   English

如何设置JRViewer导出选项?

[英]How do you set the JRViewer export options?

Recently I updated my project with the newest jasper-reports lib. 最近,我使用最新的jasper-reports lib更新了我的项目。 There is a new JRViewer class (net.sf.jasperreports.swing) which replaced the old one (net.sf.jasperreports.view). 有一个新的JRViewer类(net.sf.jasperreports.swing)代替了旧的类(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. 您必须扩展net.sf.jasperreports.swing.JRViewer并将导出贡献者设置为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 . 首先,我们定义一个枚举,其中包含所有扩展类,所有这些类都是JRSaveContributor的子类, 用于保存扩展

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: 接下来,我们需要编写一个覆盖createToolbar方法的类并应用配置:

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. 此类使用反射创建JRSaveContributor子类。

Finally you can easily create a JFrame and add this to your JFrame. 最后,您可以轻松创建一个JFrame并将其添加到您的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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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