Search code examples
javacsvjasper-reports

JRCsvExporter generates csv with blank first line


I have a Java code which generates csv file using JRCsvExporter class. For some reason, it adds blank line in the top. When open in text editor it looks like this:

csv in text editor

And when open in Excel it looks like this:

csv in excel

The issue is due the double quotes the file begins with. When I remove those, it will appear as expected. Yet, I am note sure why those quotes generated, for what reason they good for, and how I removed them safely.

The code is:

reportOutputName = generateReportOutputName(".csv");
JRCsvExporter exporter = new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
PrintStream ps = new PrintStream(new FileOutputStream(reportOutputName), true, "UTF-8");
addBOMToStream(ps);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ps);
exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER, ",");
exporter.setParameter(JRCsvExporterParameter.RECORD_DELIMITER, System.getProperty("line.separator"));
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter.exportReport();

Edit: The relevant part of .jrxml is: How can I make JRCsvExporter ignore element when creating the csv file?

        <title>
            <band height="610"  isSplitAllowed="true" >
                <image  scaleImage="Clip" vAlign="Middle" hAlign="Right" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        x="0"
                        y="5"
                        width="945"
                        height="40"
                        key="image-1"/>
                    <box>                   <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
                    <graphicElement stretchType="NoStretch"/>
                    <imageExpression class="java.lang.String"><![CDATA["etc/report/jasper/img/TopBanner_Report.png"]]></imageExpression>
                </image>
                <textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        mode="Transparent"
                        x="0"
                        y="5"
                        width="945"
                        height="40"
                        forecolor="#FFFFFF"
                        backcolor="#FFFFFF"
                        key="textField-51">
                            <printWhenExpression><![CDATA[new Boolean(!(($P{is_excel}).booleanValue()))]]></printWhenExpression>
                        </reportElement>
                    <box leftPadding="10">                  <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#E8D59C"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#E8D59C"/>
                    <bottomPen lineWidth="0.0" lineColor="#E8D59C"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#E8D59C"/>
