Tài liệu JasperReports 3.5 for Java Developers- P6 - Pdf 87

Chapter 8
[
239
]
Clicking on the nodes will direct the main window to the appropriate anchor.
Handling very large reports
Sometimes, when lling a report, the report datasource may have a lot of data. In
some cases, the generated report can become very large, and in some cases larger
than the memory allocated for the JVM, causing an
OutOfMemoryException
.
It is possible to set up JasperReports so that it stores segments of a report on the
disk in order to free some memory. This can be accomplished by using a built-in
report parameter
REPORT_VIRTUALIZER
. The value for this parameter must be an
instance of a class implementing
net.sf.jasperreports.engine.JRVirtualizer
.
JasperReports comes with an implementation of this interface, namely
net.sf.
jasperreports.engine.fill.JRFileVirtualizer
. This implementation is
sufcient to handle the vast majority of the large reports. If, for some reason,
this implementation is not sufcient for our needs, we can always create our own
implementation of
net.sf.jasperreports.engine.JRVirtualizer
. The following
example illustrates typical usage of
JRVirtualizer
:

"jdbc:mysql://localhost:3306/flightstats?" +
"user=user&password=secret");
System.out.println("Filling report...");
JasperFillManager.fillReportToFile(reportDirectory + "/"
+ reportName + ".jasper",
parameterMap,connection);
System.out.println("Done!");
connection.close();
}
catch (JRException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new DbConnectionReportFill().generateReport(args[0]);
}
}
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 8

to our reports by taking advantage of the
<crosstab>
JRXML element and display
related charts or crosstabs for each record in a report by using subdatasets. To ease
the task of report navigation, we learned how to add hyperlinks, anchors, and
bookmarks to our reports. We have also seen how we can safely generate reports
larger than the available memory by taking advantage of report virtualization.
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Exporting to Other Formats
Reports can be exported to several formats. Because reports in native JasperReports
format can be viewed only by using the JasperReports API (or by using the
JasperViewer utility included with JasperReports), exporting reports is a common
requirement. Exported reports can be viewed with readily available software like PDF
viewers, word processors, and web browsers. In this chapter, we will learn how to
export our reports to all of the formats supported by JasperReports.
Topics covered in this chapter include:
• Exporting reports to PDF
• Exporting reports to RTF
• Exporting reports to ODT
• Exporting reports to Excel
• Exporting reports to HTML
• Exporting reports to XML
• Exporting reports to CSV
• Exporting reports to plain text
• Directing exported reports to a browser
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

method is used to set the parameters needed to export the
report. In most cases, two parameters need to be set: the name of the output le
or output stream used to output the exported report and the JasperPrint object
containing the native report. We would set the output le any time we are sure
we want to save the exported report to the disk. We would set the output stream
parameter to send the exported report through the network or when we are not sure
if we want to save the exported report to the disk or stream it through the network.
As an output stream can be easily saved to the disk or streamed through the
network, the decision can be made at the runtime.
As can be seen in the signature of the
setParameter()
method, it takes an instance
of
net.sf.jasperreports.engine.JRExporterParameter
as its rst argument.
JRExporterParameter
contains a number of static constants that are typically used
as the rst argument to the
setParameter()
method. To accommodate the most
common cases, the
JRExporterParameter
constants of interest are:

JRExporterParameter.JASPER_PRINT
: This is used to set the
JasperPrint object to export.

JRExporterParameter.OUTPUT_FILE_NAME
: This is used to set

import java.io.File;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
public class PdfExportDemo
{
public static final String REPORT_DIRECTORY = "reports";
public void pdfExport(String reportName)
{
File file = new File(REPORT_DIRECTORY + "/" + reportName +
".jrprint");
try
{
JasperPrint jasperPrint = (JasperPrint)
JRLoader.loadObject(file);
JRPdfExporter pdfExporter = new JRPdfExporter();
pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT,
jasperPrint);
pdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
REPORT_DIRECTORY + "/" + reportName + ".pdf");
System.out.println("Exporting report...");
pdfExporter.exportReport();
System.out.println("Done!");
}
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Exporting to Other Formats
[

247
]
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Exporting to Other Formats
[
248
]
Exporting to RTF
Rich Text Format (RTF) is a document le format that is supported by most word
processors. Exporting to RTF allows our documents to be read by Microsoft Word
and several other word processors.
Unfortunately, RTF documents generated by JasperReports
are not always readable by OpenOfce.org or StarOfce writer
because these ofce suites are not fully compliant with the RTF
specication. As we'll see in the next section, JasperReports can
export to OpenDocument Text, the native format for both of these
ofce suites.
The following example illustrates how to export a report into RTF format:
package net.ensode.jasperbook;
import java.io.File;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
public class RtfExportDemo
{
public static final String REPORT_DIRECTORY = "reports";
public void rtfExport(String reportName)

}
}
As we can see in this example,
net.sf.jasperreports.engine.export.
JRRtfExporter
is the
JRExporter
implementation we need to use to export to RTF.
Like the previous example, we tell the exporter what report to export by supplying
an instance of
net.sf.jasperreports.engine.JasperPrint
as the value for the
JRExporterParameter.JASPER_PRINT
parameter, and we set the output le to be
the report name by setting the
JRExporterParameter.OUTPUT_FILE_NAME
with the
appropriate value.
This example code will generate an RTF document as shown in the following
screenshot:
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Exporting to Other Formats
[
250
]
Exporting to ODT
OpenDocument Text (ODT) is the word processing standard for Organization for the
Advancement of Structured Information Standards (OASIS) and the native format of
several open source word processing tools, most notably OpenOfce.org Writer.

}
catch (JRException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new OdtExportDemo().odtExport(args[0]);
}
}
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 9
[
251
]
As we can see, exporting to ODT is not much different from exporting to other
formats. The
JRExporter
implementation that we need to use in this case is
net.sf.jasperreports.engine.export.oasis.JROdtExporter
. Note that in
the previous examples, we have specied what report to export by supplying an
instance of
net.sf.jasperreports.engine.JasperPrint
as the value for the
JRExporterParameter.JASPER_PRINT
parameter. We then set the output le to
be the report name by setting

