【Stimulsoft Reports Java教程】使用“Export”對話方塊匯出報表
阿新 • • 發佈:2018-11-20
此示例演示如何使用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); } } } } } }
示例程式碼的結果如下圖所示: