jasper-reports With Java


Example

Export to xlsx format

try (InputStream inputStream = JRLoader.getResourceInputStream(path)) {  // read report as input stream 
    JasperReport  jasperReport = JasperCompileManager.compileReport(JRXmlLoader.load(inputStream)); // compile report

    Map<String, Object> params = new HashMap<>(); // init map with report's parameters
    params.put(JRParameter.REPORT_LOCALE, Locale.US);
    params.put(JRParameter.IS_IGNORE_PAGINATION, true);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, connection);  // prepare report - passs parameters and jdbc connection

    JRXlsxExporter exporter = new JRXlsxExporter(); // initialize exporter 
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); // set compiled report as input
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));  // set output file via path with filename
    SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
    configuration.setOnePagePerSheet(true); // setup configuration
    configuration.setDetectCellType(true);
    exporter.setConfiguration(configuration); // set configuration
    exporter.exportReport();
}