Jump to content
Changes to the Jaspersoft community edition download ×

Excel export is showing garbage via a web app


2004 IR Help

Recommended Posts

By: evgenykravchenko - ekravche

Excel export is showing garbage via a web app

2005-07-22 15:26

Hello,

 

I'm trying to export to Excel, but the result is a spreadsheet that shows binary data.

 

<--------------------- begin code ---------------------------->

 

 

JasperPrint jp = JasperFillManager.fillReport(jr, null,ds1);

byte[] output = null;

try{

response.setContentType("application/vnd.ms-excel");

//response.set

response.setHeader("Content-Disposition", "inline; filename=report.xls");

 

JRXlsExporter jasperXlsExportMgr = new JRXlsExporter();

 

ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();

jasperXlsExportMgr.setParameter(JRExporterParameter.JASPER_PRINT, jp);

jasperXlsExportMgr.setParameter(JRXlsExporterParameter.CHARACTER_ENCODING, Locale.CANADA);

jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);

jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_AUTO_DETECT_CELL_TYPE, java.lang.Boolean.TRUE);

jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, java.lang.Boolean.FALSE);

jasperXlsExportMgr.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport);

jasperXlsExportMgr.exportReport();

 

byte[] output1= null;

output1 = xlsReport.toByteArray();

 

ServletOutputStream outputStream;

outputStream = response.getOutputStream();

outputStream.write(output1, 0, output1.length);

outputStream.flush();

outputStream.close();

 

//response.setContentLength(output.length+6);

//response.setBufferSize(output.length+1000);

 

 

 

//outputStream.flush();

//outputStream.write(output);

//outputStream.flush();

//outputStream.close();

}catch (JRException jex){

// throw new Exception ("Unable to create the XLS file" + jex);

jex.printStackTrace();

}

<--------------------- end code ---------------------------->

 

 

<--------------------- report begin --------------------------->

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

 

<jasperReport name="SmallReport" pageWidth="595" pageHeight="842" columnWidth="540" columnSpacing="15" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30">

<reportFont name="ReportFont0" isDefault="false" fontName="sansserif" size="10" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>

<field name="Name" class="java.lang.String">

</field>

<title>

<band height="50">

<staticText>

<reportElement positionType="Float" x="31" y="7" width="492" height="36"/>

<textElement textAlignment="Center">

<font fontName="sansserif" size="18" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>

</textElement>

<text><![CDATA[simple Report]]></text>

</staticText>

</band>

</title>

<columnHeader>

<band height="20">

<staticText>

<reportElement positionType="Float" x="4" y="2" width="102" height="13"/>

<textElement>

<font isUnderline="true"/>

</textElement>

<text><![CDATA[NAME]]></text>

</staticText>

</band>

</columnHeader>

<detail>

<band height="100">

<textField isStretchWithOverflow="true">

<reportElement positionType="Float" x="4" y="3" width="200" height="12"/>

<textElement>

<font fontName="sansserif" size="10" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$F{Name}]]></textFieldExpression>

</textField>

<image>

<reportElement positionType="Float" x="356" y="0" width="192" height="99"/>

<graphicElement/>

<imageExpression class="java.lang.String"><![CDATA["C:\samples\openreports\reportsamples\horses.gif"]]></imageExpression>

</image>

</band>

</detail>

<pageFooter>

<band height="40">

<line>

<reportElement x="0" y="10" width="515" height="1"/>

<graphicElement/>

</line>

<textField>

<reportElement x="176" y="20" width="80" height="14"/>

<textElement textAlignment="Right"/>

<textFieldExpression class="java.lang.String"><![CDATA["Page " + String.valueOf($V{PAGE_NUMBER}) + " of"]]></textFieldExpression>

</textField>

<textField evaluationTime="Report">

<reportElement x="256" y="19" width="74" height="14"/>

<textElement>

<font fontName="sansserif" size="10" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[" " + String.valueOf($V{PAGE_COUNT})]]></textFieldExpression>

</textField>

</band>

</pageFooter>

</jasperReport>

 

 

<--------------------- report end --------------------------->

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...