彩票走势图

【Stimulsoft Reports Java教程】使用“Export”对话框导出报表

翻译|使用教程|编辑:王香|2018-11-14 10:38:51.000|阅读 560 次

概述:此示例演示如何使用Java导出对话框(Swing)将报表导出为各种格式。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

相关链接:

下载Stimulsoft Reports Java最新版本

此示例演示如何使用Java导出对话框(Swing)将报表导出为各种格式。

首先,创建JFrame并设置其选项。

public static void main(final String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                JFrame frame = new JFrame();
                frame.add(new ExportReportSettings(frame));
                frame.setSize(FRAME_SIZE);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            } catch (Throwable e) {
                StiExceptionProvider.show(e, null);
            }
        }
    });
}

接下来,我们需要加载报表以进行导出。我们使用SimpleList Report - 加载它并将Demo数据库添加到报表对象。在这些操作呈现报表之后。

private StiReport getReport() {
    if (report == null) {
        try {
            String demoDir = "Data/";
            StiXmlDatabase xmlDatabase = new StiXmlDatabase("Demo", demoDir + "Demo.xsd", demoDir + "Demo.xml");
            StiReport renderReport = StiSerializeManager.deserializeReport(new File("Reports/SimpleList.mrt"));
            renderReport.getDictionary().getDatabases().add(xmlDatabase);
            renderReport.setCalculationMode(StiCalculationMode.Interpretation);
            renderReport.Render(false);
            report = renderReport;
        } catch (Exception e) {
            StiExceptionProvider.show(e, null);
        }
    }
    return report;
}

接下来,在主面板上添加导出按钮。通过单击,每个按钮调用定义的导出功能。

JButton exportBtn = new JButton("Export to PDF");
exportBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        export(StiExportFormat.Pdf);
    }
});
add(exportBtn);
 
exportBtn = new JButton("Export to XPS");
exportBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        export(StiExportFormat.Xps);
    }
});
add(exportBtn);
 
exportBtn = new JButton("Export to HTML");
exportBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        export(StiExportFormat.Html);
    }
});
add(exportBtn);
 
exportBtn = new JButton("Export to Text");
exportBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        export(StiExportFormat.Text);
    }
});
add(exportBtn);
 
exportBtn = new JButton("Export to Rich Text");
exportBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        export(StiExportFormat.Rtf);
    }
});
add(exportBtn);
 
...

最后,添加export()方法。此方法显示返回导出设置的导出对话框。使用这些导出设置将报表导出为所选格式。

