티스토리 뷰

Programming/Java

iReport

파란크리스마스 2019. 6. 1. 20:32
728x90

출처

가상 레코드 생성 예제

public class DemoReport {
	
	public static void main(String[] args) throws Exception {
		JasperReport jasperReport = JasperCompileManager.compileReport("src/ireport/report1.jrxml");
		
		// creating the data source
		JRDataSource dataSource = new JREmptyDataSource(1000); // 가상의 1000개의 레코드 생성
		
		JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);
		
		// view report to UI
		jasperReport.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL);
		JasperViewer.viewReport(jasperPrint, false);
	}
}

PDF 출력 예제

public class DemoReport2 {

	public static void main(String[] args) throws Exception {
		String outfilename = "src/ireport/report1.pdf";
		JasperReport jasperReport = JasperCompileManager.compileReport("src/ireport/report1.jrxml");

		// compile report hm.put(“REPORT_TITLE”,”This is the title of the report”)
		HashMap<String, Object> hm = new HashMap<String, Object>();
		hm.put("id", "121");

		List list = new ArrayList();
		list.add("test data");
		JRBeanCollectionDataSource jrbcds = new JRBeanCollectionDataSource(list, false);

		// 리포트 목록
		// List<JasperPrint> japerPrintList = new ArrayList<JasperPrint>();

		JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, hm, jrbcds);

		// PDF 출력
		JRExporter ex = new JRPdfExporter();
		ex.setParameter(JRPdfExporterParameter.IS_COMPRESSED, true);
		ex.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outfilename);
		// ex.setParameter(JRExporterParameter.JASPER_PRINT_LIST, japerPrintList);
		ex.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
		ex.exportReport();
	}
}

JRDataSource 확장

필드 추가

필드 이름 변경

필드 객체를 선택해서 Detail 밴드에 드래그해 놓기

필드 배치

최종 결과물

커스텀 JRDataSource

import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

public class SampleDS implements JRDataSource {

	String[] fields;
	String[][] data;

	private Map<String, Integer> fieldIndex = new HashMap<String, Integer>();
	
	public SampleDS(String[] fields, String[][] data) {
		this.fields = fields;
		this.data = data;

		for (int i = 0; i < fields.length; i++)
			fieldIndex.put(fields[i], i);
	}

	int pos = -1;

	@Override
	public boolean next() throws JRException {
		for (;;) {
			pos++;
			if (pos >= data.length)
				return false;

			return true;
		}
	}

	@Override
	public Object getFieldValue(JRField jrField) throws JRException {
		Integer index = fieldIndex.get(jrField.getName());
		return data[pos][index];
	}
	
	public static JRDataSource getDataSource() {
		String[] fields = { "a", "b", "c", "d", "e" }; 
		String[][] data = {
				{ "a1", "b1", "c1", "d1", "e1" },
				{ "a2", "b2", "c2", "d2", "e2" },
				{ "a3", "b3", "c3", "d3", "e3" }
		};
		
		return new SampleDS(fields, data);
	}

}

예제소스

public class DemoReport3 {
	
	public static void main(String[] args) throws Exception {
		String outfilename = "src/ireport/report2.pdf";
		JasperReport jasperReport = JasperCompileManager.compileReport("src/ireport/report2.jrxml");
		
		// 커스텀 JRDataSource 생성
		JRDataSource dataSource = SampleDS.getDataSource();
		
		//
		JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);

		// PDF 출력
		JRExporter ex =new JRPdfExporter();
		ex.setParameter(JRPdfExporterParameter.IS_COMPRESSED, true);
		ex.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outfilename);
		//ex.setParameter(JRExporterParameter.JASPER_PRINT_LIST, japerPrintList);
		ex.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
		ex.exportReport();
	}

}

실행

동적으로 이미지 출력

디자인

Image Expression

$P{image_home}+"/"+$F{image1}

ImageBean 소스

public class ImageBean {
	
	private String image1;
	
	private String image2;

	public ImageBean(String image1, String image2) {
		super();
		this.image1 = image1;
		this.image2 = image2;
	}

	public String getImage1() {
		return image1;
	}

	public void setImage1(String image1) {
		this.image1 = image1;
	}

	public String getImage2() {
		return image2;
	}

	public void setImage2(String image2) {
		this.image2 = image2;
	}

}

이미지 출력 예제 소스

public class DemoReport4 {
	
	public static void main(String[] args) throws Exception {
		String outfilename = "src/ireport/report3.pdf";
		JasperReport jasperReport = JasperCompileManager.compileReport("src/ireport/report3.jrxml");
		
		// compile report hm.put(“REPORT_TITLE”,”This is the title of the report”)
		HashMap<String, Object> hm = new HashMap<String, Object>();
		hm.put("image_home", "C:\\Users\\bluesanta\\Pictures");
		
		//
		List list = new ArrayList();
		list.add(new ImageBean("73196c49-adeb-4ebd-9bde-069791498a28.jpg", "70947765.jpg"));
		JRBeanCollectionDataSource jrbcds = new JRBeanCollectionDataSource(list, false);
		
		// 리포트 목록
		// List<JasperPrint> japerPrintList = new ArrayList<JasperPrint>();

		JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, hm, jrbcds);

		// PDF 출력
		JRExporter ex = new JRPdfExporter();
		ex.setParameter(JRPdfExporterParameter.IS_COMPRESSED, true);
		ex.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outfilename);
		// ex.setParameter(JRExporterParameter.JASPER_PRINT_LIST, japerPrintList);
		ex.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
		ex.exportReport();
	}

}
댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함