├── README.md ├── employees.html ├── employees.pdf ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── javatechie │ │ └── report │ │ ├── SpringBootJasperReportApplication.java │ │ ├── entity │ │ └── Employee.java │ │ ├── repository │ │ └── EmployeeRepository.java │ │ └── service │ │ └── ReportService.java └── resources │ ├── application.properties │ └── employees.jrxml └── test └── java └── com └── javatechie └── report └── SpringBootJasperReportApplicationTests.java /README.md: -------------------------------------------------------------------------------- 1 | # spring-jasper-report 2 | How to generate dynamic report using spring boot and jasper 3 | 4 | *Download JasperSoft Studio tool* 5 | Link : https://community.jaspersoft.com/project/jaspersoft-studio/releases 6 | -------------------------------------------------------------------------------- /employees.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 654 |
  13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 31 | 33 | 35 | 37 | 39 | 41 | 43 | 45 | 47 | 49 | 50 | 51 | 53 | 55 | 57 | 59 | 61 | 63 | 65 | 67 | 68 | 69 | 71 | 73 | 75 | 77 | 79 | 80 | 81 | 83 | 85 | 87 | 89 | 91 | 93 | 95 | 97 | 99 | 101 | 102 | 103 | 105 | 106 | 107 | 109 | 111 | 113 | 115 | 117 | 119 | 121 | 122 | 123 | 125 | 126 | 127 | 129 | 171 | 173 | 174 | 175 | 177 | 219 | 221 | 222 | 223 | 225 | 267 | 269 | 270 | 271 | 273 | 315 | 317 | 318 | 319 | 321 | 363 | 365 | 366 | 367 | 369 | 411 | 413 | 414 | 415 | 417 | 459 | 461 | 462 | 463 | 465 | 507 | 509 | 510 | 511 | 513 | 555 | 557 | 558 | 559 | 561 | 603 | 605 | 606 | 607 | 609 | 610 | 611 | 613 | 642 | 644 | 646 | 647 | 648 | 650 | 651 |
30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 |
52 | 54 | Java Techie 56 | 58 | 60 | 62 | 64 | 66 |
70 | 72 | 74 | 76 | Employee Reports 78 |
82 | 84 | 86 | 88 | 90 | 92 | 94 | 96 | 98 | 100 |
104 |
108 | 110 | Id 112 | Name 114 | Designation 116 | Salary 118 | D.O.J 120 |
124 |
128 | 130 |
131 |
132 | 133 | 134 | 135 | 136 | 137 | 139 | 140 | 141 | 143 | 144 |
138 |
142 |
145 |
146 |
147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 158 | 160 | 162 | 164 | 166 | 167 |
157 | 101 159 | Basant 161 | SE 163 | 500000.0 165 | 08/08/2015
168 |
169 |
170 |
172 |
176 | 178 |
179 |
180 | 181 | 182 | 183 | 184 | 185 | 187 | 188 | 189 | 191 | 192 |
186 |
190 |
193 |
194 |
195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 206 | 208 | 210 | 212 | 214 | 215 |
205 | 102 207 | Santosh 209 | SSE 211 | 800000.0 213 | 14/04/2012
216 |
217 |
218 |
220 |
224 | 226 |
227 |
228 | 229 | 230 | 231 | 232 | 233 | 235 | 236 | 237 | 239 | 240 |
234 |
238 |
241 |
242 |
243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 254 | 256 | 258 | 260 | 262 | 263 |
253 | 103 255 | Arun 257 | Architect 259 | 1500000.0 261 | 02/11/2008
264 |
265 |
266 |
268 |
272 | 274 |
275 |
276 | 277 | 278 | 279 | 280 | 281 | 283 | 284 | 285 | 287 | 288 |
282 |
286 |
289 |
290 |
291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 302 | 304 | 306 | 308 | 310 | 311 |
301 | 104 303 | Kousik 305 | Manager 307 | 2800000.0 309 | 18/07/2011
312 |
313 |
314 |
316 |
320 | 322 |
323 |
324 | 325 | 326 | 327 | 328 | 329 | 331 | 332 | 333 | 335 | 336 |
330 |
334 |
337 |
338 |
339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 350 | 352 | 354 | 356 | 358 | 359 |
349 | 105 351 | Swat 353 | Team Lead 355 | 2100000.0 357 | 12/11/2010
360 |
361 |
362 |
364 |
368 | 370 |
371 |
372 | 373 | 374 | 375 | 376 | 377 | 379 | 380 | 381 | 383 | 384 |
378 |
382 |
385 |
386 |
387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 398 | 400 | 402 | 404 | 406 | 407 |
397 | 106 399 | Amit 401 | SSE 403 | 1200000.0 405 | 17/03/2012
408 |
409 |
410 |
412 |
416 | 418 |
419 |
420 | 421 | 422 | 423 | 424 | 425 | 427 | 428 | 429 | 431 | 432 |
426 |
430 |
433 |
434 |
435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 446 | 448 | 450 | 452 | 454 | 455 |
445 | 107 447 | Charan 449 | Module Lead 451 | 500000.0 453 | 21/10/2018
456 |
457 |
458 |
460 |
464 | 466 |
467 |
468 | 469 | 470 | 471 | 472 | 473 | 475 | 476 | 477 | 479 | 480 |
474 |
478 |
481 |
482 |
483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 494 | 496 | 498 | 500 | 502 | 503 |
493 | 108 495 | Krupa 497 | SSE 499 | 2000000.0 501 | 09/04/2015
504 |
505 |
506 |
508 |
512 | 514 |
515 |
516 | 517 | 518 | 519 | 520 | 521 | 523 | 524 | 525 | 527 | 528 |
522 |
526 |
529 |
530 |
531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 542 | 544 | 546 | 548 | 550 | 551 |
541 | 109 543 | Anurag 545 | SSE 547 | 900000.0 549 | 14/12/2017
552 |
553 |
554 |
556 |
560 | 562 |
563 |
564 | 565 | 566 | 567 | 568 | 569 | 571 | 572 | 573 | 575 | 576 |
570 |
574 |
577 |
578 |
579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 590 | 592 | 594 | 596 | 598 | 599 |
589 | 110 591 | Dileep 593 | SE 595 | 100000.0 597 | 15/05/2015
600 |
601 |
602 |
604 |
608 |
612 | 614 |
615 |
616 | 617 | 618 | 619 | 620 | 621 | 623 | 624 |
622 | Page 1 of
625 |
626 |
627 | 628 | 629 | 630 | 631 | 632 | 633 | 635 | 637 | 638 |
634 | Monday 28 October 636 |
639 |
640 |
641 |
643 |  1 645 |
649 |
652 | 653 |
 
655 | 656 | 657 | -------------------------------------------------------------------------------- /employees.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Java-Techie-jt/spring-jasper-report/1f88fd3b76d7e1dd9ef7caf75b339c8d380120d1/employees.pdf -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.0.RELEASE 9 | 10 | 11 | com.javatechie 12 | spring-boot-jasper-report 13 | 0.0.1-SNAPSHOT 14 | spring-boot-jasper-report 15 | Demo project for Spring Boot report generation using JasperReport 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-jpa 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | net.sf.jasperreports 32 | jasperreports 33 | 6.9.0 34 | 35 | 36 | mysql 37 | mysql-connector-java 38 | runtime 39 | 40 | 41 | org.projectlombok 42 | lombok 43 | true 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-test 48 | test 49 | 50 | 51 | org.junit.vintage 52 | junit-vintage-engine 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | org.springframework.boot 62 | spring-boot-maven-plugin 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/report/SpringBootJasperReportApplication.java: -------------------------------------------------------------------------------- 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 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 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/report/entity/Employee.java: -------------------------------------------------------------------------------- 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 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/report/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 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 { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/report/service/ReportService.java: -------------------------------------------------------------------------------- 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 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 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 | } 45 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 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 | 10 | -------------------------------------------------------------------------------- /src/main/resources/employees.jrxml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 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 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | -------------------------------------------------------------------------------- /src/test/java/com/javatechie/report/SpringBootJasperReportApplicationTests.java: -------------------------------------------------------------------------------- 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 | } 14 | --------------------------------------------------------------------------------