JasperReports正确导出到csv和pdf但不是xls

时间:2015-12-22 07:41:03

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

我有一份报告,我需要将其导出为pdf和xls文件。我试图在JasperViewer中保存报告而不显式编写用于在我的Java类中导出的代码。问题是,当我尝试以上述方式保存时,pdf和csv正在填充数据时正确创建,但xls似乎只是空白。
我正在使用 Ubuntu 14.04 ,但是我不认为这会是一个问题,因为xls格式仍然可以在Linux上打开和查看。

以下是我的jrxml文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2015-12-22T12:52:53 -->
<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" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ced95029-b569-4027-895c-af2843a4e088">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="net.sf.jasperreports.export.xlsx.detect.cell.type" value="true"/>
    <style name="Title" fontName="Times New Roman" fontSize="50" isBold="true"/>
    <style name="SubTitle" forecolor="#E50000" fontName="Arial" fontSize="18"/>
    <style name="Column header" forecolor="#666666" fontName="Times New Roman" fontSize="12" isBold="true"/>
    <style name="Detail" fontName="Times New Roman" fontSize="12"/>
    <style name="Row" mode="Opaque" fontName="Times New Roman" fontSize="12">
        <conditionalStyle>
            <conditionExpression><![CDATA[$V{REPORT_COUNT}%2 == 0]]></conditionExpression>
            <style backcolor="#000000"/>
        </conditionalStyle>
    </style>
    <parameter name="username" class="java.lang.String"/>
    <field name="filePath" class="java.lang.String"/>
    <field name="createdOn" class="java.sql.Timestamp"/>
    <field name="actionby" class="java.lang.Integer"/>
    <field name="actionType" class="java.lang.String"/>
    <sortField name="createdOn"/>
    <title>
        <band height="136" splitType="Stretch">
            <staticText>
                <reportElement style="Title" x="170" y="0" width="230" height="60" uuid="1a652692-054a-40f3-ade5-68d8da36626d"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[Report]]></text>
            </staticText>
            <textField>
                <reportElement style="SubTitle" x="219" y="62" width="300" height="22" uuid="88d4231e-f014-4b0d-b871-1737ba63a1ae"/>
                <textElement>
                    <font fontName="Times New Roman"/>
                </textElement>
                <textFieldExpression><![CDATA["Activity Report of user "+$P{username}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="172" y="94" width="383" height="42" uuid="f0bfb1ea-a85a-47cd-a1a4-43f22b45e80f"/>
                <textElement textAlignment="Right">
                    <font size="10"/>
                </textElement>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="16" splitType="Stretch">
            <line>
                <reportElement positionType="FixRelativeToBottom" x="0" y="15" width="555" height="1" uuid="1a61a836-d137-48b1-ad67-6ff64600bf93"/>
                <graphicElement>
                    <pen lineWidth="0.5" lineColor="#999999"/>
                </graphicElement>
            </line>
            <staticText>
                <reportElement style="Column header" x="20" y="0" width="100" height="15" forecolor="#03A9F4" uuid="8093e6b6-1479-4d7e-a3e1-8b43ac16fe34"/>
                <text><![CDATA[Date]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="150" y="0" width="250" height="15" forecolor="#03A9F4" uuid="c9463f8f-ce24-4798-9f83-1122da0d5f31"/>
                <text><![CDATA[File/Folder modified]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="400" y="0" width="150" height="15" forecolor="#03A9F4" uuid="cfb1f8c6-3240-4393-be47-70b56eea3926"/>
                <text><![CDATA[Action Taken]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="16" splitType="Stretch">
            <frame>
                <reportElement style="Row" mode="Transparent" x="0" y="0" width="555" height="15" forecolor="#009688" uuid="a98495d8-3c8d-4fa0-81f5-30c3efc2f766"/>
                <textField isStretchWithOverflow="true" pattern="dd-MMM-yyyy">
                    <reportElement style="Detail" x="20" y="0" width="100" height="15" forecolor="#009688" uuid="714380ba-f273-4f2c-9a24-e01f345aeb03"/>
                    <textFieldExpression><![CDATA[$F{createdOn}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" x="150" y="0" width="250" height="15" forecolor="#009688" uuid="e7d518fc-f335-40ab-acc1-8858f53a806e"/>
                    <textFieldExpression><![CDATA[$F{filePath}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" x="400" y="0" width="150" height="15" forecolor="#009688" uuid="34a6eb4a-7c05-4129-815e-ae1153f52798"/>
                    <textFieldExpression><![CDATA[$F{actionType}]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch">
            <line>
                <reportElement positionType="FixRelativeToBottom" x="0" y="3" width="555" height="1" uuid="fa45a887-615a-4d84-a2d7-8a2219671b3c"/>
                <graphicElement>
                    <pen lineWidth="0.5" lineColor="#999999"/>
                </graphicElement>
            </line>
        </band>
    </columnFooter>
    <pageFooter>
        <band height="25" splitType="Stretch">
            <frame>
                <reportElement mode="Transparent" x="-21" y="1" width="597" height="24" forecolor="#D0B48E" backcolor="#F2EBDF" uuid="183682bc-d976-4756-83e0-6625a3f98ed1"/>
                <textField evaluationTime="Report">
                    <reportElement style="Column header" x="533" y="0" width="40" height="20" forecolor="#03A9F4" uuid="0193f9b3-1559-491a-8580-b6988863b6a1"/>
                    <textElement verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement style="Column header" x="453" y="0" width="80" height="20" forecolor="#03A9F4" uuid="d00b105e-494b-418b-8ac9-8b1b4824f4f0"/>
                    <textElement textAlignment="Right" verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
                </textField>
                <textField pattern="EEEEE dd MMMMM yyyy">
                    <reportElement style="Column header" x="22" y="1" width="197" height="20" forecolor="#03A9F4" uuid="0616f3fe-0354-456f-8911-ec30ec51a5ae"/>
                    <textElement verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

以下是处理报告部分的Java代码片段

java.io.InputStream in = this.getClass().getClassLoader().getResourceAsStream("com/ram/report/jasper/Report.jrxml");
                    JasperReport jasperReport=JasperCompileManager.compileReport(in);
                    HashMap<String, Object> params = new HashMap<String, Object>();
                    try
                    {
                        if(emp!=null)
                            params.put("username", emp.getFirstName()+" "+emp.getLastName());
                        else
                            params=null;
                    }
                    catch(Exception e)
                    {
                        params=null;
                    }
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params,new JRBeanCollectionDataSource(list));
                    JasperViewer.viewReport(jasperPrint,false);                 
                    System.out.println("Wrote report");
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }

我浏览了this链接,但无法理解要完成的解决方法。请帮忙!

1 个答案:

答案 0 :(得分:0)

很可能你从库项目(如果使用ant)或maven repo-pom.xml(如果你使用maven)中遗漏了Apache POI。

我没有看到您的代码有任何错误