Beispiel #1
0
  public static void createCSVReport(
      InputStream reportFile, GridReportVO gridReportVO, String path, String fileName)
      throws JRException, IOException {
    gridReportVO.setPagination(true);
    JasperPrint jasperPrint = createJasperPrint(reportFile, gridReportVO);
    JRCsvExporter exporter = new JRCsvExporter();
    Map<JRExporterParameter, Object> p = new HashMap<JRExporterParameter, Object>();

    p.put(JRExporterParameter.JASPER_PRINT, jasperPrint);
    p.put(JRExporterParameter.OUTPUT_FILE_NAME, path + "/" + fileName);

    exporter.setParameters(p);
    exporter.exportReport();
  }
  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));
  }
  public void csv() throws JRException {
    long start = System.currentTimeMillis();
    File sourceFile = new File("build/reports/JRMDbReport.jrprint");

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

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

    JRCsvExporter exporter = new JRCsvExporter();

    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.setExporterOutput(new SimpleWriterExporterOutput(destFile));

    exporter.exportReport();

    System.err.println("CSV creation time : " + (System.currentTimeMillis() - start));
  }
  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;
  }