public void csv() throws JRException {
    long start = System.currentTimeMillis();
    File sourceFile = new File("build/reports/DataSourceReport.jrprint");

    JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);

    File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".csv");

    JRCsvExporter exporter = new JRCsvExporter();

    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());

    exporter.exportReport();

    System.err.println("CSV creation time : " + (System.currentTimeMillis() - start));
  }
Esempio n. 2
0
  private byte[] generaCsv(List lista) throws JRException {
    Map<String, Object> params = new HashMap<>();
    JRCsvExporter exporter = new JRCsvExporter();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    JasperDesign jd =
        JRXmlLoader.load(
            this.getClass()
                .getResourceAsStream("/mx/edu/um/mateo/inventario/reportes/almacenes.jrxml"));
    JasperReport jasperReport = JasperCompileManager.compileReport(jd);
    JasperPrint jasperPrint =
        JasperFillManager.fillReport(jasperReport, params, new JRBeanCollectionDataSource(lista));
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, byteArrayOutputStream);
    exporter.exportReport();
    byte[] archivo = byteArrayOutputStream.toByteArray();

    return archivo;
  }
  private String saveReport(JasperPrint jasperPrint, String format, String destFileName)
      throws JRException, Exception {
    String reportName = null;
    switch (Format.valueOf(format)) {
      case pdf:
        JasperExportManager.exportReportToPdfFile(jasperPrint, destFileName);
        reportName = destFileName;
        break;
      case html:
        JasperExportManager.exportReportToHtmlFile(jasperPrint, destFileName);
        reportName = createZip(destFileName);
        break;
      case xml:
        JasperExportManager.exportReportToXmlFile(jasperPrint, destFileName, true);
        reportName = createZip(destFileName);
        break;
      case csv:
        JRCsvExporter exporter = new JRCsvExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFileName);
        exporter.exportReport();
        reportName = destFileName;
        break;
      default:
        LogUtils.errorf(this, "Error Running Report: Unknown Format: %s", format);
    }

    return reportName;
  }