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

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

下载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

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2018年10月11日
下一篇 2018年10月11日

相关推荐

发表回复

登录后才能评论