1 Aralık 2021 Çarşamba

JasperReports

Giriş
Şu satırı dahil ederiz
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
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.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.export.SimpleCsvExporterConfiguration;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleWriterExporterOutput;
import net.sf.jasperreports.export.SimpleXlsxReportConfiguration;
Açıklaması şöyle
JasperReports is an open source reporting engine. It provides ability to deliver rich content onto to the printer, the screen, or into various formats such as PDF, HTML, XLS, RTF, ODT, CSV, TXT and XML files.

It is a Java library and can be used in a variety of Java-enabled applications to generate dynamic content. Its main purpose is to help create page-oriented, ready-to-print documents in a simple and flexible manner. JasperReports can also be used to provide reporting capabilities in our applications.

As it is not a standalone tool, it cannot be installed on its own. Instead, it is embedded into Java applications by including its library in the application’s CLASSPATH.

Features of JasperReports :
- Flexible report layout.
- Data can be presented either textually or graphically.
- Developers can supply data in multiple ways.
- Multiple data sources can be used to transfer data.
- Watermarks can also be applied.
- Sub reports can also be generated.

Various formats of reports can be exported.
Maven
Şöyle yaparız
<dependency>
  <groupId>net.sf.jasperreports</groupId>
  <artifactId>jasperreports</artifactId>
  <version>6.4.0</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>4.1.1</version>
</dependency>
Örnek
Şöyle yaparız
private String reportPath;

public String generateReport() {
  List<Employee> employees = ...
  try {
    File file = ResourceUtils.getFile("classpath:employee-rpt.jrxml");
    InputStream input = new FileInputStream(file);
    // Compile the Jasper report from .jrxml to .japser
    JasperReport jasperReport = JasperCompileManager.compileReport(input);
    // Get your data source
    JRBeanCollectionDataSource source = new JRBeanCollectionDataSource(employees);
    // Add parameters
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("createdBy", "JavaHelper.org");
    // Fill the report
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,
    source);
    // Export the report to various formats
    JasperExportManager.exportReportToPdfFile(jasperPrint, reportPath + "\\Emp.pdf");
    System.out.println("PDF File Generated !!");
    JasperExportManager.exportReportToXmlFile(jasperPrint, reportPath + "\\Emp.xml",
    true);
    System.out.println("XML File Generated !!");
    JasperExportManager.exportReportToHtmlFile(jasperPrint, reportPath + "\\Emp.html");
    System.out.println("HTML Generated");
    
  } catch (Exception e) {
    ...
  }
}
Daha sonra JasperReport farklı formatlara dönüştürülür. Şöyle yaparız
void csv(JasperPrint jasperPrint) throws JRException {
  JRCsvExporter exporter = new JRCsvExporter();
  exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
  exporter.setExporterOutput(new SimpleWriterExporterOutput(reportPath +
    "\\Employee.csv"));
  SimpleCsvExporterConfiguration configuration = new SimpleCsvExporterConfiguration();
  configuration.setFieldDelimiter(",");
  exporter.setConfiguration(configuration);
  exporter.exportReport();
}


void xlsx(JasperPrint jasperPrint) throws JRException {
  // Exports a JasperReports document to XLSX format. 
  //It has character output type and exports the document to a grid-based layout.
  JRXlsxExporter exporter = new JRXlsxExporter();
  exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
  exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(reportPath +
    "\\Employee.xlsx"));
  SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
  configuration.setOnePagePerSheet(true);
  configuration.setRemoveEmptySpaceBetweenColumns(true);
  configuration.setDetectCellType(true);
  exporter.setConfiguration(configuration);
  exporter.exportReport();
} 
Raport xml şöyledir. title, columnHeader, detail alanlarından oluşur
<?xml ...>

  <property name="ireport.zoom" value="1.0"/>
  <property name="ireport.x" value="0"/>
  <property name="ireport.y" value="0"/>
  <parameter name="createdBy" class="java.lang.String"/>
  <field name="id" class="java.lang.Integer"/>
  <field name="name" class="java.lang.String"/>
  <field name="organization" class="java.lang.String"/>
  <field name="designation" class="java.lang.String"/>
  <field name="salary" class="java.lang.Integer"/>
  <background>
    <band splitType="Stretch"/>
  </background>
  <title>
    <band height="42" splitType="Stretch">
      <staticText>
        <reportElement x="64" y="0" width="481" height="42"/>
	  <textElement textAlignment="Center">
	    <font size="20" isBold="true"/>
	  </textElement>
	  <text><![CDATA[Employee Details]]></text>
      </staticText>
    </band>
  </title>
  <columnHeader>
    <band height="61" splitType="Stretch">
      <staticText>
        <reportElement x="0" y="41" width="111" height="20"/>
	  <box>
            ...
	  </box>
	  <textElement textAlignment="Center">
	    <font size="12" isBold="true" isItalic="false"/>
	  </textElement>
	  <text><![CDATA[ID]]></text>
      </staticText>
      ...
    </band>
  </columnHeader>
  <detail>
    <band height="20" splitType="Stretch">
      <textField>
        <reportElement x="0" y="0" width="111" height="20"/>
	  <box>
	    ...
          </box>
          <textElement textAlignment="Center"/>
	  <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
      </textField>
      ...
    </band>
  </detail>
</jasperReport>



Hiç yorum yok:

Yorum Gönder