jQuery in Action, 2nd edition*
The moose likes Other Open Source Projects and the fly likes Generate excel reports using jasper report Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Generate excel reports using jasper report" Watch "Generate excel reports using jasper report" New topic
Author

Generate excel reports using jasper report

MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
Hi...
I am generating reports using jasper reports.I hav successfully generated pdf report.But no idea how to generate Excel.

Can anyone suggest me the method.


JAVA FILE:


JasperDesign jasperDesign=JRXmlLoader.load("xmlfile");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
Map parameters = new HashMap();
parameters.put("ReportTitle", "Basic JasperReport");
Connection conn=DriverManager.getConnection(database,userName,password);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
JasperExportManager.exportReportToPdfFile(jasperPrint,"pdffile");


XML



<?xml version="1.0" encoding="UTF-8"?>
<!-- Created using an evaluation version of JasperAssistant (http://www.jasperassistant.com) -->
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">


<jasperReport name="report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30">
<property name="com.jasperassistant.designer.GridHeight" value="12"/>
<property name="com.jasperassistant.designer.GridWidth" value="12"/>
<property name="com.jasperassistant.designer.Rulers" value="true"/>
<property name="com.jasperassistant.designer.DataSource" value="Demo"/>
<property name="com.jasperassistant.designer.SnapToGrid" value="true"/>
<property name="com.jasperassistant.designer.Margins" value="true"/>
<property name="com.jasperassistant.designer.Grid" value="true"/>
<property name="com.jasperassistant.designer.SnapToGuides" value="true"/>


<!-- The SQL query that will be used to retrieve the data needed to fill the report -->

<queryString><![CDATA[select *from users]]></queryString>

<!-- Represents the definition of a data field that will store values retrieved from the data source of the report -->

<field name="Status" class="java.lang.String">
</field>
<field name="UpdatedDate" class="java.sql.Timestamp">
</field>
<field name="UserFirstName" class="java.lang.String">
</field>
<field name="UserID" class="java.lang.String">
</field>
<field name="UserLastName" class="java.lang.String">
</field>


<!-- The title section for this report. -->

<title>
<band height="50">

<rectangle>
<reportElement mode="Transparent" x="0" y="0" width="555" height="40"/>
<graphicElement/>
</rectangle>

<staticText>
<reportElement positionType="Float" x="84" y="12" width="397" height="37"/>
<textElement textAlignment="Center">
<font size="12"/>
</textElement>
<text><![CDATA[USER DETAILS]]></text>
</staticText>
</band>
</title>

<!-- The page header section for this report -->


<pageHeader>
<band height="50">
<staticText>
<reportElement positionType="Float" x="12" y="12" width="85" height="25" forecolor="#ff" backcolor="#ffdd99"/>
<textElement/>
<text><![CDATA[USERID]]></text>
</staticText>
<staticText>
<reportElement positionType="Float" x="120" y="12" width="97" height="25" forecolor="#ff" backcolor="#ffdd99" />
<textElement/>
<text><![CDATA[FIRST NAME]]></text>
</staticText>
<staticText>
<reportElement positionType="Float" x="240" y="12" width="85" height="25" forecolor="#ff" backcolor="#ffdd99"/>
<textElement/>
<text><![CDATA[LAST NAME]]></text>
</staticText>
<staticText>
<reportElement positionType="Float" x="348" y="12" width="73" height="25" forecolor="#ff" backcolor="#ffdd99"/>
<textElement/>
<text><![CDATA[UPDATED DATE]]></text>
</staticText>
<staticText>
<reportElement positionType="Float" x="444" y="12" width="85" height="25" forecolor="#ff" backcolor="#ffdd99"/>
<textElement/>
<text><![CDATA[STATUS]]></text>
</staticText>
</band>
</pageHeader>


<!-- The detail section for this report. The detail section is repeted for each line of data supplied by the report's data source. -->


<detail>
<band height="44">
<textField>
<reportElement positionType="Float" x="456" y="12" width="80" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Status}]]></textFieldExpression>
</textField>

<textField>

<reportElement positionType="Float" x="348" y="12" width="80" height="20"/>
<textElement/>
<textFieldExpression class="java.sql.Timestamp"><![CDATA[$F{UpdatedDate}]]></textFieldExpression>

</textField>
<textField>
<reportElement positionType="Float" x="240" y="12" width="80" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{UserFirstName}]]></textFieldExpression>
</textField>
<textField>
<reportElement positionType="Float" x="12" y="12" width="80" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{UserID}]]></textFieldExpression>
</textField>
<textField>
<reportElement positionType="Float" x="132" y="12" width="80" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{UserLastName}]]></textFieldExpression>
</textField>


</band>
</detail>


<!-- Definition of the page footer section for this report. The page footer is printed at the bottom of each page on the report. -->
<pageFooter>
<band height="50">
<textField evaluationTime="Report">
<reportElement positionType="Float" x="420" y="24" width="133" height="25"/>
<textElement/>
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
</textField>
</band>
</pageFooter>


</jasperReport>


God Gave Me Nothing I Wanted<br />He Gave Me Everything I Needed<br /> - Swami Vivekananda
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
Got answer..........


JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "/sample.xls");
exporter.exportReport();
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generate excel reports using jasper report
 
Similar Threads
Unable to export Jasper Report in html format.
PDF with jsf
jasper report- error
Jasper Report Design Problem
Jasper and Eclipse