最近我用最新的jasper-reports lib更新了我的项目。 有一个新的JRViewer类(net.sf.jasperreports.swing)取代了旧的(net.sf.jasperreports.view)。现在我无法弄清楚如何设置导出选项。旧代码就像:
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 });
答案 0 :(得分:4)
您必须扩展net.sf.jasperreports.swing.JRViewer并将导出贡献者设置为JRViewerToolbar。像这样:
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;
}
}
答案 1 :(得分:0)
您可以使用此方法以模块化方式选择扩展名。
首先,我们定义一个包含所有扩展类的枚举,所有这些类都是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;
}
}
接下来,我们需要编写一个覆盖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;
}
}
如您所见,您可以在静态块中更改扩展名以获得所需的扩展名。此类使用反射创建JRSaveContributor子类。
最后,您可以轻松创建一个JFrame并将其添加到您的JFrame中。这是我们可以做到的:
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();
}
}
}