Search code examples
jasper-reportsexport-to-excel

Fixed header in Excel using JasperReports


How can I create an excel report with fixed header using JapserReports? I mean that I need the header to be fixed when I scroll the Excel file.


Solution

  • This is currently possible by adding a couple properties in the jrxml file for each report. Take a look at Advanced Excel Features for Freeze Panes. If you wanted to freeze everything after your first column header (down the left side basically) it would look like this:

                <staticText>
                    <reportElement style="Sans_Bold" mode="Opaque" x="0" y="60" width="104" height="20" forecolor="#FFFFFF" backcolor="#666666">
                        <property name="net.sf.jasperreports.export.xls.auto.filter" value="Start"/>
                        <property name="net.sf.jasperreports.export.xls.column.width" value="110"/>
                        <property name="net.sf.jasperreports.export.xls.freeze.column.edge" value="Left"/>
                    </reportElement>
                    <box>
                        <leftPen lineWidth="0.5" lineColor="#000000"/>
                        <bottomPen lineWidth="0.5" lineColor="#000000"/>
                    </box>
                    <textElement verticalAlignment="Middle"/>
                    <text><![CDATA[State]]></text>
                </staticText>
    

    or if you wanted to freeze everything below your header you could do something like this below own of your fields:

               <staticText>
                    <reportElement style="Sans_Bold" mode="Opaque" x="316" y="60" width="199" height="20" forecolor="#FFFFFF" backcolor="#666666">
                        <property name="net.sf.jasperreports.export.xls.freeze.row.edge" value="Bottom"/>
                    </reportElement>
                    <box>
                        <leftPen lineWidth="0.5" lineColor="#000000"/>
                        <bottomPen lineWidth="0.5" lineColor="#000000"/>
                        <rightPen lineWidth="0.5" lineColor="#000000"/>
                    </box>
                    <textElement verticalAlignment="Middle"/>
                    <text><![CDATA[Street]]></text>
                </staticText>
    

    If you download the JasperReports zip, there is a demo folder in it, that contains this working example, in the xlsfeatures folder. As a final not, the demo worked for me when exporting to xls, but not when I tried to use xlsx (although there may be a way to make it work, I could not find anything quickly).