CSV导出格式化和编码问题

时间:2013-06-03 07:06:38

标签: java jasper-reports export-to-csv

我正在使用 JasperReports 3.7.1 来获取.csv,.pdf和.xlsx文件格式。我有一个 jrxml 文件。我的问题是 csv 文件有字符编码问题,包含很多逗号,当我用 Excel 打开它时,列和行的顺序不正确。

我的 jrxml 文件:

<?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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <style name="style1" isDefault="false" fill="Solid" fontName="SansSerif" fontSize="6"/>
    <style name="alternateStyle" isDefault="false" backcolor="#FFFFFF" fontName="SansSerif">
        <box>
            <pen lineWidth="1.0"/>
            <topPen lineWidth="1.0"/>
            <leftPen lineWidth="1.0"/>
            <bottomPen lineWidth="1.0"/>
            <rightPen lineWidth="1.0"/>
        </box>
        <conditionalStyle>
            <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue() % 2 == 0)]]></conditionExpression>
            <style isDefault="false" style="alternateStyle" backcolor="#CCCCCC"/>
        </conditionalStyle>
    </style>
    <parameter name="ReportTitle" class="java.lang.String"/>
    <parameter name="StartDate" class="java.lang.String"/>
    <parameter name="EndDate" class="java.lang.String"/>
    <parameter name="FilterList" class="java.lang.String"/>
    <parameter name="ProcessDateLbl" class="java.lang.String"/>
    <parameter name="CalledNumberLbl" class="java.lang.String"/>
    <parameter name="ZoneLbl" class="java.lang.String"/>
    <parameter name="CallTypeLbl" class="java.lang.String"/>
    <parameter name="DurationVolumeLbl" class="java.lang.String"/>
    <parameter name="CostLbl" class="java.lang.String"/>
    <field name="processDate" class="java.lang.String"/>
    <field name="calledNumberAccessPoint" class="java.lang.String"/>
    <field name="zone" class="java.lang.String"/>
    <field name="callType" class="java.lang.String"/>
    <field name="durationVolume" class="java.lang.String"/>
    <field name="cost" class="java.lang.Double">
        <fieldDescription><![CDATA[cost]]></fieldDescription>
    </field>
    <variable name="SUM_COST" class="java.lang.Double" calculation="Sum">
        <variableExpression><![CDATA[$F{cost}]]></variableExpression>
        <initialValueExpression><![CDATA[0.0]]></initialValueExpression>
    </variable>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="163" splitType="Stretch">
            <rectangle>
                <reportElement x="0" y="33" width="555" height="117" forecolor="#999999" backcolor="#CCCCCC"/>
            </rectangle>
            <rectangle>
                <reportElement x="34" y="43" width="484" height="25" forecolor="#CCCCCC"/>
            </rectangle>
            <staticText>
                <reportElement x="61" y="47" width="107" height="20"/>
                <textElement>
                    <font fontName="SansSerif" size="12"/>
                </textElement>
                <text><![CDATA[Baslangic Tarihi:]]></text>
            </staticText>
            <staticText>
                <reportElement x="310" y="47" width="107" height="20"/>
                <textElement>
                    <font fontName="SansSerif" size="12"/>
                </textElement>
                <text><![CDATA[Bitis Tarihi:]]></text>
            </staticText>
            <textField>
                <reportElement x="168" y="47" width="77" height="20"/>
                <textElement>
                    <font fontName="SansSerif" size="12"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$P{StartDate}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="415" y="47" width="77" height="20"/>
                <textElement>
                    <font fontName="SansSerif" size="12"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$P{EndDate}]]></textFieldExpression>
            </textField>
            <rectangle>
                <reportElement x="0" y="0" width="555" height="33" forecolor="#999999" backcolor="#999999"/>
            </rectangle>
            <textField isBlankWhenNull="true">
                <reportElement x="0" y="0" width="555" height="33" forecolor="#FFFF00" backcolor="#999999"/>
                <textElement textAlignment="Center">
                    <font fontName="SansSerif" size="22"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$P{ReportTitle}]]></textFieldExpression>
            </textField>
            <rectangle>
                <reportElement x="34" y="80" width="484" height="54" forecolor="#CCCCCC"/>
            </rectangle>
            <staticText>
                <reportElement x="61" y="85" width="71" height="20"/>
                <textElement>
                    <font fontName="SansSerif" size="12"/>
                </textElement>
                <text><![CDATA[Filtrelerim:]]></text>
            </staticText>
            <textField>
                <reportElement x="145" y="85" width="347" height="36"/>
                <textElement>
                    <font fontName="SansSerif" size="9"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$P{FilterList}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <columnHeader>
        <band height="32" splitType="Stretch">
            <textField>
                <reportElement mode="Opaque" x="6" y="1" width="101" height="31" forecolor="#FFFFFF" backcolor="#999999"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="SansSerif" size="10" isBold="true"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$P{ProcessDateLbl}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement mode="Opaque" x="107" y="1" width="89" height="31" forecolor="#FFFFFF" backcolor="#999999"/>
                <textElement textAlignment="Center" verticalAlignment="Bottom">
                    <font fontName="SansSerif" size="10" isBold="true"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$P{CalledNumberLbl}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement mode="Opaque" x="196" y="1" width="88" height="31" forecolor="#FFFFFF" backcolor="#999999"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="SansSerif" size="10" isBold="true"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$P{ZoneLbl}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement mode="Opaque" x="284" y="1" width="88" height="31" forecolor="#FFFFFF" backcolor="#999999"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="SansSerif" size="10" isBold="true"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$P{CallTypeLbl}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement mode="Opaque" x="372" y="1" width="88" height="31" forecolor="#FFFFFF" backcolor="#999999"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="SansSerif" size="10" isBold="true"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$P{DurationVolumeLbl}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement mode="Opaque" x="460" y="1" width="88" height="31" forecolor="#FFFFFF" backcolor="#999999"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="SansSerif" size="10" isBold="true"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$P{CostLbl}]]></textFieldExpression>
            </textField>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement style="alternateStyle" mode="Opaque" x="107" y="0" width="89" height="20"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{calledNumberAccessPoint}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="alternateStyle" mode="Opaque" x="6" y="0" width="101" height="20"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{processDate}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="alternateStyle" mode="Opaque" x="196" y="0" width="88" height="20"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{zone}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="alternateStyle" mode="Opaque" x="284" y="0" width="88" height="20"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{callType}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="alternateStyle" mode="Opaque" x="372" y="0" width="88" height="20"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{durationVolume}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="alternateStyle" mode="Opaque" x="460" y="0" width="88" height="20"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.lang.String"><![CDATA[(new java.text.DecimalFormat("0.000").format($F{cost}.doubleValue()))]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="32" splitType="Stretch">
            <rectangle>
                <reportElement x="6" y="0" width="542" height="31" forecolor="#999999" backcolor="#999999"/>
            </rectangle>
        </band>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch">
            <textField>
                <reportElement x="497" y="18" width="18" height="20" forecolor="#990000"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="SansSerif" size="11" isBold="true"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement x="524" y="18" width="24" height="20" forecolor="#990000"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="SansSerif" size="11" isBold="true"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="515" y="18" width="17" height="20" forecolor="#990000"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="SansSerif" size="11" isBold="true"/>
                </textElement>
                <text><![CDATA[/]]></text>
            </staticText>
            <textField evaluationTime="Report">
                <reportElement x="122" y="18" width="23" height="20"/>
                <textElement>
                    <font fontName="SansSerif" size="11"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="6" y="18" width="116" height="20"/>
                <textElement>
                    <font fontName="SansSerif" size="11"/>
                </textElement>
                <text><![CDATA[Toplam Kayit Sayisi:]]></text>
            </staticText>
        </band>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch">
            <staticText>
                <reportElement x="468" y="11" width="46" height="20"/>
                <textElement>
                    <font fontName="SansSerif" size="11" isBold="true" isItalic="false"/>
                </textElement>
                <text><![CDATA[Toplam :]]></text>
            </staticText>
            <textField evaluationTime="Auto" pattern="#,##0.00;-#,##0.00">
                <reportElement x="515" y="11" width="23" height="20"/>
                <textElement>
                    <font fontName="SansSerif" size="11" isBold="true"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$V{SUM_COST}]]></textFieldExpression>
            </textField>
        </band>
    </summary>
</jasperReport>

Csv导出类是:

public abstract class AbstractCsvServlet extends BaseHttpServlet {

    public void service(HttpServletRequest request, HttpServletResponse response) 
            throws IOException, ServletException {
        List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);

        if (jasperPrintList == null) {
            throw new ServletException("No JasperPrint documents found on the HTTP session.");
        }

        Boolean isBuffered = Boolean.valueOf(request.getParameter(BaseHttpServlet.BUFFERED_OUTPUT_REQUEST_PARAMETER));
        if (isBuffered.booleanValue()) {
            FileBufferedOutputStream fbos = new FileBufferedOutputStream();
            JRCsvExporter exporter = getCsvExporter();
            exporter.setParameter(JRCsvExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
            exporter.setParameter(JRCsvExporterParameter.OUTPUT_STREAM, fbos);
            exporter.setParameter(JRCsvExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE);

            try {
                exporter.exportReport();
                fbos.close();

                if (fbos.size() > 0) {
                    response.setContentType(getResponseContentType());
                    setResponseHeader(response);
                    response.setContentLength(fbos.size());
                    ServletOutputStream ouputStream = response.getOutputStream();
                    try {
                        fbos.writeData(ouputStream);
                        fbos.dispose();
                        ouputStream.flush();
                    } finally {
                        if (ouputStream != null) {
                            try {
                                ouputStream.close();
                            } catch (IOException ex) {
                            }
                        }
                    }
                }
            } catch (JRException e) {
                throw new ServletException(e);
            } finally {
                fbos.close();
                fbos.dispose();
            }
        } else {
            response.setContentType(getResponseContentType());
            setResponseHeader(response);

            JRCsvExporter exporter = getCsvExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);

            OutputStream ouputStream = response.getOutputStream();
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);

            try {
                exporter.exportReport();
            } catch (JRException e) {
                throw new ServletException(e);
            } finally {
                if (ouputStream != null) {
                    try {
                        ouputStream.close();
                    } catch (IOException ex) {
                    }
                }
            }
        }
    }

    protected abstract JRCsvExporter getCsvExporter();

    protected String getResponseContentType() {
        return "application/csv";
    }

    protected void setResponseHeader(HttpServletResponse response) {
        response.setHeader("Content-Disposition", "inline; filename=\"file.csv\"");
    }
}

Excel 中打开的报告如下所示:

enter image description here

0 个答案:

没有答案