Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 21st, 2006, 02:19 AM
Authorized User
 
Join Date: Aug 2005
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Exporting data into Excel using jasper reports

Hi I am Using Jasper Reports In my project and i have created .jrxml file and i am able to Export the report in to PDF ( with data) but same code i am not able export in EXCel . The File is getting generated with No data .

Please Guide me Regarding this.

This Is My File (stockroom.jrxml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="DataSourceReport" pageWidth="595" pageHeight="842" columnWidth="515" leftMargin="40" rightMargin="40" topMargin="50" bottomMargin="50">
<style name="Arial_Normal" isDefault="true" fontName="Arial" fontSize="12" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<style name="Arial_Bold" isDefault="false" fontName="Arial" fontSize="12" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Bold" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<style name="Arial_Italic" isDefault="false" fontName="Arial" fontSize="12" isBold="false" isItalic="true" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Oblique" pdfEncoding="Cp1252" isPdfEmbedded="false"/>

<field name="STOCKROOM_ID" class="java.lang.Integer">
</field>
<field name="STOCKROOM_NAME" class="java.lang.String">
</field>
<field name="STOCKROOM_DESC" class="java.lang.String">
</field>

<pageHeader>
<band height="20">

<staticText>
<reportElement mode="Opaque" x="0" y="5" width="55" height="15" forecolor="#ffffff" backcolor="#333333" style="Arial_Bold"/>
<textElement textAlignment="Center"/>
<text><![CDATA[STOCKROOM_ID]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="55" y="5" width="205" height="15" forecolor="#ffffff" backcolor="#333333" style="Arial_Bold"/>
<text><![CDATA[STOCKROOM_NAME]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="260" y="5" width="255" height="15" forecolor="#ffffff" backcolor="#333333" style="Arial_Bold"/>
<text><![CDATA[STOCKROOM_DESC]]></text>
</staticText>

</band>

</pageHeader>
<detail>
<band height="15">
<textField bookmarkLevel="2">
<reportElement x="0" y="0" width="50" height="15"/>
<box leftBorder="Thin" bottomBorder="Thin" leftPadding="10" rightPadding="10"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Integer"><![CDATA[$F{STOCKROOM_ID}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" x="50" y="0" width="200" height="15"/>
<box leftBorder="Thin" bottomBorder="Thin" leftPadding="10" rightPadding="10"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{STOCKROOM_NAME}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" x="250" y="0" width="265" height="15"/>
<box leftBorder="Thin" bottomBorder="Thin" rightBorder="Thin" leftPadding="10" rightPadding="10"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{STOCKROOM_DESC}]]></textFieldExpression>
</textField>




</band>
</detail>
</jasperReport>
---------------------------
And This My Java Jasper Program for PDF:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRResultSetDataSource;
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.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

public class JasperReportDemo
{
public static void main(String[] args) throws SQLException, FileNotFoundException,
IllegalAccessException, ClassNotFoundException,Exception
{

try
{

final String driverClass = "xxxxxxxxxxxxxxxxxx";
final String connectionURL = "xxxxxxxxxxxx ";
final String userID = "xxxxxxxxxxx";
final String userPassword = "zzzzzzzzzzzz";
Connection con = null;
Statement stmt = null;
ResultSet rset = null;
Class.forName(driverClass).newInstance();
con = DriverManager.getConnection(connectionURL, userID, userPassword);
String queryString = "SELECT STOCKROOM_ID, STOCKROOM_NAME, STOCKROOM_DESC "
+ "FROM stockroom ";
stmt = con.createStatement();
rset = stmt.executeQuery(queryString);
InputStream input = new FileInputStream(new File("c:/stockroom.xml"));
JasperDesign design = JRXmlLoader.load(input);
JasperReport report = JasperCompileManager.compileReport(design);
JRResultSetDataSource jasperReports = new JRResultSetDataSource(rset);
JasperPrint print = JasperFillManager.fillReport(report, new HashMap(), jasperReports);
long start = System.currentTimeMillis();

OutputStream output = new FileOutputStream(new File("c:/output/JasperReport.pdf"));
JasperExportManager.exportReportToPdfStream(print, output);

// coding For Excel:


JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JA SPER_PRINT, print);
exporterXLS.setParameter(JRXlsExporterParameter.OU TPUT_STREAM, output);
exporterXLS.setParameter(JRXlsExporterParameter.IS _ONE_PAGE_PER_SHEET, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS _AUTO_DETECT_CELL_TYPE, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS _WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS _REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporterXLS.exportReport();



---------------------------------------------------------------
I want to know why it is generating an empty Excel file without data.
Please help me.



Reply With Quote
  #2 (permalink)  
Old April 6th, 2006, 06:03 AM
Registered User
 
Join Date: Apr 2006
Location: hyd, ap, India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi even i am facing with the same problem
i am able to export my jasperReport to PDF.
but when i am exporting it to .xls format
i am only getting textexpressions, static texts i used specifically.

but i am not able to get the imageExpressions.

has any one worked on it and if so what is the solution.

Bala Raghavendra Prasad
Reply With Quote
  #3 (permalink)  
Old July 14th, 2006, 12:08 PM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the modified code snippet that you can use.

        JasperPrint print = JasperManager.fillReport(input, new HashMap(), jasperReports);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        OutputStream outputfile= new FileOutputStream(new File("c:/output/JasperReport.xls"));

        // coding For Excel:
         JRXlsExporter exporterXLS = new JRXlsExporter();
         exporterXLS.setParameter(JRXlsExporterParameter.JA SPER_PRINT, print);
         exporterXLS.setParameter(JRXlsExporterParameter.OU TPUT_STREAM, outputByteArray);
         exporterXLS.setParameter(JRXlsExporterParameter.IS _ONE_PAGE_PER_SHEET, Boolean.FALSE);
         exporterXLS.setParameter(JRXlsExporterParameter.IS _AUTO_DETECT_CELL_TYPE, Boolean.TRUE);
         exporterXLS.setParameter(JRXlsExporterParameter.IS _WHITE_PAGE_BACKGROUND, Boolean.FALSE);
         exporterXLS.setParameter(JRXlsExporterParameter.IS _REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
         exporterXLS.exportReport();
         outputfile.write(outputByteArray.toByteArray());

Thanks,
Anveshini


Reply With Quote
  #4 (permalink)  
Old October 17th, 2006, 11:32 PM
Registered User
 
Join Date: Aug 2006
Location: Mangalore, , India.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you anveshini...
      you method helped me to get report in Excel format, only one thing have to change in that

exporterXLS.setParameter(JRXlsExporterParameter.IS _AUTO_DETECT_CELL_TYPE, Boolean.TRUE);

it has to be depricated , its better if we use

exporterXLS.setParameter(JRXlsExporterParameter.IS _DETECT_CELL_TYPE, Boolean.TRUE);


Thank you
Manu D G

Reply With Quote
  #5 (permalink)  
Old November 24th, 2006, 07:20 AM
Authorized User
 
Join Date: Sep 2006
Location: , , .
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

its great yar ur trail is good

Regards
Kotaiah.K

Reply With Quote
  #6 (permalink)  
Old December 29th, 2006, 04:42 AM
Registered User
 
Join Date: May 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks anveshini..I am able to generate reports in Excel format ...
But I have two issues with display
1)Managing new line characters in an Excel cell
2)Setting the row width

Do you have any idea how to solve this?
Reply With Quote
  #7 (permalink)  
Old May 9th, 2007, 07:30 AM
Registered User
 
Join Date: May 2007
Location: bangalore, , India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by babuchelat
 Thanks anveshini..I am able to generate reports in Excel format ...
But I have two issues with display
1)Managing new line characters in an Excel cell
2)Setting the row width

Do you have any idea how to solve this?
Hi,

Did you able to solve the above issues.?
If so, can you please let me know the solution.

Sunil

Reply With Quote
  #8 (permalink)  
Old February 4th, 2008, 04:26 AM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HI.. I am using Jasper Reports in my project. I am able to get PDF format but when I try to get output in xls form useing JRXlsExporter i am facing some problem thats given below, my code is just below of problem :-

java.lang.reflect.InvocationTargetException

Caused by: java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFCellStyle
    at com.app.webapp.action.ReportAction.viewReportWithP aram(ReportAction.java:74)


public String viewReportWithParam() throws Exception {
        byte[] output;
        Map parameters = buildParameters();
        JasperPrint jasperPrint = reportsManager.generateReport(id, parameters);
        HttpServletResponse response = getResponse();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        JRXlsExporter exporterXLS = new JRXlsExporter();
        exporterXLS.setParameter(JRXlsExporterParameter.JA SPER_PRINT, jasperPrint);
        exporterXLS.setParameter(JRXlsExporterParameter.IS _DETECT_CELL_TYPE, Boolean.TRUE);
        exporterXLS.setParameter(JRXlsExporterParameter.IS _WHITE_PAGE_BACKGROUND, Boolean.FALSE);
        exporterXLS.setParameter(JRXlsExporterParameter.OU TPUT_STREAM, byteArrayOutputStream);
        exporterXLS.exportReport();

        response.setContentType("application/vnd.ms-excel");
        ServletOutputStream ouputStream = response.getOutputStream();
        ouputStream.write(byteArrayOutputStream.toByteArra y());
        ouputStream.flush();
        ouputStream.close();
        return null;

    }

Plz Help its Urgent.

Thanks in advance.



<s></s>
Reply With Quote
  #9 (permalink)  
Old February 25th, 2009, 04:17 AM
Registered User
 
Join Date: Feb 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi.....
---------------------------------------------------------------
exporterXLS.setParameter(JRExporterParameter.JASPE R_PRINT, print);
exporterXLS.setParameter(JRExporterParameter.OUTPU T_STREAM, output);


I HAVE SEND SOLUTION FOR YOUR QUERY...
JUST TRY, ITS WORK 100%..
U DIDN'T GET BLANK DATA INTO XLS FILE..

REGARDS,
SACHIN
Reply With Quote
  #10 (permalink)  
Old March 21st, 2009, 07:24 AM
Registered User
 
Join Date: Mar 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I found the following code snippet useful in generating the excel sheet...
Here is the modified code snippet that you can use.

JasperPrint print = JasperManager.fillReport(input, new HashMap(), jasperReports);
ByteArrayOutputStream output = new ByteArrayOutputStream();
OutputStream outputfile= new FileOutputStream(new File("c:/output/JasperReport.xls"));

// coding For Excel:
JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JA SPER_PRINT, print);
exporterXLS.setParameter(JRXlsExporterParameter.OU TPUT_STREAM, outputByteArray);
exporterXLS.setParameter(JRXlsExporterParameter.IS _ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS _AUTO_DETECT_CELL_TYPE, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS _WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS _REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporterXLS.exportReport();
outputfile.write(outputByteArray.toByteArray());

However, I want to generate multiple sheets in a single excel file. Please Help....Its very urgent. Thanks in advance
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exporting data to Excel lrickett Beginning VB 6 3 February 10th, 2011 12:27 AM
Reports in excel using jasper mohanasrini J2EE 1 July 1st, 2009 11:15 AM
help exporting data to excel MarkGT Classic ASP Basics 5 April 29th, 2008 06:13 PM
Crystal Reports not exporting to Word & Excel anand7642 Crystal Reports 1 October 28th, 2006 09:31 AM
Exporting into Excel format using jasper reports raj_plkd Classic ASP Basics 1 February 14th, 2006 06:50 PM



All times are GMT -4. The time now is 12:13 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.