Skip to content

Instantly share code, notes, and snippets.

@danilobatistaqueiroz
Last active February 6, 2018 01:16
Show Gist options
  • Save danilobatistaqueiroz/75466c3978762ed22c7e7add2079527c to your computer and use it in GitHub Desktop.
Save danilobatistaqueiroz/75466c3978762ed22c7e7add2079527c to your computer and use it in GitHub Desktop.
creating a jasper report

Downloading

https://sourceforge.net/projects/ireport/

and the new version:

https://community.jaspersoft.com/download

How the application was structured

project_structure

I used the following packages:

jasper – JasperReports Files

model – Beans for our data

relatorio – Classes designated to generate reports

testes – Classes for testing

Maven Dependencies

	<dependencies>
		<!-- jasperreports -->
		<dependency>
			<groupId>net.sf.jasperreports</groupId>
			<artifactId>jasperreports</artifactId>
			<version>5.6.0</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.groovy</groupId>
			<artifactId>groovy-all</artifactId>
			<version>2.3.0</version>
		</dependency>
		<!-- junit4 -->
		<dependency>
		    <groupId>junit</groupId>
		    <artifactId>junit</artifactId>
		    <version>4.12</version>
		    <scope>test</scope>
		</dependency>
	</dependencies>

Designing a new report

Open Jaspersoft.
Go to New -> Report -> Blank A4

1º Right click on “fields” and “Add Field”
2º Click in the field

newfield

3º Go to Properties
4º Configure the name (the same of properties in the model)

newfield2

Create the class to generate the reports

package com.labs.report;

import java.io.File;
import java.util.List;

import com.labs.model.Client;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
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;

public class ClientReport 
{
	private String path;
	
	private String pathToReportPackage;
	
	public ClientReport() {
		this.path = this.getClass().getClassLoader().getResource("").getPath();
		this.pathToReportPackage = this.path + "com/labs/jasper/";
		System.out.println(path);
	}
	
	
	public void generate(List<Client> clients) throws Exception	
	{
		JasperReport report = JasperCompileManager.compileReport(this.getPathToReportPackage() + "Clients.jrxml");
		
		JasperPrint print = JasperFillManager.fillReport(report, null, new JRBeanCollectionDataSource(clients));
		
		File filePath = new File("f:/java/jasperreports/clients.pdf");

		JasperExportManager.exportReportToPdfFile(print, filePath.getAbsolutePath());		
	}

	public String getPathToReportPackage() {
		return this.pathToReportPackage;
	}
	
	public String getPath() {
		return this.path;
	}
}

Create the class for tests

package com.labs.tests;


import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.labs.model.Client;
import com.labs.report.ClientReport;

public class ReportsTest {

	@Test
	public void generateClientsTest()
	{
		try{
			List<Client> clients = new ArrayList<Client>();
			
			Client Client1 = new Client();
			Client1.setName("TI Labs");
			Client1.setAddress("132, My Street");
			Client1.setPostalCode("BT274DP");
			Client1.setPhone("8888-5566");
			Client1.setZipCode("BT");
			
			Client Client2 = new Client();
			Client2.setName("Pepper Jonh");
			Client2.setAddress("9214 Lake St.");
			Client2.setPostalCode("G15HF");
			Client2.setZipCode("GA");
			
			Client Client3 = new Client();
			Client3.setName("FLC Technology");
			Client3.setAddress("44 Shirley Ave.");
			Client3.setPostalCode("WC2R");
			Client3.setPhone("98523-1234");
			Client3.setZipCode("WC");
			
			
			clients.add(Client1);
			clients.add(Client2);
			clients.add(Client3);
			
			
			ClientReport creport = new ClientReport();
			creport.generate(clients);
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
	}
}

Links:

http://respostas.guj.com.br/28139-tutorial-jasperreports-em-projeto-java-web-utilizando-o-maven

https://www.codigofonte.com.br/artigos/tutorial-criando-relatorios-com-jasperreports-primeiros-passos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment