我有从jrxml到pdf的转换。我在DTO中将数据发送为001,但在Excel中显示该数据时却变成了1。以下是用于显示数据的代码。
代码:
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement style="table 5_CH" stretchType="RelativeToTallestObject" x="0" y="0" width="71" height="26" backcolor="#FFFFFF">
<property name="class" value="sortable"/>
</reportElement>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font pdfFontName="Helvetica"/>
<paragraph lineSpacing="Single" firstLineIndent="3" spacingAfter="5"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{reference}]]>
</textFieldExpression>
</textField>
我还需要显示前导零。因此,请让我知道如何解决此问题。
您正在使用MS Excel应用程序查看csv文件。该CSV只是一个纯文本格式,它不包含信息“如何显示数据”。此类数据的视图仅取决于您的查看器。因此,你可以尝试使用JRXlsExporter或JExcelApiExporter或JRXlsxExporter出口商-用于构建报告的Excel格式(XLS或XLSX)。
让我们来看一个例子。我们将csv文件作为数据源。
该CSV文件数据(excel_data.csv):
number_as_string,date,number_as_int
001,2013-09-23,1
002,2013-09-22,3
3,2013-09-21,4
004,2013-09-20,5
iReport中数据源的配置为:
我们将使用样本中的net.sf.jasperreports.export.xls.pattern和net.sf.jasperreports.export.xls.detect.cell.type属性获取所需格式的信息。
我们的模板将是:
<?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="test_excel_formatting" language="groovy" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="952b781d-520d-4a76-aa59-f635b8759fbe">
<property name="net.sf.jasperreports.export.xls.detect.cell.type" value="true"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="number_as_string" class="java.lang.String"/>
<field name="date" class="java.util.Date"/>
<field name="number_as_int" class="java.lang.Integer"/>
<columnHeader>
<band height="42" splitType="Stretch">
<staticText>
<reportElement uuid="667b6ca6-bf50-404e-8ef1-c654456ed8e4" stretchType="RelativeToTallestObject" mode="Opaque" x="0" y="0" width="88" height="42" backcolor="#FF99CC"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Number as String, without pattern]]></text>
</staticText>
<staticText>
<reportElement uuid="080a5cc0-fa66-4b11-a3d3-f0b8a4e69204" stretchType="RelativeToTallestObject" mode="Opaque" x="88" y="0" width="88" height="42" backcolor="#9999FF"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Number as Int, without pattern]]></text>
</staticText>
<staticText>
<reportElement uuid="d1fc6219-2e33-4cc9-b4fe-4ac2b5365102" stretchType="RelativeToTallestObject" mode="Opaque" x="176" y="0" width="104" height="42" backcolor="#99FFCC"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Date without pattern]]></text>
</staticText>
<staticText>
<reportElement uuid="991837d7-a1ab-44ed-8177-756cfae9d369" stretchType="RelativeToTallestObject" mode="Opaque" x="280" y="0" width="104" height="42" backcolor="#9999FF"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Number as Int with common pattern]]></text>
</staticText>
<staticText>
<reportElement uuid="d38154af-2017-4e10-a176-4d66e5cc9f96" stretchType="RelativeToTallestObject" mode="Opaque" x="384" y="0" width="104" height="42" backcolor="#9999FF"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Number as Int with xls pattern]]></text>
</staticText>
<staticText>
<reportElement uuid="17f57019-8691-4643-abdb-ab15afe63f36" stretchType="RelativeToTallestObject" mode="Opaque" x="488" y="0" width="104" height="42" backcolor="#9999FF"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Number as Int with common and xls pattern]]></text>
</staticText>
<staticText>
<reportElement uuid="f4f506bf-9511-4e1e-805b-8752a98e91ea" stretchType="RelativeToTallestObject" mode="Opaque" x="592" y="0" width="104" height="42" backcolor="#99FFCC"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Date with common pattern]]></text>
</staticText>
<staticText>
<reportElement uuid="dffea96e-1dfa-47e0-8551-dd417c25ecbd" stretchType="RelativeToTallestObject" mode="Opaque" x="696" y="0" width="104" height="42" backcolor="#99FFCC"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Date with common pattern]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="3d11d568-6e2c-4373-af7d-fa79b99a2908" mode="Opaque" x="0" y="0" width="88" height="20" backcolor="#FF99CC"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{number_as_string}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="8b8f968e-ca6d-44d0-bc2e-50a4773d0a6f" mode="Opaque" x="88" y="0" width="88" height="20" backcolor="#9999FF"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{number_as_int}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="943a468b-491d-4955-b0c0-58d550536234" mode="Opaque" x="176" y="0" width="104" height="20" backcolor="#99FFCC"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
</textField>
<textField pattern="000000">
<reportElement uuid="52a26b66-1ef5-4f77-a4a2-900a606a1171" mode="Opaque" x="280" y="0" width="104" height="20" backcolor="#9999FF"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{number_as_int}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="c26be152-fae4-4a85-ae55-c70e27739a87" mode="Opaque" x="384" y="0" width="104" height="20" backcolor="#9999FF">
<property name="net.sf.jasperreports.export.xls.pattern" value="00000000000000"/>
</reportElement>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{number_as_int}]]></textFieldExpression>
</textField>
<textField pattern="00000">
<reportElement uuid="fab0fba1-1718-479d-841b-6cbaf1963a0b" mode="Opaque" x="488" y="0" width="104" height="20" backcolor="#9999FF">
<property name="net.sf.jasperreports.export.xls.pattern" value="00000000000000"/>
</reportElement>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{number_as_int}]]></textFieldExpression>
</textField>
<textField pattern="dd-MMM-yyyy">
<reportElement uuid="07552f94-56c8-413d-8dd8-5b5c667c81cf" mode="Opaque" x="592" y="0" width="104" height="20" backcolor="#99FFCC"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="844449f9-1578-4729-b911-18bdd17f8cd4" mode="Opaque" x="696" y="0" width="104" height="20" backcolor="#99FFCC">
<property name="net.sf.jasperreports.export.xls.pattern" value="dd mmmm yyyy"/>
</reportElement>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
iReport中报告的设计如下所示:
在示例中,我将报表的属性net.sf.jasperreports.export.xls.detect.cell.type设置为true。
我将没有任何模式的number_as_string字段放入第一列(粉红色)。
我已将number_as_int字段放入第2、4、5、6(蓝色)列中。
我已将日期字段放入第3、7、8列(绿色)。
结果(以Excel格式生成的文件)将为:
注意:
如您所见,第一列包含原始格式的数据。
列#6包含通过net.sf.jasperreports.export.xls.pattern属性设置的格式的数据。该属性比textFields元素的pattern属性“强” 。
在Excel中,用的帮助下生成的文件的iReport 5.2.0与XLS预览,XLS预览(使用JExcel API)和Excel 2007中(XLSX)预览选项-结果是一样的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句