</box>
                    <textElement verticalAlignment="Middle">
                        <font fontName="Times-Roman" pdfFontName="etc/report/jasper/fonts/ARIALUNI.TTF" size="14" isBold="false" isPdfEmbedded ="true" pdfEncoding ="Identity-H"/>
                    </textElement>
                <textFieldExpression   class="java.lang.String"><![CDATA[$P{report_name}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        mode="Opaque"
                        x="0"
                        y="60"
                        width="945"
                        height="25"
                        forecolor="#000000"
                        backcolor="#B8CFF2"
                        key="textField-52"
                        isRemoveLineWhenBlank="true">
                            <printWhenExpression><![CDATA[new Boolean(!(($P{is_excel}).booleanValue()))]]></printWhenExpression>
                        </reportElement>
                    <box leftPadding="10">                  <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#B2E66F"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#B2E66F"/>
                    <bottomPen lineWidth="0.0" lineColor="#B2E66F"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#B2E66F"/>
</box>
                    <textElement verticalAlignment="Middle">
                        <font fontName="Arial Unicode MS" pdfFontName="etc/report/jasper/fonts/ARIALUNI.TTF" size="12" isBold="true" isPdfEmbedded ="true" pdfEncoding ="Identity-H"/>
                    </textElement>
                <textFieldExpression   class="java.lang.String"><![CDATA["Report Details"]]></textFieldExpression>
                </textField>
                <rectangle radius="0" >
                    <reportElement
                        mode="Opaque"
                        x="0"
                        y="85"
                        width="945"
                        height="505"
                        forecolor="#535353"
                        backcolor="#ECECEC"
                        key="rectangle-1"
                        stretchType="RelativeToTallestObject"/>
                    <graphicElement stretchType="RelativeToTallestObject">
                    <pen lineWidth="0.0" lineStyle="Solid"/>
</graphicElement>
                </rectangle>
                <textField isStretchWithOverflow="true" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        mode="Opaque"
                        x="0"
                        y="110"
                        width="945"
                        height="25"
                        forecolor="#535353"
                        backcolor="#EBEDEF"
                        key="textField-48"
                        isRemoveLineWhenBlank="true">
                            <printWhenExpression><![CDATA[new Boolean(!(($P{is_excel}).booleanValue()))]]></printWhenExpression>
                        </reportElement>
                    <box leftPadding="10">                  <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#E8D59C"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#C1CDD8"/>
                    <bottomPen lineWidth="0.0" lineColor="#E8D59C"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#C1CDD8"/>
</box>
                    <textElement verticalAlignment="Middle">
                        <font fontName="Arial Unicode MS" pdfFontName="etc/report/jasper/fonts/ARIALUNI.TTF" size="12" isBold="true" isPdfEmbedded ="true" pdfEncoding ="Identity-H"/>
                    </textElement>
                <textFieldExpression   class="java.lang.String"><![CDATA["Generated By: "+$P{report_generatedby}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        mode="Opaque"
                        x="0"
                        y="85"
                        width="945"
                        height="25"
                        forecolor="#535353"
                        backcolor="#EBEDEF"
                        key="textField-46"
                        isRemoveLineWhenBlank="true">
                            <printWhenExpression><![CDATA[new Boolean(!(($P{is_excel}).booleanValue()))]]></printWhenExpression>
                        </reportElement>
                    <box leftPadding="10">                  <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#C1CDD8"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#C1CDD8"/>
                    <bottomPen lineWidth="0.0" lineColor="#E8D59C"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#C1CDD8"/>
</box>
                    <textElement verticalAlignment="Middle">
                        <font fontName="Arial Unicode MS" pdfFontName="etc/report/jasper/fonts/ARIALUNI.TTF" size="12" isBold="true" isPdfEmbedded ="true" pdfEncoding ="Identity-H" isStrikeThrough="false" />
                    </textElement>
                <textFieldExpression   class="java.lang.String"><![CDATA["Hosts: '"+$P{ip_range_text}+"'"]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        mode="Opaque"
                        x="0"
                        y="135"
                        width="945"
                        height="25"
                        forecolor="#535353"
                        backcolor="#EBEDEF"
                        key="textField-49"
                        isRemoveLineWhenBlank="true">
                            <printWhenExpression><![CDATA[new Boolean(!(($P{is_excel}).booleanValue()))]]></printWhenExpression>
                        </reportElement>
                    <box leftPadding="10" bottomPadding="5">                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#E8D59C"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#C1CDD8"/>
                    <bottomPen lineWidth="0.0" lineColor="#E8D59C"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#C1CDD8"/>
</box>
                    <textElement verticalAlignment="Middle">
                        <font fontName="Arial Unicode MS" pdfFontName="etc/report/jasper/fonts/ARIALUNI.TTF" size="12" isBold="true" isPdfEmbedded ="true" pdfEncoding ="Identity-H"/>
                    </textElement>
                <textFieldExpression   class="java.lang.String"><![CDATA["Generated At: "+(new java.util.Date()).toString()]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        mode="Opaque"
                        x="0"
                        y="160"
                        width="945"
                        height="100"
                        forecolor="#535353"
                        backcolor="#EBEDEF"
                        key="textField-47"
                        isRemoveLineWhenBlank="true">
                            <printWhenExpression><![CDATA[new Boolean(!(($P{is_excel}).booleanValue()))]]></printWhenExpression>
                        </reportElement>
                    <box leftPadding="10" rightPadding="10" bottomPadding="5">                  <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#C1CDD8"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#C1CDD8"/>
                    <bottomPen lineWidth="0.0" lineColor="#C1CDD8"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#C1CDD8"/>
</box>
                    <textElement textAlignment="Justified" verticalAlignment="Middle">
                        <font fontName="Arial Unicode MS" pdfFontName="etc/report/jasper/fonts/ARIALUNI.TTF" size="12" isBold="true" isPdfEmbedded ="true" pdfEncoding ="Identity-H"/>
                    </textElement>
                <textFieldExpression   class="java.lang.String"><![CDATA[$P{report_description}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Report" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        mode="Transparent"
                        x="0"
                        y="260"
                        width="945"
                        height="330"
                        forecolor="#535353"
                        backcolor="#EBEDEF"
                        key="notice-text"/>
                    <box leftPadding="10" rightPadding="10" bottomPadding="5"></box>
                    <textElement textAlignment="Justified" verticalAlignment="Middle" markup="html">
                        <font fontName="Arial Unicode MS" pdfFontName="etc/report/jasper/fonts/ARIALUNI.TTF" size="12" isBold="true" isPdfEmbedded ="true" pdfEncoding ="Identity-H"/>
                    </textElement>
                <textFieldExpression   class="java.lang.String"><![CDATA[$P{notice_text}+"<p><span style='color:#D8000C'>"+$V{missingDataWrapper}+"</span></p>"]]></textFieldExpression>
                </textField>
            </band>
        </title>

Solution

  • The issue was due to reportElement "notice-text". The data of its textFieldExpression in many cases can be empty or null, In order to avoid printing it when it's null or empty we shall add: enter image description here

    so it will looks like:

    enter image description here