Search in sources :

Example 1 with SimpleXlsReportConfiguration

use of net.sf.jasperreports.export.SimpleXlsReportConfiguration in project jgnash by ccavanaugh.

the class JasperViewerDialogController method handleSaveAction.

@FXML
private void handleSaveAction() {
    Preferences preferences = Preferences.userNodeForPackage(JasperViewerDialogController.class);
    final String lastDir = preferences.get(LAST_DIR, null);
    final FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle(ResourceUtils.getString("Title.SaveFile"));
    if (lastDir != null) {
        fileChooser.setInitialDirectory(new File(lastDir));
    }
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("PDF Document", "*.pdf", "*.PDF"));
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("ODT Document", "*.odt", "*.ODT"));
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("DOCX Document", "*.docx", "*.DOCX"));
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("XLSX Document", "*.xlsx", "*.XLSX"));
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("CSV Document", "*.csv", "*.CSV"));
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("RTF Document", "*.rtf", "*.RTF"));
    final File file = fileChooser.showSaveDialog(MainView.getPrimaryStage());
    if (file != null) {
        preferences.put(LAST_DIR, file.getParent());
        switch(FileUtils.getFileExtension(file.getAbsolutePath()).toLowerCase(Locale.ROOT)) {
            case "pdf":
                try {
                    JasperExportManager.exportReportToPdfFile(jasperPrint.get(), file.getAbsolutePath());
                } catch (final JRException e) {
                    StaticUIMethods.displayException(e);
                }
                break;
            case "odt":
                try {
                    final JROdtExporter exporter = new JROdtExporter();
                    exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
                    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(file));
                    exporter.exportReport();
                } catch (final JRException e) {
                    StaticUIMethods.displayException(e);
                }
                break;
            case "docx":
                try {
                    final JRDocxExporter exporter = new JRDocxExporter();
                    exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
                    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(file));
                    exporter.exportReport();
                } catch (final JRException e) {
                    StaticUIMethods.displayException(e);
                }
                break;
            case "xlsx":
                try {
                    final JRXlsExporter exporter = new JRXlsExporter();
                    exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
                    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(file));
                    final SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
                    configuration.setOnePagePerSheet(false);
                    exporter.setConfiguration(configuration);
                    exporter.exportReport();
                } catch (final JRException e) {
                    StaticUIMethods.displayException(e);
                }
                break;
            case "csv":
                try {
                    final JRCsvExporter exporter = new JRCsvExporter();
                    exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
                    exporter.setExporterOutput(new SimpleWriterExporterOutput(file));
                    exporter.exportReport();
                } catch (final JRException e) {
                    StaticUIMethods.displayException(e);
                }
                break;
            case "rtf":
                try {
                    final JRRtfExporter exporter = new JRRtfExporter();
                    exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
                    exporter.setExporterOutput(new SimpleWriterExporterOutput(file));
                    exporter.exportReport();
                } catch (final JRException e) {
                    StaticUIMethods.displayException(e);
                }
                break;
            default:
        }
    }
}
Also used : SimpleXlsReportConfiguration(net.sf.jasperreports.export.SimpleXlsReportConfiguration) JROdtExporter(net.sf.jasperreports.engine.export.oasis.JROdtExporter) JRException(net.sf.jasperreports.engine.JRException) SimpleOutputStreamExporterOutput(net.sf.jasperreports.export.SimpleOutputStreamExporterOutput) JRCsvExporter(net.sf.jasperreports.engine.export.JRCsvExporter) SimpleExporterInput(net.sf.jasperreports.export.SimpleExporterInput) JRXlsExporter(net.sf.jasperreports.engine.export.JRXlsExporter) SimpleWriterExporterOutput(net.sf.jasperreports.export.SimpleWriterExporterOutput) FileChooser(javafx.stage.FileChooser) Preferences(java.util.prefs.Preferences) JRDocxExporter(net.sf.jasperreports.engine.export.ooxml.JRDocxExporter) File(java.io.File) JRRtfExporter(net.sf.jasperreports.engine.export.JRRtfExporter) FXML(javafx.fxml.FXML) InjectFXML(jgnash.uifx.util.InjectFXML)

Example 2 with SimpleXlsReportConfiguration

