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
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<Employee> 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<String, Object>());
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();
}
Use as a JRXlsExporter to export to XSL and a JRCsvExporter for CSV.
There should be no need to create a custom class in most cases.
Edit
The class is stored inside the jar poi-3.5-FINAL-20090928.jar, which should be locate in your "iReportInstallationFolder"\modules\ext\
For me it's C:\Program Files\Jaspersoft\iReport-3.7.4\ireport\modules\ext\
The name could be different but should be poi-3.5-FINAL-*.jar.
Make it is included in your classpath and you should be fine.
You can download the jar from Apache Poi home page.
Here is a link to the jar I have from their site http://archive.apache.org/dist/poi/release/bin/poi-bin-3.5-FINAL-20090928.tar.gz
JRCsvExporter exporter = new JRCsvExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleWriterExporterOutput(new FileOutputStream(new File(output1))));
SimpleCsvExporterConfiguration configuration = new SimpleCsvExporterConfiguration();
//configuration.setWriteBOM(Boolean.TRUE);
exporter.setConfiguration(configuration);
exporter.exportReport();
Kindly use this code .its working fine