JasperReports - Excel导出

3
我已经成功使用JasperReports导出PDF报告,但我无法导出Excel格式。
我的程序生成的xls输出文件无法在Excel中打开(已损坏),如果我用LibreOffice打开,则完全为空白。
以下是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="xls" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d8281a37-1a28-43e6-824c-0d39b69f7d23">
<property name="net.sf.jasperreports.export.xls.detect.cell.type" value="true"/>
<property name="net.sf.jasperreports.print.keep.full.text" value="true"/>
<property name="net.sf.jasperreports.export.xls.wrap.text" value="false"/>
<property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
<property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
<style name="table">
    <box>
        <pen lineWidth="1.0" lineColor="#000000"/>
    </box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<subDataset name="Table Dataset 1" uuid="3897caef-b047-4ba2-8b62-bd46ec43553d"/>
<field name="credit" class="java.lang.Double"/>
<field name="time" class="java.lang.String"/>
<title>
    <band height="50"/>
</title>
<pageHeader>
    <band height="50">
        <staticText>
            <reportElement x="0" y="30" width="100" height="20" uuid="4a091ece-05dc-48a7-8390-bf3722715ca8"/>
            <text><![CDATA[Nikos]]></text>
        </staticText>
    </band>
</pageHeader>
<columnHeader>
    <band height="50">
        <staticText>
            <reportElement x="0" y="30" width="100" height="20" uuid="39c43e60-ee74-4373-bafb-b78647870be5"/>
            <text><![CDATA[Nikos]]></text>
        </staticText>
    </band>
</columnHeader>
<detail>
    <band height="153">
        <textField>
            <reportElement x="0" y="0" width="100" height="20" uuid="ec837765-6e78-417f-b6ea-1cec760fba54"/>
            <textFieldExpression><![CDATA[$F{credit}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="100" y="0" width="100" height="20" uuid="23e21ff0-e776-4a04-a5d0-a4188694446e"/>
            <textFieldExpression><![CDATA[$F{time}]]></textFieldExpression>
        </textField>
    </band>
</detail>
<summary>
    <band height="229">
        <textField>
            <reportElement x="0" y="0" width="100" height="20" uuid="a5015437-e944-41c1-88bf-9a0d9f88e2d0"/>
            <textFieldExpression><![CDATA[$F{credit}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="100" y="0" width="100" height="20" uuid="066120ca-8df1-40fb-8dea-b93bf5ef30a9"/>
            <textFieldExpression><![CDATA[$F{time}]]></textFieldExpression>
        </textField>
    </band>
</summary>

这是我的Java程序(还包括成功导出PDF文件的功能):
public static void main(String[] args) {
    String inPdfFileName = "src/test/javabeans.jasper";
    String inXlsFileName = "src/test/xls.jasper";
    String outPdfName = "test.pdf";
    String outXlsName = "test.xls";
    HashMap pdfParams = new HashMap();
    HashMap xlsParams = new HashMap();

    Collection<FundBean> fundbeans = FundBeanFactory.getBeanCollection();
    pdfParams.put("HighTime", FundBeanFactory.getMax().getTime());
    pdfParams.put("LowTime", FundBeanFactory.getMin().getTime());

    pdfParams.put("Highest", FundBeanFactory.getMax().getCredit().toString());
    pdfParams.put("Lowest", FundBeanFactory.getMin().getCredit().toString());

    JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(fundbeans);
    try {
        JasperPrint PdfPrint = JasperFillManager.fillReport(
                inPdfFileName,
                pdfParams,
                beanCollectionDataSource);

        JasperPrint xlsPrint = JasperFillManager.fillReport(
                inXlsFileName,
                xlsParams,
                beanCollectionDataSource);

        JRPdfExporter pdfExporter = new JRPdfExporter();
        pdfExporter.setExporterInput(new SimpleExporterInput(PdfPrint));
        pdfExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outPdfName));
        SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
        configuration.setCreatingBatchModeBookmarks(true);
        pdfExporter.setConfiguration(configuration);
        pdfExporter.exportReport();

        JRXlsExporter xlsExporter = new JRXlsExporter();

        xlsExporter.setExporterInput(new SimpleExporterInput(xlsPrint));
        xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outXlsName));
        SimpleXlsReportConfiguration xlsReportConfiguration = new SimpleXlsReportConfiguration();
        xlsReportConfiguration.setOnePagePerSheet(false);
        xlsReportConfiguration.setRemoveEmptySpaceBetweenRows(true);
        xlsReportConfiguration.setDetectCellType(false);
        xlsReportConfiguration.setWhitePageBackground(false);
        xlsExporter.setConfiguration(xlsReportConfiguration);

        xlsExporter.exportReport();
    } catch (JRException e) {
        e.printStackTrace();
    }
  }

当我使用iReport Designer在xls格式下预览时,我得到了想要的结果。因此,我猜测问题出现在我的Java代码中...


可能是[使用Jasper和.xlsx时出现无法读取内容的消息]的重复问题。(http://stackoverflow.com/questions/31649772/unreadable-content-message-with-jasper-and-xlsx) - Robert Mugattarov
非常感谢,这帮助我理解了问题所在,这两个字段没有从JRBeanCollection中获取任何数据。现在我能够打开xls文件,但我无法弄清楚为什么这些字段没有被填充。在上面的代码中,pdf报告从同一个JavaBean中填充,运行良好。你能帮忙吗? - user4865748
你的bean集合可能为空,或者这些bean不遵循Java bean规范:https://dev59.com/onI-5IYBdhLWcg3wMFW0 - Robert Mugattarov
иҝҷдёҚжҳҜй—®йўҳпјҢжҲ‘зҢңжөӢгҖӮжҲ‘еңЁPdfеҜјеҮәеҷЁе’ҢXlsеҜјеҮәеҷЁдёӯйғҪдҪҝз”ЁдәҶзӣёеҗҢзҡ„JavaBeanе’ҢзӣёеҗҢзҡ„beanйӣҶеҗҲж–№жі•гҖӮPdfиҫ“еҮәиў«еЎ«е……дәҶпјҢдҪҶxlsжҠҘе‘ҠжІЎжңүгҖӮжҲ‘е°қиҜ•дәҶеҫҲеӨҡдёҚеҗҢзҡ„ж–№жі•пјҢеӨҡж¬Ўжү«жҸҸд»Јз Ғ...дҪ жңүе…¶д»–жғіжі•еҗ—пјҹ - user4865748
它可能是类型不匹配。1)时间报告字段声明为String类型-尝试使用datetime类型。2)同时,使字段类与字段表达式类匹配。现在,您的两个字段都具有表达式类String。3)将此属性添加到您的报告http://jasperreports.sourceforge.net/config.reference.html#net.sf.jasperreports.export.xls.detect.cell.type,并将值设置为true。 - Robert Mugattarov
感谢您的回答,但问题仍然存在。我已将net.sf.jasperreports.export.xls.detect.cell.type设置为true,并使字段类-表达式类匹配。然而,问题仍未解决。 - user4865748
1个回答

2

感谢您的所有帮助, 我找出了问题的原因,我在pdf报告和xls中都使用了相同的bean List,由于某种原因,当我将列表传递给fillReport()以创建pdf打印对象时,所有bean都从列表中删除了。 我为xls打印对象创建了第二个JRBeanCollection,一切正常!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接