JasperReports generating an Excel file
This is an extension to the JasperReport step by step tutorial to demonstrate how to generate a Excel file from the Main.java. To generate an Excel file, you need Apache POI as a dependency. Here are the Maven dependency files required.
<!-- Jasper reports --> <dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>5.0.1</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.7</version> <optional>true</optional> </dependency>
Now, the Main.java file to generate the Excel file.
package com.mycompany.app.jasper; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Collection; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.export.JRXlsExporter; import net.sf.jasperreports.engine.export.JRXlsExporterParameter; import net.sf.jasperreports.engine.xml.JRXmlLoader; public class Main { public static JasperDesign jasperDesign; public static JasperPrint jasperPrint; public static JasperReport jasperReport; public static String reportTemplateUrl = "person-template.jrxml"; public static void main(String[] args) throws IOException { try { InputStream resourceAsStream = Thread.currentThread().getContextClassLoader() .getResourceAsStream(reportTemplateUrl); //get report file and then load into jasperDesign jasperDesign = JRXmlLoader.load(resourceAsStream); //compile the jasperDesign jasperReport = JasperCompileManager.compileReport(jasperDesign); ByteArrayOutputStream output = new ByteArrayOutputStream(); OutputStream outputfile = new FileOutputStream(new File("c:/temp/person.xls")); //fill the ready report with data and parameter jasperPrint = JasperFillManager.fillReport(jasperReport, getParameters(), new JRBeanCollectionDataSource( findReportData())); //coding for Excel JRXlsExporter exporterXls = new JRXlsExporter(); exporterXls.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint); exporterXls.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, output); exporterXls.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); exporterXls.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); exporterXls.exportReport(); outputfile.write(output.toByteArray()); } catch (JRException e) { e.printStackTrace(); } } private static Collection<Person> findReportData() { //declare a list of object List<Person> data = new LinkedList<Person>(); Person p1 = new Person(); p1.setFirstName("John"); p1.setSurname("Smith"); p1.setAge(Integer.valueOf(5)); data.add(p1); return data; } private static Map<String, Object> getParameters() { Map<String, Object> params = new HashMap<String, Object>(); params.put("footerText", "Just to demonstrate how to pass parameters to report"); return params; } }
If you execute the above class, person.xls will be generated under the c:\temp folder.
Labels: JasperReports
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home