Hi friends,
Way of exporting jasper Reports into .pdf, .xls and .txt format with "Save As" dialog to download at client side
my .jrxml file is like this.( ListOfItems.jrxml)
<?xml version="1.0" encoding="UTF-8"?>
<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="null" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<queryString language="SQL">
<![CDATA[select inventory_id,
item_no,
(select item_name from bp_items where item_no= bp_inventory.item_no) as item_name,
quantity,
shelf_time,
optimum_shelf_time
from
bp_inventory
order by item_name]]>
</queryString>
<field name="inventory_id" class="java.lang.Integer">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="item_no" class="java.lang.Integer">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="item_name" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="quantity" class="java.math.BigDecimal">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="shelf_time" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="optimum_shelf_time" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<background>
<band/>
</background>
<title>
<band height="55">
<staticText>
<reportElement x="65" y="13" width="424" height="35" backcolor="#0000FF"/>
<textElement textAlignment="Center">
<font size="16" isBold="true"/>
</textElement>
<text><![CDATA[Items Report]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band/>
</pageHeader>
<columnHeader>
<band height="18">
<staticText>
<reportElement mode="Opaque" x="0" y="0" width="79" height="18" forecolor="#FFFFFF" backcolor="#4661C2"/>
<textElement textAlignment="Center">
<font fontName="verdana" size="12" isBold="true"/>
</textElement>
<text><![CDATA[Inventory No]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="79" y="0" width="68" height="18" forecolor="#FFFFFF" backcolor="#4661C2"/>
<textElement textAlignment="Center">
<font fontName="verdana" size="12" isBold="true"/>
</textElement>
<text><![CDATA[Item No]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="147" y="0" width="79" height="18" forecolor="#FFFFFF" backcolor="#4661C2"/>
<textElement textAlignment="Center">
<font fontName="verdana" size="12" isBold="true"/>
</textElement>
<text><![CDATA[Item Name]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="226" y="0" width="77" height="18" forecolor="#FFFFFF" backcolor="#4661C2"/>
<textElement textAlignment="Center">
<font fontName="verdana" size="12" isBold="true"/>
</textElement>
<text><![CDATA[Quantity]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="303" y="0" width="103" height="18" forecolor="#FFFFFF" backcolor="#4661C2"/>
<textElement textAlignment="Center">
<font fontName="verdana" size="12" isBold="true"/>
</textElement>
<text><![CDATA[Shelf Time]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="406" y="0" width="147" height="18" forecolor="#FFFFFF" backcolor="#4661C2"/>
<textElement>
<font fontName="verdana" size="12" isBold="true"/>
</textElement>
<text><![CDATA[OptimumShelfTime]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20">
<textField>
<reportElement x="0" y="0" width="79" height="20"/>
<textElement textAlignment="Center">
<font fontName="Verdana" size="12"/>
</textElement>
<textFieldExpression class="java.lang.Integer"><![CDATA[$F{inventory_id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="79" y="0" width="68" height="20"/>
<textElement textAlignment="Center">
<font fontName="Verdana" size="12"/>
</textElement>
<textFieldExpression class="java.lang.Integer"><![CDATA[$F{item_no}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="147" y="0" width="79" height="20"/>
<textElement textAlignment="Center">
<font fontName="Verdana" size="12"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{item_name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="226" y="0" width="77" height="20"/>
<textElement textAlignment="Center">
<font fontName="Verdana" size="12"/>
</textElement>
<textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{quantity}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="303" y="0" width="103" height="20"/>
<textElement textAlignment="Center">
<font fontName="Verdana" size="12"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{shelf_time}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="406" y="0" width="147" height="20"/>
<textElement textAlignment="Center">
<font fontName="Verdana" size="12"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{optimum_shelf_time}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band/>
</columnFooter>
<pageFooter>
<band height="54">
<textField>
<reportElement x="410" y="15" width="80" height="20"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.String"><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="490" y="15" width="40" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
<summary>
<band/>
</summary>
</jasperReport>
Code in Action class
//filename is the .jrxml file ( ListOfItems.jrxml)
String filename=request.getParameter("file");
// title is the title of the report.Here we are passing dynamically.So that this class is useful to remaining reports also.
String title=request.getParameter("title");
Connection con = MMSDB.getConnection();
String jrept = filename;
String reportFileName = JasperCompileManager.compileReportToFile(request.getRealPath("/reports") + "/" + jrept);
java.util.Map parameters = new java.util.HashMap();
parameters.put("ReportTitle", title);
File reportFile = new File(reportFileName);
if (!reportFile.exists()) {
throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first.");
}
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFileName, parameters, con);
//Maintaining request.----
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
HashMap fontMap = new HashMap();
exporter.setParameter(JRHtmlExporterParameter.FONT_MAP, fontMap);
exporter.setParameter(JRHtmlExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.IS_WRAP_BREAK_WORD, Boolean.TRUE);
request.setAttribute("exportObject", exporter);
if (!reportFile.exists()) {
throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first.");
}
if (fileType.equals("PDF"))
{
response.setContentType("application/pdf");
JasperExportManager.exportReportToPdfFile(jasperPrint, request.getRealPath("/reports") + "/" +title+".pdf");
File f=new File(request.getRealPath("/reports") + "/" +title+".pdf");
FileInputStream fin = new FileInputStream(f);
ServletOutputStream outStream = response.getOutputStream();
// SET THE MIME TYPE.
response.setContentType("application/pdf");
// set content dispostion to attachment in with file name.
// case the open/save dialog needs to appear.
response.setHeader("Content-Disposition", "attachment;filename='"+title+"'.pdf");
byte[] buffer = new byte[1024];
int n = 0;
while ((n = fin.read(buffer)) != -1) {
outStream.write(buffer, 0, n);
System.out.println(buffer);
}
outStream.flush();
fin.close();
outStream.close();
}
else
if (fileType.equals("EXCEL"))
{
JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_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.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, request.getRealPath("/reports") + "/" +title+".xls");
exporterXLS.exportReport();
File f=new File(request.getRealPath("/reports") + "/" +title+".xls");
FileInputStream fin = new FileInputStream(f);
ServletOutputStream outStream = response.getOutputStream();
// SET THE MIME TYPE.
response.setContentType("application/vnd.ms-excel");
// set content dispostion to attachment in with file name.
// case the open/save dialog needs to appear.
response.setHeader("Content-Disposition", "attachment;filename="+title+".xls");
byte[] buffer = new byte[1024];
int n = 0;
while ((n = fin.read(buffer)) != -1) {
outStream.write(buffer, 0, n);
System.out.println(buffer);
}
outStream.flush();
fin.close();
outStream.close();
}
else
if (fileType.equals("TEXT"))
{
JRTextExporter exporterTxt = new JRTextExporter();
exporterTxt.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporterTxt.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, request.getRealPath("/reports") + "/" +title+".txt");
exporterTxt.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, new Integer(7));
exporterTxt.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, new Integer(11));
exporterTxt.exportReport();
File f=new File(request.getRealPath("/reports") + "/" + title+".txt");
FileInputStream fin = new FileInputStream(f);
ServletOutputStream outStream = response.getOutputStream();
// SET THE MIME TYPE.
response.setContentType("application/text");
// set content dispostion to attachment in with file name.
// case the open/save dialog needs to appear.
response.setHeader("Content-Disposition", "attachment;filename="+title+".txt");
byte[] buffer = new byte[1024];
int n = 0;
while ((n = fin.read(buffer)) != -1) {
outStream.write(buffer, 0, n);
System.out.println(buffer);
}
outStream.flush();
fin.close();
outStream.close();