how to export to excel and csv format in jasper report

后端 未结 3 590
情话喂你
情话喂你 2021-01-06 19:03

i need to export to excel and csv format in jasper reports. for excel i tried by creating custom class (using api)but it is not exporting, the thing is save and cancel p

3条回答
  •  被撕碎了的回忆
    2021-01-06 19:16

    For those who are using JRCsvExporter, the following code may be useful. Giving some sample structure.

    Spring Framework Service class (CSVExportService.java):

    public JasperPrint getRawData(String empIds) {
    
    JasperPrint jp = null;
    String reportName = "Employee Report";
    
    // use your own method to get empList
    // eg: List empList = empServiceClass.findByEmpIds(empIds);
    JRDataSource jrDataSource = new JRBeanCollectionDataSource(empList);
    
    // build your report 
    DynamicReportBuilder dynamicReportBuilder = new DynamicReportBuilder();
    dynamicReportBuilder.setAllowDetailSplit(false);
    // configure your report with few more options here
    
    // create columns
    ColumnBuilder columnBuilderName = ColumnBuilder.getNew();
    columnBuilderName.setTitle("Emp Name");
    columnBuilderName.setWidth(300);
    columnBuilderName.setFixedWidth(true);
    columnBuilderName.setColumnProperty("name", String.class.getName());
    dynamicReportBuilder.addColumn(columnBuilderName.build());
    
    DynamicReport dynamicReport = dynamicReportBuilder.build();
    
    jp = DynamicJasperHelper.generateJasperPrint(dynamicReport, new ClassicLayoutManager(), jrDataSource, new HashMap());
    return jp;
    }
    

    Spring Framework Controller Class:

    public void exportToCSV(@PathVariable String empIds){
      JasperPrint jp = null;
      jp = csvExportService.getRawData(empIds);
      response.setContentType("text/csv");
      response.setHeader("Content-Disposition", "attachment; filename="EMPRawData.csv");
      OutputStream out = response.getOutputStream();
      JRCsvExporter exporterCSV = new JRCsvExporter();
      exporterCSV.setParameter(JRExporterParameter.JASPER_PRINT, jp);
      exporterCSV.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
      exporterCSV.exportReport();
    
      out.flush();
    }
    

提交回复
热议问题