Skip to content

Instantly share code, notes, and snippets.

@vakho10
Last active January 1, 2023 03:58
Show Gist options
  • Save vakho10/240148736fc8ecc5b73bbf0cff0628d8 to your computer and use it in GitHub Desktop.
Save vakho10/240148736fc8ecc5b73bbf0cff0628d8 to your computer and use it in GitHub Desktop.
Excel generation example using JasperReports engine in Java
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.5.1.final using JasperReports Library version 6.5.1 -->
<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_1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="81bf5e97-602a-4005-ac1e-dc620119b4d9">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<style name="Default Font Setter Style" isDefault="true" fontName="SylfaenFontExtension" fontSize="11"/>
<parameter name="name" class="java.lang.String"/>
<parameter name="description" class="java.lang.String"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="lastName" class="java.lang.String"/>
<field name="firstName" class="java.lang.String"/>
<field name="phoneNumber" class="java.lang.String"/>
<field name="petitionSignTime" class="java.util.Date"/>
<columnHeader>
<band height="21">
<property name="com.jaspersoft.studio.unit.height" value="cm"/>
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.HorizontalRowLayout"/>
<staticText>
<reportElement x="0" y="0" width="141" height="21" uuid="ace8e2fb-a0a0-45fc-b63a-9288c893beab"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[სახელი]]></text>
</staticText>
<staticText>
<reportElement x="141" y="0" width="138" height="21" uuid="8a910cea-ee5b-479c-9212-b968178510c7"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[გვარი]]></text>
</staticText>
<staticText>
<reportElement x="279" y="0" width="138" height="21" uuid="8646b765-aeec-4924-8757-c777494d74d8"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[მობილური ტელეფონის ნომერი]]></text>
</staticText>
<staticText>
<reportElement x="417" y="0" width="138" height="21" uuid="652cc5f7-a5c9-4c84-97dd-b2392ee8dfdf"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[მობილური ტელეფონის ნომერი]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="21">
<textField>
<reportElement x="0" y="0" width="141" height="21" uuid="b3253ca0-d0ff-449a-b6b5-9b9e5cf9d3e7"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{firstName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="141" y="0" width="139" height="21" uuid="bb134782-42e6-4be4-b5b8-921c7432beaa"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{lastName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="280" y="0" width="137" height="21" uuid="9e64e039-b830-4545-be01-b1b57ae39975"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{phoneNumber}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="417" y="0" width="138" height="21" uuid="9676485b-e997-4606-9ca6-b0aa6263b051"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{petitionSignTime}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
package ge.vakho.test_jasper_excel;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.github.javafaker.Faker;
import net.sf.jasperreports.engine.JRParameter;
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.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.export.OutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleXlsReportConfiguration;
public class Main {
public static void main(String[] args) throws Exception {
try (InputStream templateStream = Main.class.getResourceAsStream("/excel.jrxml");
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
final JasperReport jasperReport = JasperCompileManager.compileReport(templateStream);
// Disable pagination throught parameter
// P. S. New pages are still created if row cound is more than 65535!
final Map<String, Object> parameters = new HashMap<>();
parameters.put(JRParameter.IS_IGNORE_PAGINATION, true);
final JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(getDataBeanList());
final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
configuration.setOnePagePerSheet(true);
configuration.setDetectCellType(true); // Detect cell types (date and etc.)
configuration.setWhitePageBackground(false); // No white background!
configuration.setFontSizeFixEnabled(false);
// No spaces between rows and columns
configuration.setRemoveEmptySpaceBetweenRows(true);
configuration.setRemoveEmptySpaceBetweenColumns(true);
// If you want to name sheets then uncomment this line
// configuration.setSheetNames(new String[] { "Data" });
final JRXlsExporter exporter = new JRXlsExporter();
exporter.setConfiguration(configuration);
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
try (ByteArrayOutputStream excelStream = new ByteArrayOutputStream();
FileOutputStream fos = new FileOutputStream("result.xls")) {
OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(excelStream);
exporter.setExporterOutput(exporterOutput);
exporter.exportReport();
excelStream.writeTo(fos);
}
}
}
public static ArrayList<DataBean> getDataBeanList() {
Faker faker = new Faker();
ArrayList<DataBean> dataBeanList = new ArrayList<DataBean>();
for (int i = 0; i < 100_000; i++) {
dataBeanList.add(new DataBean(faker.name().firstName(), faker.name().lastName(),
faker.phoneNumber().cellPhone(), faker.date().birthday()));
}
return dataBeanList;
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ge.vakho</groupId>
<artifactId>test-jasper-excel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jasper.version>6.8.0</jasper.version>
</properties>
<dependencies>
<!-- JasperReports -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>${jasper.version}</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>sylfaen</groupId>
<artifactId>sylfaen</artifactId>
<version>1.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.javafaker/javafaker -->
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.17.2</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment