Search in sources :

Example 1 with JRMapCollectionDataSource

use of net.sf.jasperreports.engine.data.JRMapCollectionDataSource 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 2 with JRMapCollectionDataSource

use of net.sf.jasperreports.engine.data.JRMapCollectionDataSource 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)

Aggregations

FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JasperPrint (net.sf.jasperreports.engine.JasperPrint)2 JasperReport (net.sf.jasperreports.engine.JasperReport)2 JRMapCollectionDataSource (net.sf.jasperreports.engine.data.JRMapCollectionDataSource)2 JRXlsExporter (net.sf.jasperreports.engine.export.JRXlsExporter)2 SimpleExporterInput (net.sf.jasperreports.export.SimpleExporterInput)2 SimpleOutputStreamExporterOutput (net.sf.jasperreports.export.SimpleOutputStreamExporterOutput)2 SimpleXlsReportConfiguration (net.sf.jasperreports.export.SimpleXlsReportConfiguration)2