Skip to content

Commit 11f954b

Browse files
authored
#code
1 parent 303d779 commit 11f954b

8 files changed

Lines changed: 375 additions & 0 deletions

File tree

pom.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.0.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.javatechie</groupId>
12+
<artifactId>spring-boot-jasper-report</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>spring-boot-jasper-report</name>
15+
<description>Demo project for Spring Boot report generation using JasperReport</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-data-jpa</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>net.sf.jasperreports</groupId>
32+
<artifactId>jasperreports</artifactId>
33+
<version>6.9.0</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>mysql</groupId>
37+
<artifactId>mysql-connector-java</artifactId>
38+
<scope>runtime</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.projectlombok</groupId>
42+
<artifactId>lombok</artifactId>
43+
<optional>true</optional>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-test</artifactId>
48+
<scope>test</scope>
49+
<exclusions>
50+
<exclusion>
51+
<groupId>org.junit.vintage</groupId>
52+
<artifactId>junit-vintage-engine</artifactId>
53+
</exclusion>
54+
</exclusions>
55+
</dependency>
56+
</dependencies>
57+
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-maven-plugin</artifactId>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
67+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.javatechie.report;
2+
3+
import com.javatechie.report.entity.Employee;
4+
import com.javatechie.report.repository.EmployeeRepository;
5+
import com.javatechie.report.service.ReportService;
6+
import net.sf.jasperreports.engine.JRException;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
import java.io.FileNotFoundException;
15+
import java.util.List;
16+
17+
@SpringBootApplication
18+
@RestController
19+
public class SpringBootJasperReportApplication {
20+
21+
@Autowired
22+
private EmployeeRepository repository;
23+
@Autowired
24+
private ReportService service;
25+
26+
@GetMapping("/getEmployees")
27+
public List<Employee> getEmployees() {
28+
29+
return repository.findAll();
30+
}
31+
32+
@GetMapping("/report/{format}")
33+
public String generateReport(@PathVariable String format) throws FileNotFoundException, JRException {
34+
return service.exportReport(format);
35+
}
36+
37+
public static void main(String[] args) {
38+
SpringApplication.run(SpringBootJasperReportApplication.class, args);
39+
}
40+
41+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.javatechie.report.entity;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import lombok.ToString;
7+
8+
import javax.persistence.Entity;
9+
import javax.persistence.Id;
10+
import javax.persistence.Table;
11+
12+
@AllArgsConstructor
13+
@NoArgsConstructor
14+
@ToString
15+
@Data
16+
@Entity
17+
@Table(name = "EMPLOYEE_TBL")
18+
public class Employee {
19+
@Id
20+
private int id;
21+
private String name;
22+
private String designation;
23+
private double salary;
24+
private String doj;
25+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.javatechie.report.repository;
2+
3+
import com.javatechie.report.entity.Employee;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface EmployeeRepository extends JpaRepository<Employee,Integer> {
7+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.javatechie.report.service;
2+
3+
import com.javatechie.report.entity.Employee;
4+
import com.javatechie.report.repository.EmployeeRepository;
5+
import net.sf.jasperreports.engine.*;
6+
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
7+
import net.sf.jasperreports.engine.design.JasperDesign;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.stereotype.Service;
10+
import org.springframework.util.ResourceUtils;
11+
12+
import java.io.File;
13+
import java.io.FileNotFoundException;
14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
@Service
19+
public class ReportService {
20+
21+
@Autowired
22+
private EmployeeRepository repository;
23+
24+
25+
public String exportReport(String reportFormat) throws FileNotFoundException, JRException {
26+
String path = "C:\\Users\\basan\\Desktop\\Report";
27+
List<Employee> employees = repository.findAll();
28+
//load file and compile it
29+
File file = ResourceUtils.getFile("classpath:employees.jrxml");
30+
JasperReport jasperReport = JasperCompileManager.compileReport(file.getAbsolutePath());
31+
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(employees);
32+
Map<String, Object> parameters = new HashMap<>();
33+
parameters.put("createdBy", "Java Techie");
34+
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
35+
if (reportFormat.equalsIgnoreCase("html")) {
36+
JasperExportManager.exportReportToHtmlFile(jasperPrint, path + "\\employees.html");
37+
}
38+
if (reportFormat.equalsIgnoreCase("pdf")) {
39+
JasperExportManager.exportReportToPdfFile(jasperPrint, path + "\\employees.pdf");
40+
}
41+
42+
return "report generated in path : " + path;
43+
}
44+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
2+
spring.datasource.url = jdbc:mysql://localhost:3306/javatechie
3+
spring.datasource.username = root
4+
spring.datasource.password = Password
5+
spring.jpa.show-sql = true
6+
spring.jpa.hibernate.ddl-auto = update
7+
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
8+
server.port=9090
9+

src/main/resources/employees.jrxml

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Created with Jaspersoft Studio version 6.10.0.final using JasperReports Library version 6.10.0-unknown -->
3+
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="employees" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ea9d9eab-78ea-4cb9-adce-7588ae6a483e">
4+
5+
<field name="id" class="java.lang.Integer">
6+
</field>
7+
<field name="name" class="java.lang.String">
8+
</field>
9+
<field name="designation" class="java.lang.String">
10+
</field>
11+
<field name="salary" class="java.lang.Double">
12+
</field>
13+
<field name="doj" class="java.lang.String">
14+
</field>
15+
<background>
16+
<band/>
17+
</background>
18+
<title>
19+
<band height="72">
20+
<frame>
21+
<reportElement mode="Opaque" x="-20" y="-20" width="595" height="92" backcolor="#006699" uuid="067e5760-2a3c-4197-92e5-afbec0f9ce47"/>
22+
<staticText>
23+
<reportElement x="20" y="20" width="234" height="43" forecolor="#FFFFFF" uuid="7fd19f78-c0bc-46e9-a879-cead959aa829"/>
24+
<textElement>
25+
<font size="34" isBold="true"/>
26+
</textElement>
27+
<text><![CDATA[Java Techie]]></text>
28+
</staticText>
29+
<staticText>
30+
<reportElement x="395" y="43" width="180" height="20" forecolor="#FFFFFF" uuid="d542e825-fb57-41bd-b967-ba4a22515efe"/>
31+
<textElement textAlignment="Right">
32+
<font size="14" isBold="false"/>
33+
</textElement>
34+
<text><![CDATA[Employee Reports]]></text>
35+
</staticText>
36+
</frame>
37+
</band>
38+
</title>
39+
<pageHeader>
40+
<band height="13"/>
41+
</pageHeader>
42+
<columnHeader>
43+
<band height="21">
44+
<line>
45+
<reportElement x="-20" y="20" width="595" height="1" forecolor="#666666" uuid="ca533965-9162-4dc2-8a9e-2c99b0db8e3e"/>
46+
</line>
47+
<staticText>
48+
<reportElement mode="Opaque" x="0" y="0" width="111" height="20" forecolor="#006699" backcolor="#E6E6E6" uuid="55735968-342c-4d29-8dc1-db2263a44d11">
49+
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="40a9814d-ecb4-424f-aea8-cad246f112b5"/>
50+
</reportElement>
51+
<textElement textAlignment="Center">
52+
<font size="14" isBold="true"/>
53+
</textElement>
54+
<text><![CDATA[Id]]></text>
55+
</staticText>
56+
<staticText>
57+
<reportElement mode="Opaque" x="111" y="0" width="111" height="20" forecolor="#006699" backcolor="#E6E6E6" uuid="e9b64414-ad92-4257-a8f2-63a23c460255">
58+
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="372fc4a5-58e8-4f68-9b0b-b8c92b4521d4"/>
59+
</reportElement>
60+
<textElement textAlignment="Center">
61+
<font size="14" isBold="true"/>
62+
</textElement>
63+
<text><![CDATA[Name]]></text>
64+
</staticText>
65+
<staticText>
66+
<reportElement mode="Opaque" x="222" y="0" width="111" height="20" forecolor="#006699" backcolor="#E6E6E6" uuid="12ce83f4-fe29-45f6-b1d5-5e016771ec1b">
67+
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="aafde1ac-9f21-4267-b383-ffd18072315c"/>
68+
</reportElement>
69+
<textElement textAlignment="Center">
70+
<font size="14" isBold="true"/>
71+
</textElement>
72+
<text><![CDATA[Designation]]></text>
73+
</staticText>
74+
<staticText>
75+
<reportElement mode="Opaque" x="333" y="0" width="111" height="20" forecolor="#006699" backcolor="#E6E6E6" uuid="e2a0186b-afad-426b-b9f2-2152394df140">
76+
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="c05cb4c4-7321-464e-b20b-38a19f892a19"/>
77+
</reportElement>
78+
<textElement textAlignment="Center">
79+
<font size="14" isBold="true"/>
80+
</textElement>
81+
<text><![CDATA[Salary]]></text>
82+
</staticText>
83+
<staticText>
84+
<reportElement mode="Opaque" x="444" y="0" width="111" height="20" forecolor="#006699" backcolor="#E6E6E6" uuid="6d3e02fc-d8db-485a-96c9-54fa46d8a0ef">
85+
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="95c0a1aa-91d9-473d-a271-b59d6dea0342"/>
86+
</reportElement>
87+
<textElement textAlignment="Center">
88+
<font size="14" isBold="true"/>
89+
</textElement>
90+
<text><![CDATA[D.O.J]]></text>
91+
</staticText>
92+
</band>
93+
</columnHeader>
94+
<detail>
95+
<band height="20">
96+
<line>
97+
<reportElement positionType="FixRelativeToBottom" x="0" y="19" width="555" height="1" uuid="305d3455-6f0a-49e7-ad2a-055974675fd4"/>
98+
</line>
99+
<textField isStretchWithOverflow="true">
100+
<reportElement x="0" y="0" width="111" height="20" uuid="1324277f-1cf1-4cc2-a5f3-88ededc0ff6e">
101+
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="40a9814d-ecb4-424f-aea8-cad246f112b5"/>
102+
</reportElement>
103+
<textElement textAlignment="Center">
104+
<font size="14"/>
105+
</textElement>
106+
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
107+
</textField>
108+
<textField isStretchWithOverflow="true">
109+
<reportElement x="111" y="0" width="111" height="20" uuid="b9c4d404-6ffe-47b5-aeaf-ee9ffb804c6a">
110+
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="372fc4a5-58e8-4f68-9b0b-b8c92b4521d4"/>
111+
</reportElement>
112+
<textElement textAlignment="Center">
113+
<font size="14"/>
114+
</textElement>
115+
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
116+
</textField>
117+
<textField isStretchWithOverflow="true">
118+
<reportElement x="222" y="0" width="111" height="20" uuid="72ac64bb-1484-4b8a-8bdb-6ddcf6c57c70">
119+
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="aafde1ac-9f21-4267-b383-ffd18072315c"/>
120+
</reportElement>
121+
<textElement textAlignment="Center">
122+
<font size="14"/>
123+
</textElement>
124+
<textFieldExpression><![CDATA[$F{designation}]]></textFieldExpression>
125+
</textField>
126+
<textField isStretchWithOverflow="true">
127+
<reportElement x="333" y="0" width="111" height="20" uuid="c7d9f004-ba41-4c99-9a6c-cfbf3173864a">
128+
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="c05cb4c4-7321-464e-b20b-38a19f892a19"/>
129+
</reportElement>
130+
<textElement textAlignment="Center">
131+
<font size="14"/>
132+
</textElement>
133+
<textFieldExpression><![CDATA[$F{salary}]]></textFieldExpression>
134+
</textField>
135+
<textField isStretchWithOverflow="true">
136+
<reportElement x="444" y="0" width="111" height="20" uuid="f6b932f3-4a67-4720-9e27-53a9da43dedc">
137+
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="95c0a1aa-91d9-473d-a271-b59d6dea0342"/>
138+
</reportElement>
139+
<textElement textAlignment="Center">
140+
<font size="14"/>
141+
</textElement>
142+
<textFieldExpression><![CDATA[$F{doj}]]></textFieldExpression>
143+
</textField>
144+
</band>
145+
</detail>
146+
<columnFooter>
147+
<band/>
148+
</columnFooter>
149+
<pageFooter>
150+
<band height="17">
151+
<textField>
152+
<reportElement mode="Opaque" x="0" y="4" width="515" height="13" backcolor="#E6E6E6" uuid="c7f5dcbd-921c-4949-8f5c-3922c35efa2c"/>
153+
<textElement textAlignment="Right"/>
154+
<textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
155+
</textField>
156+
<textField evaluationTime="Report">
157+
<reportElement mode="Opaque" x="515" y="4" width="40" height="13" backcolor="#E6E6E6" uuid="0f776560-e86c-4c18-bb5a-76c0842abe06"/>
158+
<textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
159+
</textField>
160+
<textField pattern="EEEEE dd MMMMM yyyy">
161+
<reportElement x="0" y="4" width="100" height="13" uuid="4f042a9c-a347-4667-9795-ae1c1612e30f"/>
162+
<textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
163+
</textField>
164+
</band>
165+
</pageFooter>
166+
<summary>
167+
<band/>
168+
</summary>
169+
</jasperReport>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.javatechie.report;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class SpringBootJasperReportApplicationTests {
8+
9+
@Test
10+
void contextLoads() {
11+
}
12+
13+
}

0 commit comments

Comments
 (0)