JRCsvExporter 生成第一行空白的 csv
JRCsvExporter generates csv with blank first line
我有一个 Java 代码,它使用 JRCsvExporter class 生成 csv 文件。出于某种原因,它在顶部添加了空行。在文本编辑器中打开时,它看起来像这样:
在 Excel 中打开时,它看起来像这样:
问题是由于文件开头的双引号引起的。当我删除它们时,它会按预期显示。然而,我清楚地知道为什么会产生这些引号,它们有什么用处,以及我是如何安全地删除它们的。
密码是:
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();
编辑:
.jrxml 的相关部分是:
如何让 JRCsvExporter 在创建 csv 文件时忽略元素?
<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>
问题是由于 reportElement“notice-text”。
它的textFieldExpression的数据在很多情况下可以为空或者为null,为了避免在为null或者为空的时候打印出来我们要加上:
所以它看起来像:
我有一个 Java 代码,它使用 JRCsvExporter class 生成 csv 文件。出于某种原因,它在顶部添加了空行。在文本编辑器中打开时,它看起来像这样:
在 Excel 中打开时,它看起来像这样:
问题是由于文件开头的双引号引起的。当我删除它们时,它会按预期显示。然而,我清楚地知道为什么会产生这些引号,它们有什么用处,以及我是如何安全地删除它们的。
密码是:
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();
编辑: .jrxml 的相关部分是: 如何让 JRCsvExporter 在创建 csv 文件时忽略元素?
<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>
问题是由于 reportElement“notice-text”。
它的textFieldExpression的数据在很多情况下可以为空或者为null,为了避免在为null或者为空的时候打印出来我们要加上:
所以它看起来像: