Search code examples
jasper-reportsexport-to-excel

How do I prevent text overflowing into the next cell in a Jasper Report that exports to Excel?


I am creating a jrxml in Jaspersoft Studio that exports a table to Excel. When I view the Excel output, the data in the last column is overflowing into the next cell like so:

enter image description here

I am trying to prevent the data from overflowing into the next cell without wrapping the data in the cell.

I was able to find a reference for why Excel does overflow text: https://www.ablebits.com/office-addins-blog/stop-text-spilling-over-excel/

One of the solutions is to manually Format Cells and change horizontal text alignment to 'Fill'. This gives me the following table, which is exactly what I need:

enter image description here

However, I was not able to find a way to configure my jrxml to obtain this behavior. How may I create my jrxml such that it will export to Excel without overflowing text into adjacent columns (without wrapping the text)?


Solution

  • I don't think text alignment 'Fill' can be set when exporting to XLS.

    One (kinda dirty) solution is having a 'blank' cell next to COL2 which will prevent the text in COL2 overflowing into the 'blank' cell.

    Your example (modified):

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Created with Jaspersoft Studio version 6.20.0.final using JasperReports Library version 6.1.1  -->
    <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="Blank_A4" pageWidth="240" pageHeight="400" columnWidth="200" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="af068097-e37b-4e41-9083-4c7b8e13c800">
        <property name="com.jaspersoft.studio.unit." value="pixel"/>
        <property name="net.sf.jasperreports.export.xls.wrap.text" value="false"/>
        <property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
        <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
        <subDataset name="Dataset1" uuid="f12cc232-2035-43e1-b3e3-a446ee7318bf">
            <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
            <queryString>
                <![CDATA[select 'abcdefghijklmnopqrstuvwxyz' as data1,
    'abcdefghijklmnopqrstuvwxyz' as data2]]>
            </queryString>
            <field name="data1" class="java.lang.String"/>
            <field name="data2" class="java.lang.String"/>
        </subDataset>
        <queryString>
            <![CDATA[select 1]]>
        </queryString>
        <detail>
            <band height="359" splitType="Stretch">
                <componentElement>
                    <reportElement x="0" y="0" width="220" height="60" uuid="6a7376a7-6ee6-4e94-8e7b-024170505320">
                        <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                    </reportElement>
                    <c:table xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                        <datasetRun subDataset="Dataset1" uuid="0fee875c-6568-4a06-a8e4-f7fb34ebd4e5"/>
                        <c:column width="100" uuid="6517d845-8cbe-44b6-a644-cedfc40576c9">
                            <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
                            <c:columnHeader height="30" rowSpan="1">
                                <staticText>
                                    <reportElement mode="Opaque" x="0" y="0" width="100" height="30" forecolor="#FFFFFF" backcolor="#7845D9" uuid="f222fdba-a6db-46dd-97a5-e8e0181aa7e4">
                                        <propertyExpression name="net.sf.jasperreports.export.xls.wrap.text"><![CDATA["true"]]></propertyExpression>
                                    </reportElement>
                                    <box>
                                        <pen lineWidth="0.5" lineColor="#000000"/>
                                        <bottomPen lineWidth="1.5"/>
                                    </box>
                                    <textElement textAlignment="Center" verticalAlignment="Middle">
                                        <font isBold="true"/>
                                    </textElement>
                                    <text><![CDATA[COL1]]></text>
                                </staticText>
                            </c:columnHeader>
                            <c:detailCell height="30">
                                <textField isBlankWhenNull="true">
                                    <reportElement mode="Opaque" x="0" y="0" width="100" height="30" uuid="2a235121-a4f3-487b-8138-edc5b5340e50"/>
                                    <box>
                                        <pen lineWidth="0.5"/>
                                    </box>
                                    <textElement verticalAlignment="Middle">
                                        <paragraph leftIndent="3"/>
                                    </textElement>
                                    <textFieldExpression><![CDATA[$F{data1}]]></textFieldExpression>
                                </textField>
                            </c:detailCell>
                        </c:column>
                        <c:column width="100" uuid="23415ab5-ef42-4f13-9395-cc9bb019149a">
                            <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
                            <c:columnHeader height="30" rowSpan="1">
                                <staticText>
                                    <reportElement mode="Opaque" x="0" y="0" width="100" height="30" forecolor="#FFFFFF" backcolor="#7845D9" uuid="8ecbe731-1442-40b4-94a6-f3c57337704c">
                                        <propertyExpression name="net.sf.jasperreports.export.xls.wrap.text"><![CDATA["true"]]></propertyExpression>
                                    </reportElement>
                                    <box>
                                        <pen lineWidth="0.5" lineColor="#000000"/>
                                        <bottomPen lineWidth="1.5"/>
                                    </box>
                                    <textElement textAlignment="Center" verticalAlignment="Middle">
                                        <font isBold="true"/>
                                    </textElement>
                                    <text><![CDATA[COL2]]></text>
                                </staticText>
                            </c:columnHeader>
                            <c:detailCell height="30">
                                <textField isBlankWhenNull="true">
                                    <reportElement mode="Opaque" x="0" y="0" width="100" height="30" uuid="c8f16b37-4f63-4043-abc6-d321125c247e"/>
                                    <box>
                                        <pen lineWidth="0.5"/>
                                    </box>
                                    <textElement verticalAlignment="Middle">
                                        <paragraph leftIndent="3"/>
                                    </textElement>
                                    <textFieldExpression><![CDATA[$F{data2}]]></textFieldExpression>
                                </textField>
                            </c:detailCell>
                        </c:column>
                        <c:column width="20" uuid="4fff907e-9cb8-41d3-a264-b78ca5ed814a">
                            <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/>
                            <c:columnHeader height="30" rowSpan="1"/>
                            <c:detailCell height="30">
                                <property name="com.jaspersoft.studio.unit.width" value="px"/>
                                <textField>
                                    <reportElement x="0" y="0" width="20" height="30" uuid="eb0dd902-0635-4a55-8f84-03834107f2bf"/>
                                    <textFieldExpression><![CDATA[""]]></textFieldExpression>
                                </textField>
                            </c:detailCell>
                        </c:column>
                    </c:table>
                </componentElement>
            </band>
        </detail>
    </jasperReport>