{
File file = new File(REPORT_DIRECTORY + "/" + reportName +
".jrprint");
try
{
JasperPrint jasperPrint = (JasperPrint)
JRLoader.loadObject(file);
JExcelApiExporter xlsExporter = new JExcelApiExporter();
xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT,
jasperPrint);
xlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
REPORT_DIRECTORY + "/" + reportName + ".xls");
System.out.println("Exporting report...");
xlsExporter.exportReport();
System.out.println("Done!");
}
catch (JRException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new XlsExportDemo().xlsExport(args[0]);
}
}
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 9
[

import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.util.JRLoader;
public class HtmlExportDemo
{
public static final String REPORT_DIRECTORY = "reports";
public void htmlExport(String reportName)
{
File file = new File(REPORT_DIRECTORY + "/" + reportName +
".jrprint");
try
{
JasperPrint jasperPrint = (JasperPrint)
JRLoader.loadObject(file);
JRHtmlExporter htmlExporter = new JRHtmlExporter();
htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT,
jasperPrint);
htmlExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
REPORT_DIRECTORY + "/" + reportName + ".html");
System.out.println("Exporting report...");
htmlExporter.exportReport();
System.out.println("Done!");
}
catch (JRException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)

Exporting to XML
JasperReports uses a Document Type Denition (DTD) le to generate XML
reports. XML reports can be exported back to the compiled reports by using the
net.sf.jasperreports.engine.xml.JRPrintXmlLoader
class. The following
example demonstrates how to export a report to XML:
package net.ensode.jasperbook;
import java.io.File;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRXmlExporter;
import net.sf.jasperreports.engine.util.JRLoader;
public class XmlExportDemo
{
public static final String REPORT_DIRECTORY = "reports";
public void xmlExport(String reportName)
{
File file = new File(REPORT_DIRECTORY + "/" + reportName +
".jrprint");
try
{
JasperPrint jasperPrint = (JasperPrint)
JRLoader.loadObject(file);
JRXmlExporter xmlExporter = new JRXmlExporter();
xmlExporter.setParameter(JRExporterParameter.JASPER_PRINT,
jasperPrint);
xmlExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
REPORT_DIRECTORY + "/" + reportName + ".jrpxml");
System.out.println("Exporting report...");

instead of xml.
The following is a partial listing of the generated XML le:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperPrint PUBLIC "-//JasperReports//DTD Report Design//EN"
" /><jasperPrint name="DatasetDemoReport" pageWidth="595"
pageHeight="842">
<page>
<text textHeight="13.578125" lineSpacingFactor="1.3578125"
leadingOffset="-3.1972656">
<reportElement x="5" y="40" width="500" height="20"/>
<textContent>
<![CDATA[Aircraft registered in MD]]>
</textContent>
</text>
<rectangle radius="0">
<reportElement mode="Opaque" x="5" y="60" width="782"
height="80" forecolor="#000000"/>
<graphicElement pen="None" fill="Solid"/>
</rectangle>
<frame>
<reportElement x="105" y="80" width="100" height="20"
backcolor="#FFFFFF"/>
<box border="Thin" borderColor="#000000"/>
<text textAlignment="Left"
verticalAlignment="Bottom"
textHeight="13.578125"
lineSpacingFactor="1.3578125"
leadingOffset="-3.1972656">
<reportElement x="5" y="0" width="55" height="20"/>
<textContent><![CDATA[1]]></textContent>

import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.util.JRLoader;
public class CsvExportDemo
{
public static final String REPORT_DIRECTORY = "reports";
public void csvExport(String reportName)
{
File file = new File(REPORT_DIRECTORY + "/" + reportName +
".jrprint");
try
{
JasperPrint jasperPrint = (JasperPrint)
JRLoader.loadObject(file);
JRCsvExporter csvExporter = new JRCsvExporter();
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status