Intro to JasperReports with Spring (#1608)
This commit is contained in:
parent
190ce77a0b
commit
13a42ea281
|
@ -107,6 +107,12 @@
|
||||||
<version>${guava.version}</version>
|
<version>${guava.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.sf.jasperreports</groupId>
|
||||||
|
<artifactId>jasperreports</artifactId>
|
||||||
|
<version>${jasperreports.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- logging -->
|
<!-- logging -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -296,6 +302,7 @@
|
||||||
<ehcache.version>3.1.3</ehcache.version>
|
<ehcache.version>3.1.3</ehcache.version>
|
||||||
<easymock.version>3.4</easymock.version>
|
<easymock.version>3.4</easymock.version>
|
||||||
<assertj.version>3.6.1</assertj.version>
|
<assertj.version>3.6.1</assertj.version>
|
||||||
|
<jasperreports.version>6.4.0</jasperreports.version>
|
||||||
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
package org.baeldung.jasperreports;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.baeldung.jasperreports.config.JasperRerportsSimpleConfig;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
|
ctx.register(JasperRerportsSimpleConfig.class);
|
||||||
|
ctx.refresh();
|
||||||
|
|
||||||
|
SimpleReportFiller simpleReportFiller = ctx.getBean(SimpleReportFiller.class);
|
||||||
|
simpleReportFiller.setReportFileName("employeeEmailReport.jrxml");
|
||||||
|
simpleReportFiller.compileReport();
|
||||||
|
|
||||||
|
simpleReportFiller.setReportFileName("employeeReport.jrxml");
|
||||||
|
simpleReportFiller.compileReport();
|
||||||
|
|
||||||
|
Map<String, Object> parameters = new HashMap<>();
|
||||||
|
parameters.put("title", "Employee Report Example");
|
||||||
|
parameters.put("minSalary", 15000.0);
|
||||||
|
parameters.put("condition", " LAST_NAME ='Smith' ORDER BY FIRST_NAME");
|
||||||
|
|
||||||
|
simpleReportFiller.setParameters(parameters);
|
||||||
|
simpleReportFiller.fillReport();
|
||||||
|
|
||||||
|
SimpleReportExporter simpleExporter = ctx.getBean(SimpleReportExporter.class);
|
||||||
|
simpleExporter.setJasperPrint(simpleReportFiller.getJasperPrint());
|
||||||
|
|
||||||
|
simpleExporter.exportToPdf("employeeReport.pdf", "baeldung");
|
||||||
|
simpleExporter.exportToXlsx("employeeReport.xlsx", "Employee Data");
|
||||||
|
simpleExporter.exportToCsv("employeeReport.csv");
|
||||||
|
simpleExporter.exportToHtml("employeeReport.html");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,115 @@
|
||||||
|
package org.baeldung.jasperreports;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import net.sf.jasperreports.engine.JRException;
|
||||||
|
import net.sf.jasperreports.engine.JasperPrint;
|
||||||
|
import net.sf.jasperreports.engine.export.HtmlExporter;
|
||||||
|
import net.sf.jasperreports.engine.export.JRCsvExporter;
|
||||||
|
import net.sf.jasperreports.engine.export.JRPdfExporter;
|
||||||
|
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
|
||||||
|
import net.sf.jasperreports.export.SimpleExporterInput;
|
||||||
|
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
|
||||||
|
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
|
||||||
|
import net.sf.jasperreports.export.SimplePdfExporterConfiguration;
|
||||||
|
import net.sf.jasperreports.export.SimplePdfReportConfiguration;
|
||||||
|
import net.sf.jasperreports.export.SimpleWriterExporterOutput;
|
||||||
|
import net.sf.jasperreports.export.SimpleXlsxReportConfiguration;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class SimpleReportExporter {
|
||||||
|
|
||||||
|
private JasperPrint jasperPrint;
|
||||||
|
|
||||||
|
public SimpleReportExporter() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public SimpleReportExporter (JasperPrint jasperPrint){
|
||||||
|
this.jasperPrint = jasperPrint;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JasperPrint getJasperPrint() {
|
||||||
|
return jasperPrint;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setJasperPrint(JasperPrint jasperPrint) {
|
||||||
|
this.jasperPrint = jasperPrint;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void exportToPdf(String fileName, String author) {
|
||||||
|
|
||||||
|
// print report to file
|
||||||
|
JRPdfExporter exporter = new JRPdfExporter();
|
||||||
|
|
||||||
|
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
||||||
|
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(fileName));
|
||||||
|
|
||||||
|
SimplePdfReportConfiguration reportConfig = new SimplePdfReportConfiguration();
|
||||||
|
reportConfig.setSizePageToContent(true);
|
||||||
|
reportConfig.setForceLineBreakPolicy(false);
|
||||||
|
|
||||||
|
SimplePdfExporterConfiguration exportConfig = new SimplePdfExporterConfiguration();
|
||||||
|
exportConfig.setMetadataAuthor(author);
|
||||||
|
exportConfig.setEncrypted(true);
|
||||||
|
exportConfig.setAllowedPermissionsHint("PRINTING");
|
||||||
|
|
||||||
|
exporter.setConfiguration(reportConfig);
|
||||||
|
exporter.setConfiguration(exportConfig);
|
||||||
|
try {
|
||||||
|
exporter.exportReport();
|
||||||
|
} catch (JRException ex) {
|
||||||
|
Logger.getLogger(SimpleReportFiller.class.getName())
|
||||||
|
.log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void exportToXlsx(String fileName,String sheetName) {
|
||||||
|
JRXlsxExporter exporter = new JRXlsxExporter();
|
||||||
|
|
||||||
|
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
||||||
|
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(fileName));
|
||||||
|
|
||||||
|
SimpleXlsxReportConfiguration reportConfig = new SimpleXlsxReportConfiguration();
|
||||||
|
reportConfig.setSheetNames(new String[]{sheetName});
|
||||||
|
|
||||||
|
exporter.setConfiguration(reportConfig);
|
||||||
|
|
||||||
|
try {
|
||||||
|
exporter.exportReport();
|
||||||
|
} catch (JRException ex) {
|
||||||
|
Logger.getLogger(SimpleReportFiller.class.getName())
|
||||||
|
.log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void exportToCsv(String fileName) {
|
||||||
|
JRCsvExporter exporter = new JRCsvExporter();
|
||||||
|
|
||||||
|
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
||||||
|
exporter.setExporterOutput(new SimpleWriterExporterOutput(fileName));
|
||||||
|
|
||||||
|
try {
|
||||||
|
exporter.exportReport();
|
||||||
|
} catch (JRException ex) {
|
||||||
|
Logger.getLogger(SimpleReportFiller.class.getName())
|
||||||
|
.log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void exportToHtml(String fileName) {
|
||||||
|
HtmlExporter exporter = new HtmlExporter();
|
||||||
|
|
||||||
|
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
||||||
|
exporter.setExporterOutput(new SimpleHtmlExporterOutput(fileName));
|
||||||
|
|
||||||
|
try {
|
||||||
|
exporter.exportReport();
|
||||||
|
} catch (JRException ex) {
|
||||||
|
Logger.getLogger(SimpleReportFiller.class.getName())
|
||||||
|
.log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,90 @@
|
||||||
|
package org.baeldung.jasperreports;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import net.sf.jasperreports.engine.JRException;
|
||||||
|
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.util.JRSaver;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class SimpleReportFiller {
|
||||||
|
|
||||||
|
private String reportFileName;
|
||||||
|
|
||||||
|
private JasperReport jasperReport;
|
||||||
|
|
||||||
|
private JasperPrint jasperPrint;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
|
private Map<String, Object> parameters;
|
||||||
|
|
||||||
|
public SimpleReportFiller() {
|
||||||
|
parameters = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void prepareReport() {
|
||||||
|
compileReport();
|
||||||
|
fillReport();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void compileReport() {
|
||||||
|
try {
|
||||||
|
InputStream reportStream = getClass().getResourceAsStream("/".concat(reportFileName));
|
||||||
|
jasperReport = JasperCompileManager.compileReport(reportStream);
|
||||||
|
JRSaver.saveObject(jasperReport, reportFileName.replace(".jrxml", ".jasper"));
|
||||||
|
} catch (JRException ex) {
|
||||||
|
Logger.getLogger(SimpleReportFiller.class.getName())
|
||||||
|
.log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void fillReport() {
|
||||||
|
try {
|
||||||
|
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());
|
||||||
|
} catch (JRException | SQLException ex) {
|
||||||
|
Logger.getLogger(SimpleReportFiller.class.getName())
|
||||||
|
.log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataSource getDataSource() {
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDataSource(DataSource dataSource) {
|
||||||
|
this.dataSource = dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> getParameters() {
|
||||||
|
return parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParameters(Map<String, Object> parameters) {
|
||||||
|
this.parameters = parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getReportFileName() {
|
||||||
|
return reportFileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReportFileName(String reportFileName) {
|
||||||
|
this.reportFileName = reportFileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JasperPrint getJasperPrint() {
|
||||||
|
return jasperPrint;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package org.baeldung.jasperreports.config;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import org.baeldung.jasperreports.SimpleReportExporter;
|
||||||
|
import org.baeldung.jasperreports.SimpleReportFiller;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||||
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class JasperRerportsSimpleConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DataSource dataSource() {
|
||||||
|
return new EmbeddedDatabaseBuilder()
|
||||||
|
.setType(EmbeddedDatabaseType.HSQL)
|
||||||
|
.addScript("classpath:employee-schema.sql")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public SimpleReportFiller reportFiller() {
|
||||||
|
return new SimpleReportFiller();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public SimpleReportExporter reportExporter() {
|
||||||
|
return new SimpleReportExporter();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
CREATE TABLE EMPLOYEE(
|
||||||
|
ID INT NOT NULL PRIMARY KEY,
|
||||||
|
FIRST_NAME VARCHAR(255),
|
||||||
|
LAST_NAME VARCHAR(255),
|
||||||
|
SALARY DOUBLE,
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE EMAIL(
|
||||||
|
ID INT NOT NULL PRIMARY KEY,
|
||||||
|
ID_EMPLOYEE VARCHAR(255),
|
||||||
|
ADDRESS VARCHAR(255)
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO EMPLOYEE VALUES (1, 'John', 'Doe', 10000.10);
|
||||||
|
INSERT INTO EMPLOYEE VALUES (2, 'Kevin', 'Smith', 20000.20);
|
||||||
|
INSERT INTO EMPLOYEE VALUES (3, 'Kim', 'Smith', 30000.30);
|
||||||
|
INSERT INTO EMPLOYEE VALUES (4, 'Stephen', 'Torvalds', 40000.40);
|
||||||
|
INSERT INTO EMPLOYEE VALUES (5, 'Christian', 'Reynolds', 50000.50);
|
||||||
|
|
||||||
|
INSERT INTO EMAIL VALUES (1, 1, 'john@baeldung.com');
|
||||||
|
INSERT INTO EMAIL VALUES (2, 1, 'john@gmail.com');
|
||||||
|
INSERT INTO EMAIL VALUES (3, 2, 'kevin@baeldung.com');
|
||||||
|
INSERT INTO EMAIL VALUES (4, 3, 'kim@baeldung.com');
|
||||||
|
INSERT INTO EMAIL VALUES (5, 3, 'kim@gmail.com');
|
||||||
|
INSERT INTO EMAIL VALUES (6, 3, 'kim@outlook.com');
|
||||||
|
INSERT INTO EMAIL VALUES (7, 4, 'stephen@baeldung.com');
|
||||||
|
INSERT INTO EMAIL VALUES (8, 5, 'christian@gmail.com');
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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="employeeReport" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
|
||||||
|
<parameter name="idEmployee" class="java.lang.Integer" isForPrompting="false"/>
|
||||||
|
<queryString>
|
||||||
|
<![CDATA[SELECT * FROM EMAIL WHERE ID_EMPLOYEE = $P{idEmployee}]]>
|
||||||
|
</queryString>
|
||||||
|
<field name="ADDRESS" class="java.lang.String"/>
|
||||||
|
<detail>
|
||||||
|
<band height="20" splitType="Stretch">
|
||||||
|
<textField>
|
||||||
|
<reportElement x="0" y="0" width="156" height="20"/>
|
||||||
|
<textElement/>
|
||||||
|
<textFieldExpression class="java.lang.String"><![CDATA[$F{ADDRESS}]]></textFieldExpression>
|
||||||
|
</textField>
|
||||||
|
</band>
|
||||||
|
</detail>
|
||||||
|
</jasperReport>
|
|
@ -0,0 +1,51 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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="employeeReport" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
|
||||||
|
<parameter name="title" class="java.lang.String" isForPrompting="false"/>
|
||||||
|
<parameter name="condition" class="java.lang.String" isForPrompting="false">
|
||||||
|
<defaultValueExpression><![CDATA[" 1 = 1"]]></defaultValueExpression>
|
||||||
|
</parameter>
|
||||||
|
<parameter name="minSalary" class="java.lang.Double" isForPrompting="false"/>
|
||||||
|
<queryString>
|
||||||
|
<![CDATA[SELECT * FROM EMPLOYEE WHERE SALARY >= $P{minSalary} AND $P!{condition}]]>
|
||||||
|
</queryString>
|
||||||
|
<field name="FIRST_NAME" class="java.lang.String"/>
|
||||||
|
<field name="LAST_NAME" class="java.lang.String"/>
|
||||||
|
<field name="SALARY" class="java.lang.Double"/>
|
||||||
|
<field name="ID" class="java.lang.Integer"/>
|
||||||
|
<title>
|
||||||
|
<band height="20" splitType="Stretch">
|
||||||
|
<textField>
|
||||||
|
<reportElement x="238" y="0" width="100" height="20"/>
|
||||||
|
<textElement/>
|
||||||
|
<textFieldExpression class="java.lang.String"><![CDATA[$P{title}]]></textFieldExpression>
|
||||||
|
</textField>
|
||||||
|
</band>
|
||||||
|
</title>
|
||||||
|
<detail>
|
||||||
|
<band height="47" splitType="Stretch">
|
||||||
|
<textField>
|
||||||
|
<reportElement x="0" y="0" width="100" height="20"/>
|
||||||
|
<textElement/>
|
||||||
|
<textFieldExpression class="java.lang.String"><![CDATA[$F{FIRST_NAME}]]></textFieldExpression>
|
||||||
|
</textField>
|
||||||
|
<textField>
|
||||||
|
<reportElement x="100" y="0" width="100" height="20"/>
|
||||||
|
<textElement/>
|
||||||
|
<textFieldExpression class="java.lang.String"><![CDATA[$F{LAST_NAME}]]></textFieldExpression>
|
||||||
|
</textField>
|
||||||
|
<textField>
|
||||||
|
<reportElement x="200" y="0" width="100" height="20"/>
|
||||||
|
<textElement/>
|
||||||
|
<textFieldExpression class="java.lang.String"><![CDATA[$F{SALARY}]]></textFieldExpression>
|
||||||
|
</textField>
|
||||||
|
<subreport>
|
||||||
|
<reportElement x="0" y="20" width="300" height="27"/>
|
||||||
|
<subreportParameter name="idEmployee">
|
||||||
|
<subreportParameterExpression><![CDATA[$F{ID}]]></subreportParameterExpression>
|
||||||
|
</subreportParameter>
|
||||||
|
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
|
||||||
|
<subreportExpression class="java.lang.String"><![CDATA["employeeEmailReport.jasper"]]></subreportExpression>
|
||||||
|
</subreport>
|
||||||
|
</band>
|
||||||
|
</detail>
|
||||||
|
</jasperReport>
|
Loading…
Reference in New Issue