private void export(StiExportFormat format) {
    final StiReport report = getReport();
 
    StiExportSettings settings = null;
    switch (format) {
        case Html:
            settings = StiHtmlExportDialog.showDialog(parentFrame, false, 1);
            break;
        case ImageBmp:
        case ImageJpeg:
        case ImagePng:
        case ImageSvg:
        case ImageSvgz:
        case ImagePcx:
            settings = StiImageExportDialog.showDialog(parentFrame, false, 1);
            break;
        case Text:
            settings = StiTxtExportDialog.showDialog(parentFrame, false, 1);
            break;
        case Rtf:
            settings = StiRtfExportDialog.showDialog(parentFrame, false, 1);
            break;
        case Xps:
            settings = StiXpsExportDialog.showDialog(parentFrame, false, 1);
            break;
        case Csv:
            settings = StiDataExportDialog.showDialog(parentFrame, false, 1);
            break;
        case Word2007:
            settings = StiWord2007ExportDialog.showDialog(parentFrame, false, 1);
            break;
        case Pdf:
            settings = StiPdfExportDialog.showDialog(parentFrame, false, 1);
            break;
        case Excel:
            settings = StiExcelExportDialog.showDialog(parentFrame, false, 1);
            break;
        default:
            break;
    }
    if (settings != null) {
        if (settings instanceof StiExcel2007ExportSettings) {
            format = StiExportFormat.Excel2007;
        } else if (settings instanceof StiExcelExportSettings) {
            format = StiExportFormat.Excel;
        } else if (settings instanceof StiExcelXmlExportSettings) {
            format = StiExportFormat.ExcelXml;
        } else if (settings instanceof StiSylkExportSettings) {
            format = StiExportFormat.Sylk;
        } else if (settings instanceof StiXmlExportSettings) {
            format = StiExportFormat.Xml;
        }
 
        final StiFileSaveDialog stiFileChooser = new StiFileSaveDialog(format, report, report.getReportAlias());
        int chooserResult = stiFileChooser.showSaveDialog(this);
        if (chooserResult == JFileChooser.APPROVE_OPTION) {
            FileOutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(stiFileChooser.getFile());
                switch (format) {
                    case Pdf:
                        StiExportManager.exportPdf(report, (StiPdfExportSettings) settings, outputStream);
                        break;
                    case Xps:
                        StiExportManager.exportXps(report, (StiXpsExportSettings) settings, outputStream);
                        break;
                    case Html:
                        StiExportManager.exportHtml(report, (StiHtmlExportSettings) settings, outputStream);
                        break;
                    case Text:
                        StiExportManager.exportText(report, (StiTxtExportSettings) settings, outputStream);
                        break;
                    case Rtf:
                        StiExportManager.exportRtf(report, (StiRtfExportSettings) settings, outputStream);
                        break;
                    case Word2007:
                        StiExportManager.exportWord2007(report, (StiWord2007ExportSettings) settings, outputStream);
                        break;
                    case Excel2007:
                        StiExportManager.exportExcel2007(report, (StiExcel2007ExportSettings) settings, outputStream);
                        break;
                    case Excel:
                        StiExportManager.exportExcel(report, (StiExcelExportSettings) settings, outputStream);
                        break;
                    case ExcelXml:
                        StiExportManager.exportExcelXml(report, (StiExcelXmlExportSettings) settings, outputStream);
                        break;
                    case Csv:
                        StiExportManager.exportCsv(report, (StiCsvExportSettings) settings, outputStream);
                        break;
                    case Xml:
                        StiExportManager.exportXml(report, (StiXmlExportSettings) settings, outputStream);
                        break;
                    case Sylk:
                        StiExportManager.exportSylk(report, (StiSylkExportSettings) settings, outputStream);
                        break;
                    case ImageBmp:
                        StiExportManager.exportImageBmp(report, (StiBmpExportSettings) settings, outputStream);
                        break;
                    case ImageJpeg:
                        StiExportManager.exportImageJpeg(report, (StiJpegExportSettings) settings, outputStream);
                        break;
                    case ImagePcx:
                        StiExportManager.exportImagePcx(report, (StiPcxExportSettings) settings, outputStream);
                        break;
                    case ImagePng:
                        StiExportManager.exportImagePng(report, (StiPngExportSettings) settings, outputStream);
                        break;
                    case ImageSvg:
                        StiExportManager.exportImageSvg(report, (StiSvgExportSettings) settings, outputStream);
                        break;
                    case ImageSvgz:
                        StiExportManager.exportImageSvgz(report, (StiSvgzExportSettings) settings, outputStream);
                        break;
                    default:
                        break;
                }
                if (settings.isOpenAfterExport()) {
                    StiFileExecuter.openByExtension(stiFileChooser.getFile().getAbsolutePath());
                } else {
                    JOptionPane.showMessageDialog(null, "Export finished");
                }
 
            } catch (FileNotFoundException e) {
                StiExceptionProvider.show(e, null);
            } catch (StiException e) {
                StiExceptionProvider.show(e, null);
            } finally {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        StiExceptionProvider.show(e, null);
                    }
                }
            }
        }
    }
}

示例代码的结果如下图所示:

Stimulsoft

购买Stimulsoft正版授权,请点击“”哟!


标签:报表Java报表控件Stimulsoft

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP