Reporting using JASPER

Posted By : Shubhankar Ajmani | 30-Jul-2020

Loading...

Jasper Reports are one of the best ways in JAVA to ANALYZE and SUMMARIZE data.

It is a Library that helps to export report in any of the format like xls,pdf etc

Work on Jasper starts with designing templates using iReport ,JASPER STUDIO and many other tools. The template file is a JRXML file which includes all the design which we create using either of the tools.

Following is the sample file which we create using JASPER STUDIO for one of our client

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.11.0.final using JasperReports Library version 6.11.0-0c4056ccaa4d25a5a8c45672d2f764ea3498bebb  -->
<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="Blank_A4_Landscape" pageWidth="2384" pageHeight="1684" orientation="Landscape" columnWidth="2344" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="37cf0516-4d28-46f5-8be3-5ae3a42a4039">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="New Data Adapter "/>
    <queryString language="SQL">
        
    </queryString>
    <field name="po_id" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="po_id"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ct_po"/>
    </field>
    <field name="mode_of_transportations" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="mode_of_transportations"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ct_shipment"/>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <columnHeader>
        <band height="15" splitType="Stretch">
            <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.spreadsheet.SpreadsheetLayout"/>
                <reportElement x="2200" y="-1" width="100" height="15" uuid="8ddf94cd-7b93-47a9-aee2-7a72b57ea614"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[ATD]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="15" splitType="Stretch">
            <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.spreadsheet.SpreadsheetLayout"/>
            <textField>
                <reportElement x="1300" y="0" width="100" height="15" uuid="90fb73b2-978f-4be4-a5bd-67a74279cfce"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{request_inbound_date}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="2100" y="0" width="100" height="15" uuid="2ab6fca0-67f0-40c0-b861-9bd7f2bd39d1"/>
                <textFieldExpression><![CDATA[$F{ata}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="2200" y="0" width="100" height="15" uuid="6cd0e335-ecee-41f7-a612-b0e9c693409f"/>
                <textFieldExpression><![CDATA[$F{atd}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

After creating the respective JRXML template design file we make use of that in spring boot application file.Write the code for compliling the code

We aslo need DATA SOURCE object to be created either create by defined the properties seprately in class file or by injectiong Data Source and using the properties sepecified in application.properties file

@Service
@Slf4j
public class ReportFillerService {
    private String reportFileName;
    private JasperReport jasperReport;
    private JasperPrint jasperPrint;
    @Autowired
    private DataSource dataSource;
    private Map<String, Object> parameters;
    public ReportFillerService() {
        parameters = new HashMap<>();
    }
    public void prepareReport() throws SQLException {
        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(ReportFillerService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void fillReport() throws SQLException  {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
        } catch (JRException | SQLException ex) {
            Logger.getLogger(ReportFillerService.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally {
            connection.close();
        }
    }
    
}

After compiling the file we further do reproduce the file in specific format we require like html,pdf,csv,xlsx etc

Various inbuild library classes are helpup in populating the report.

@Service
@Slf4j
public class ReportExporterService {
    private JasperPrint jasperPrint;
    public ReportExporterService() {
    }
    public ReportExporterService(JasperPrint jasperPrint) {
        this.jasperPrint = jasperPrint;
    }
    public JasperPrint getJasperPrint() {
        return jasperPrint;
    }
    public void setJasperPrint(JasperPrint jasperPrint) {
        this.jasperPrint = jasperPrint;
    }
    

We can export the file in csv format as well, using inbuilt JRCsvExporter class provided by Jasper Library.

It also provide various other inbuilt class to export to html also

    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(ReportExporterService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}

Code to export to xlsx format we have to mention variou cell properties and produce output stream of that.We can give the tab name of excel file

public byte[] exportToXlsx(String fileName, String sheetName) {

        JRXlsxExporter exporter = new JRXlsxExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));byte[] rawBytes;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        SimpleOutputStreamExporterOutput simpleOutputStreamExporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
        exporter.setExporterOutput(simpleOutputStreamExporterOutput);

        SimpleXlsxReportConfiguration reportConfig = new SimpleXlsxReportConfiguration();
        reportConfig.setSheetNames(new String[]{sheetName});
        exporter.setConfiguration(reportConfig);
        try {  exporter.exportReport();} catch (Exception ex) {
      ex.printStackTrace();    } rawBytes = outputStream.toByteArray();return rawBytes;


Its export excel functionallity add to statiscal benefit of analysy.
It can be achieved in ERP Domain from warehousing to hr subdomains.
To keep the records.

Further you can follow various alternatives like Next Reports,SSRS Reporting etc.


Its benefitial for ERP Domain in achieving high complex calculation easily.

Jasper is an open source Java reporting tool that helps generate dynamic content. We are a Java Development Company with an objective to provide the best-in-class development services for SaaS models for various software applications and suites. Our services include custom development for CRM, WFMS, HRM, WMS, eCommerce, Finance, and Accounting software for your business. Talk to our experts to find the best software solutions that match your business needs.