jasper report

5
Jasper report Khái niệm: Jasper report là framework cho phép thiết kế, dịch và xuất báo cáo dưới nhiều định dạng khác nhau như PDF, Excel, HTML, Java Applet Viewer, plain text. Jasper report là công nghệ mã nguồn mở được phát triển bởi Jaspersoft. Thiết kế báo cáo: Để thiết kế báo cáo, ta sử dụng công cụ iReport: Một báo cáo bao gồm có 7 phần chính như trong hình trên: - Title: phần tiêu đề của báo cáo, nó xuất hiện một lần duy nhất tại phần đầu của trang đầu báo cáo.

Upload: hoang-nam

Post on 15-Sep-2015

218 views

Category:

Documents


0 download

DESCRIPTION

asdsaf dsadsafa

TRANSCRIPT

Jasper reportKhi nim: Jasper report l framework cho php thit k, dch v xut bo co di nhiu nh dng khc nhau nh PDF, Excel, HTML, Java Applet Viewer, plain text. Jasper report l cng ngh m ngun m c pht trin bi Jaspersoft.Thit k bo co: thit k bo co, ta s dng cng c iReport:

Mt bo co bao gm c 7 phn chnh nh trong hnh trn:

Title: phn tiu ca bo co, n xut hin mt ln duy nht ti phn u ca trang u bo co.

Page Header: phn tiu ca tng trang bo co, n xut hin ti phn u ca mi trang bo co.

Colunm Header: phn tiu ca cc ct d liu.

Detail: phn lp li ca bo co. N dng hin th d liu ca bo co.

Colunm Footer: phn kt thc ca cc ct d liu. N thng dng hin th gi tr m s lng, tng hoc tnh trung bnh ca cc gi tr ca ct tng ng.

Page Footer: phn kt thc ca cc trang. N thng c dng tng kt, thng k cc gi tr trong tng trang hoc nh s trang.

Summary: phn hin th tng kt, thng k ca c bo co. N xut hin mt ln duy nht ti cui bo co.

Trong iReport cho php nh ngha datasource dng lm ngun cho bo co. Mc nh, t datasource, iReport s trch xut ra c cc ct d liu (FIELD) phc v cho vic hin th trong phn Detail v cc thng k trong phn Page Footer, Summary. Ngoi ra, jasper report cho php ngi dng nh ngha ra cc tham s (PARAMETER) c th truyn vo trong lc dch bo co; nh ngha cc bin (VARIABLE) cho php tnh ton gi tr t cc parameter, cc field ly t datasource. thit k bo co, ta s dng cc i tng c h tr sn trong panel Palette. Cc i tng quan trng:

Static text: Cho php hin th ni dung mt chui xc nh trc ln bo co.

Text Field: Cho php hin th ni dung ca cc FIELD, PARAMETER, VARIABLE thng qua c php $F{TEN_FIELD}, $P{TEN_PARAMETER}, $V{TEN_VARIABLE}

Line: cho php hin th ng k trn bo co. Lu : mt line khng c.

Image: cho php hin th nh trn bo co Table: cho php hin th datasource khc c nh ngha trong bo co di dng bng d liu.

Sau khi thit k bo co, ta c bo co tng t nh bo co sau:

Kt qu ca bo co sau khi c lu l mt file XML c phn m rng l jrxml. File jrxml ny chnh l file ngun c jasper report s dng trong phn lp trnh web.

Dch bo co: Jasper report cung cp b API dng dch file ngun bo co jrxml thnh file bo co (.jasper) sau khi c truyn cc tham s. dch bo co, ta s dng cc phng thc tnh sau:

jasperReport =JasperCompileManager.compileReport(jrxmlReportPath)jasperPrint = JasperFillManager.fillReport(jasperReport,

jrParameters, jrDataSource)hocjasperPrint = JasperFillManager.fillReport(jasperReport,

jrParameters, connection)Trong :

jrxmlReportPath: l ng dn n file jrxml

jasperReport: l bo co sau khi c dch

jrParamaters: Map cc tham s bo co c truyn

jrDataSource: datasource c truyn vo lm ngun cho bo co

connection: connection dng kt ni vo c s d liu

jasperPrint: bo co sau khi c dch v np d liu thc.Xut bo co: jasper report cho php xut ra tng i nhiu nh dng khc nhau nh PDF, Excel, HTML hay applet viewer.

on code cho php xut bo co di dng PDF:response.setContentType("application/pdf");

response.addHeader("Content-Disposition", "attachment; filename=\"TenFile.pdf\"");

response.setCharacterEncoding("UTF-8");

JRPdfExporter exporter = new JRPdfExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());

exporter.setParameter(JRPdfExporterParameter.CHARACTER_ENCODING, "UTF-8");

exporter.exportReport();

on code cho php xut bo co di dng Excel:response.setContentType("application/vnd.ms-excel");

response.addHeader("Content-Disposition", "attachment; filename=\"TenFile.xls\"");

JRXlsExporter exporterXLS = new JRXlsExporter();

exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);

exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, response.getOutputStream());

exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);

exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);

exporterXLS.setParameter(JRXlsExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE);

exporterXLS.setParameter(JRXlsExporterParameter.OFFSET_X, 0);

exporterXLS.setParameter(JRXlsExporterParameter.IS_IGNORE_CELL_BORDER, Boolean.FALSE);

exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);

exporterXLS.exportReport();

on code cho php xut bo co di dng HTML:JRHtmlExporter exporter = new JRHtmlExporter();

response.setContentType("text/html");

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

exporter.setParameter(JRHtmlExporterParameter.OUTPUT_WRITER, out);

HashMap fontMap = new HashMap();

exporter.setParameter(JRHtmlExporterParameter.FONT_MAP, fontMap);

exporter.setParameter(JRHtmlExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);

exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);

exporter.exportReport();

on code cho php xut bo co di dng applet viewer:ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());

oos.writeObject(jasperPrint);

oos.flush();