This sample shows how to export a report to various formats with the Java export dialog (Swing).
First, create the
JFrame
and set its options:
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);
}
}
});
}
Next, we need to load the report for exporting. We use the 'SimpleList' report - load it and add a 'Demo' database to report object. After these actions render the report:
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;
}
Next, add the export buttons on the main panel. By clicking, each button calls the defined export function:
...
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);
...
Finally, add the
export()
method. This method shows the export dialog that returns export settings. Use these export settings to export report to the selected format:
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);
}
}
}
}
}
}
In the screenshot below you can see the result of the sample code: