Jasper Reports - 보고서 구현하기
오픈소스 "Jasper Report"로 보고서 구현을 소개하겠습니다.

1. 개발 환경
구분
| 비고
|
개발언어
| JAVA / JDK 1.8 이상(최신 17. 10월 이상 버전)
|
-> 데이터베이스는 MySQL, MSSQL 등 지원함.
2. 필요한 라이브러리
구분 | 라이브러리 명
| 링크 | 비고
|
Jasper Report | jasperreports-6.2.0-project
| 링크(소스포지) | 6.20 버전 이상부터는 6.2.1버전 이상은 lib폴더가 존재하지 않음.
|
Jasper Report | jasperreports-6.4.3-project | 링크(소스포지) | "jasperreports-6.2.0-project\lib" 폴더의 파일을 활용해야 함. |
MySQL Connector (JDBC) | mysql-connector-java-5.1 | 링크(MySQL) | |
참고 링크
1. https://sourceforge.net/projects/jasperreports/files/jasperreports/JasperReports%205.0.0/
2. https://sourceforge.net/projects/jasperreports/files/jasperreports/
3. 설치 / 프로젝트 생성 / 프로젝트 라이브러리 환경 설정하기

|
1. JapserReport 설치하기 (MarketPlace) |
|
2. 프로젝트 생성하기 (SWT)
|
|
3. 라이브러리 추가하기
|
4. 코드 구현
package demo;
import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import entities.*; import model.ProductModel; import net.sf.jasperreports.engine.JRDataSource; 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.swing.JRViewer; import java.util.*; import javax.swing.JScrollPane;
public class JFrameMain extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { JFrameMain frame = new JFrameMain(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }
/** * Create the frame. */ public JFrameMain() { setTitle("보고서"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 745, 608); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); ShowReport(); }
private void ShowReport() { try { ProductModel productModel = new ProductModel(); List<Map<String, ?>> dataSource = new ArrayList<Map<String, ?>>(); for ( Product product : productModel.findAll() ) { Map<String, Object> m = new HashMap<String, Object>(); m.put("id", product.getId() ); m.put("name", product.getName() ); m.put("price", product.getPrice() ); m.put("quantity", product.getQuantity() ); dataSource.add(m); } JRDataSource jrDataSource = new JRBeanCollectionDataSource( dataSource ); String sourceName = "src/demo/ProductReport.jrxml"; JasperReport report = JasperCompileManager.compileReport(sourceName); JasperPrint filledReport = JasperFillManager.fillReport(report, null, jrDataSource ); JRViewer viewer = new JRViewer(filledReport); contentPane.setLayout(null); JScrollPane scrollPane = new JScrollPane(viewer); scrollPane.setBounds(12, 82, 500, 500); contentPane.add(scrollPane); // this.getContentPane().add(viewer); // this.pack(); }catch(Exception e) { JOptionPane.showMessageDialog(null, e.getMessage()); } } }
|
JFrameMain.java
|
Entities: 패키지(Package)
package entities;
public class Product { private String id; private String name; private double price; private int quantity; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public Product(String id, String name, double price, int quantity) { super(); this.id = id; this.name = name; this.price = price; this.quantity = quantity; } public Product() { super(); } }
|
Product.java
|
model: 패키지(Package)
package model; import entities.*; import java.util.*; public class ProductModel { public List <Product> findAll() { List <Product> listProducts = new ArrayList<Product>(); listProducts.add(new Product("p1", "이름 1", 100, 2)); listProducts.add(new Product("p2", "이름 2", 200, 3)); listProducts.add(new Product("p3", "이름 3", 300, 4)); listProducts.add(new Product("p4", "이름 4", 400, 5)); listProducts.add(new Product("p5", "이름 5", 500, 6)); return listProducts; } }
|
ProductModel.java
|
5. jrXML - JasperReport 보고서 양식
|
JasperReport 보고서 - 디자인 하기
|
[소스코드]
JavaSwingWithJasperReport.zip
6. 실행하기
7. 참고 사이트
https://sourceforge.net/projects/jasperreports/files/jasperreports/JasperReports%206.4.3/
JavaSwingWithJasperReport.zip
0.0MB