Java – JasperReports export to excel auto size columns

jasper-reportsjava

I have a problems with JRXlsExporter. Autofit properties not work correctly.
Here is my 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="templ" language="groovy" pageWidth="1100" pageHeight="1400" columnWidth="1060" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isSummaryNewPage="true" isSummaryWithPageHeaderAndFooter="true" uuid="683fcccd-cd5a-4e1d-a085-b49c54c13fff">
    <property name="ireport.zoom" value="1.2100000000000029"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <property name="net.sf.jasperreports.export.xls.detect.cell.type" value="true"/>
    <property name="net.sf.jasperreports.print.keep.full.text" value="true"/>
    <property name="net.sf.jasperreports.export.xls.wrap.text" value="false"/>
    <property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
    <property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
    <queryString language="SQL">
        <![CDATA[]]>
    </queryString>
</jasperReport>

Java Code:

excelExporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
excelExporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, outputStream);
excelExporter.setParameter(JRXlsExporterParameter.MAXIMUM_ROWS_PER_SHEET, 65536);
excelExporter.setParameter(JRXlsExporterParameter.IS_IGNORE_CELL_BORDER, Boolean.FALSE);
excelExporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
excelExporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
excelExporter.setParameter(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN, Boolean.TRUE);
excelExporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE);
excelExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
excelExporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
excelExporter.setParameter(JRXlsExporterParameter.IS_IMAGE_BORDER_FIX_ENABLED, Boolean.TRUE);
excelExporter.setParameter(JRXlsExporterParameter.IS_IGNORE_GRAPHICS, Boolean.FALSE);

and output result:
resultFile.

For example first column must be "Customer Number" instead of "Custome"

Best Answer

The following properties do not work on Report Level:

<property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
<property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>

You have to put them on Report Elements (i.e.):

        <staticText>
            <reportElement x="0" y="100" width="100" height="20">
                <property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
                <property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
            </reportElement>
            <textElement/>
            <text><![CDATA[Your Static Text]]></text>
        </staticText>

Also I'd recommend you to try using net.sf.jasperreports.export.xls.column.width property, which is more useful. Reference: http://jasperreports.sourceforge.net/config.reference.html