use of net.sf.jasperreports.export.SimpleXlsReportConfiguration in project java-examples by urvanov-ru.

the class App method main.

public static void main(String[] args) {
    try {
        JasperReport jasperReport = JasperCompileManager.compileReport(App.class.getResourceAsStream("chart1.jrxml"));
        Map<String, Object> parameters = new HashMap<>();
        List<Map<String, ?>> data = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("x", Integer.valueOf(1));
        map.put("v", Integer.valueOf(2));
        map.put("name", "name");
        data.add(map);
        map = new HashMap<>();
        map.put("x", Integer.valueOf(2));
        map.put("v", Integer.valueOf(1));
        map.put("name", "name");
        data.add(map);
        map = new HashMap<>();
        map.put("x", Integer.valueOf(3));
        map.put("v", Integer.valueOf(3));
        map.put("name", "name");
        data.add(map);
        JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(data);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
        try (FileOutputStream baos = new FileOutputStream("chart1.xls")) {
            JRXlsExporter xlsExporter = new JRXlsExporter();
            xlsExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
            xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos));
            SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
            configuration.setOnePagePerSheet(false);
            xlsExporter.setConfiguration(configuration);
            xlsExporter.exportReport();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SimpleXlsReportConfiguration(net.sf.jasperreports.export.SimpleXlsReportConfiguration) HashMap(java.util.HashMap) SimpleOutputStreamExporterOutput(net.sf.jasperreports.export.SimpleOutputStreamExporterOutput) JasperPrint(net.sf.jasperreports.engine.JasperPrint) ArrayList(java.util.ArrayList) SimpleExporterInput(net.sf.jasperreports.export.SimpleExporterInput) JasperReport(net.sf.jasperreports.engine.JasperReport) JRXlsExporter(net.sf.jasperreports.engine.export.JRXlsExporter) JRMapCollectionDataSource(net.sf.jasperreports.engine.data.JRMapCollectionDataSource) FileOutputStream(java.io.FileOutputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with SimpleXlsReportConfiguration

use of net.sf.jasperreports.export.SimpleXlsReportConfiguration in project java-examples by urvanov-ru.

the class App method main.

public static void main(String[] args) {
    try {
        JasperReport jasperReport = JasperCompileManager.compileReport(App.class.getResourceAsStream("chart1.jrxml"));
        Map<String, Object> parameters = new HashMap<>();
        List<Map<String, ?>> data = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("x", Integer.valueOf(10));
        map.put("v", Integer.valueOf(20));
        map.put("name", "name");
        data.add(map);
        map = new HashMap<>();
        map.put("x", Integer.valueOf(20));
        map.put("v", Integer.valueOf(10));
        map.put("name", "name");
        data.add(map);
        map = new HashMap<>();
        map.put("x", Integer.valueOf(30));
        map.put("v", Integer.valueOf(30));
        map.put("name", "name");
        data.add(map);
        JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(data);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
        try (FileOutputStream baos = new FileOutputStream("chart1.xls")) {
            JRXlsExporter xlsExporter = new JRXlsExporter();
            xlsExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
            xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos));
            SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
            configuration.setOnePagePerSheet(false);
            xlsExporter.setConfiguration(configuration);
            xlsExporter.exportReport();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SimpleXlsReportConfiguration(net.sf.jasperreports.export.SimpleXlsReportConfiguration) HashMap(java.util.HashMap) SimpleOutputStreamExporterOutput(net.sf.jasperreports.export.SimpleOutputStreamExporterOutput) JasperPrint(net.sf.jasperreports.engine.JasperPrint) ArrayList(java.util.ArrayList) SimpleExporterInput(net.sf.jasperreports.export.SimpleExporterInput) JasperReport(net.sf.jasperreports.engine.JasperReport) JRXlsExporter(net.sf.jasperreports.engine.export.JRXlsExporter) JRMapCollectionDataSource(net.sf.jasperreports.engine.data.JRMapCollectionDataSource) FileOutputStream(java.io.FileOutputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with SimpleXlsReportConfiguration

use of net.sf.jasperreports.export.SimpleXlsReportConfiguration in project java-examples by urvanov-ru.

the class App method generateReport.

public static void generateReport() throws JRException, IOException {
    JasperDesign jasperDesign = createDesign();
    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
    JRDataSource jrDataSource = prepareDataSource();
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("startDate", new Date());
    params.put("endDate", new Date());
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, jrDataSource);
    try (FileOutputStream baos = new FileOutputStream("dynamicReport.xls")) {
        JRXlsExporter xlsExporter = new JRXlsExporter();
        xlsExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos));
        SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
        configuration.setOnePagePerSheet(false);
        xlsExporter.setConfiguration(configuration);
        xlsExporter.exportReport();
    }
}
Also used : SimpleXlsReportConfiguration(net.sf.jasperreports.export.SimpleXlsReportConfiguration) JasperDesign(net.sf.jasperreports.engine.design.JasperDesign) HashMap(java.util.HashMap) SimpleOutputStreamExporterOutput(net.sf.jasperreports.export.SimpleOutputStreamExporterOutput) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JRDataSource(net.sf.jasperreports.engine.JRDataSource) SimpleExporterInput(net.sf.jasperreports.export.SimpleExporterInput) JasperReport(net.sf.jasperreports.engine.JasperReport) Date(java.util.Date) JRXlsExporter(net.sf.jasperreports.engine.export.JRXlsExporter) FileOutputStream(java.io.FileOutputStream)

Example 5 with SimpleXlsReportConfiguration

use of net.sf.jasperreports.export.SimpleXlsReportConfiguration in project dhis2-core by dhis2.

the class JRExportUtils method export.

/**
 * Export the provided JasperPrint the format given by type.
 *
 * @param type the type to export to. XLS, PDF and HTML are supported.
 * @param out the OutputStream to export to.
 * @param jasperPrint the JasperPrint to export.
 * @throws JRException on export failure.
 */
public static void export(String type, OutputStream out, JasperPrint jasperPrint) throws JRException {
    if (TYPE_XLS.equals(type)) {
        SimpleXlsReportConfiguration config = new SimpleXlsReportConfiguration();
        config.setDetectCellType(true);
        config.setRemoveEmptySpaceBetweenRows(true);
        config.setRemoveEmptySpaceBetweenRows(true);
        config.setCollapseRowSpan(true);
        config.setWhitePageBackground(false);
        JRXlsExporter exporter = new JRXlsExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
        exporter.setConfiguration(config);
        exporter.exportReport();
    } else if (TYPE_PDF.equals(type)) {
        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
        exporter.exportReport();
    }
}
Also used : SimpleXlsReportConfiguration(net.sf.jasperreports.export.SimpleXlsReportConfiguration) JRXlsExporter(net.sf.jasperreports.engine.export.JRXlsExporter) SimpleOutputStreamExporterOutput(net.sf.jasperreports.export.SimpleOutputStreamExporterOutput) SimpleExporterInput(net.sf.jasperreports.export.SimpleExporterInput) JRPdfExporter(net.sf.jasperreports.engine.export.JRPdfExporter)

Aggregations

JRXlsExporter (net.sf.jasperreports.engine.export.JRXlsExporter)5 SimpleExporterInput (net.sf.jasperreports.export.SimpleExporterInput)5 SimpleOutputStreamExporterOutput (net.sf.jasperreports.export.SimpleOutputStreamExporterOutput)5 SimpleXlsReportConfiguration (net.sf.jasperreports.export.SimpleXlsReportConfiguration)5 FileOutputStream (java.io.FileOutputStream)3 HashMap (java.util.HashMap)3 JasperPrint (net.sf.jasperreports.engine.JasperPrint)3 JasperReport (net.sf.jasperreports.engine.JasperReport)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 JRMapCollectionDataSource (net.sf.jasperreports.engine.data.JRMapCollectionDataSource)2 File (java.io.File)1 Date (java.util.Date)1 Preferences (java.util.prefs.Preferences)1 FXML (javafx.fxml.FXML)1 FileChooser (javafx.stage.FileChooser)1 InjectFXML (jgnash.uifx.util.InjectFXML)1 JRDataSource (net.sf.jasperreports.engine.JRDataSource)1 JRException (net.sf.jasperreports.engine.JRException)1 JasperDesign (net.sf.jasperreports.engine.design.JasperDesign)1