简体   繁体   English

无法使用Jasper报表库生成Excel工作表报表

[英]Couldn't generate Excel sheet report using Jasper report library

I tried to generate Excel report, using the below code. 我尝试使用以下代码生成Excel报告。

import java.util.*;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.export.OutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleXlsReportConfiguration;

public class FirstReport 
{
public static void main(String[] args) 
{
    try
    {

        JRXlsExporter exporter = new JRXlsExporter();
        exporter.setExporterInput(new         SimpleExporterInput("FirstReport.jrxml"));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("C://sample_report.xls"));

        SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
        configuration.setOnePagePerSheet(true);
        configuration.setDetectCellType(true);
        configuration.setCollapseRowSpan(false);
        exporter.setConfiguration(configuration);   
        exporter.exportReport();

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

}
}

This program is generating following exception 该程序正在生成以下异常

net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error loading object from file : FirstReport.jrxml at net.sf.jasperreports.export.SimpleExporterInput.(SimpleExporterInput.java:157) at FirstReport.main(FirstReport.java:33) Caused by: net.sf.jasperreports.engine.JRException: Error loading object from file : FirstReport.jrxml at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:131) at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:103) at net.sf.jasperreports.engine.util.JRLoader.loadObjectFromFile(JRLoader.java:94) at net.sf.jasperreports.export.SimpleExporterInput. net.sf.jasperreports.engine.JRRuntimeException:net.sf.jasperreports.engine.JRException:从File:FirstReport.jrxml加载对象时出错:net.sf.jasperreports.export.SimpleExporterInput。(SimpleExporterInput.java:157)在FirstReport。 main(FirstReport.java:33)原因:net.sf.jasperreports.engine.JRException:从net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:131)的文件:FirstReport.jrxml加载对象时出错)在net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:103)在net.sf.jasperreports.engine.util.JRLoader.loadObjectFromFile(JRLoader.java:94)在net.sf.jasperreports。 export.SimpleExporterInput。 (SimpleExporterInput.java:153) ... 1 more Caused by: java.io.StreamCorruptedException: invalid stream header: 3C3F786D at java.io.ObjectInputStream.readStreamHeader(Unknown Source) at java.io.ObjectInputStream.(Unknown Source) at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream. (SimpleExporterInput.java:153)...还有1个原因:java.io.StreamCorruptedException:无效的流头:java.io.ObjectInputStream.readStreamHeader(Unknown Source)处的java.io.ObjectInputStream.readStreamHeader(Unknown Source)处的3C3F786D net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream。 (ContextClassLoaderObjectInputStream.java:57) at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:126) ... 4 more net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:126)上的(ContextClassLoaderObjectInputStream.java:57)...还有4个

Parameter of exporter.setExporterInput() must be based on JasperPrint object (not jrxml file). exporter.setExporterInput()参数必须基于JasperPrint对象(而不是jrxml文件)。

You can see it at net.sf.jasperreports.export.SimpleExporterInput class. 您可以在net.sf.jasperreports.export.SimpleExporterInput类中看到它。 Some code from this class: 此类的一些代码:

/**
 * Creates an {@link ExporterInput} object with a single item wrapping the {@link JasperPrint} object that will be exported. 
 * If you already have a JasperPrint object, you can pass it to the exporter using this type of input.
 */
public SimpleExporterInput(JasperPrint jasperPrint)
{
    if (jasperPrint != null)
    {
        this.items = new ArrayList<ExporterInputItem>();
        items.add(new SimpleExporterInputItem(jasperPrint));
    }
}


/**
 * Creates an {@link ExporterInput} object with a single {@link JasperPrint} item read from the provided input stream. 
 * If you want to read the JasperPrint object from an input stream (like a web location), you can pass the stream to this constructor.
 */
public SimpleExporterInput(InputStream inputStream)
{
    if (inputStream != null)
    {
        JasperPrint jasperPrint = null;
        try
        {
            jasperPrint = (JasperPrint)JRLoader.loadObject(inputStream);
        }
        catch (JRException e)
        {
            throw new JRRuntimeException(e);
        }
        this.items = new ArrayList<ExporterInputItem>();
        items.add(new SimpleExporterInputItem(jasperPrint));
    }
}

JasperPrint object is result of execution method fillReport. JasperPrint对象是执行方法fillReport的结果。 For example: 例如:

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReportFile, reportParameters, dataSource);

In this code jasperReportFile is object which corresponds with compiled report file (not jrxml). 在此代码中,jasperReportFile是与编译的报告文件(不是jrxml)相对应的对象。 In your case FirstReport.jasper 在您的情况下,FirstReport.jasper

Some code from net.sf.jasperreports.engine.JasperFillManager : 来自net.sf.jasperreports.engine.JasperFillManager一些代码:

/**
 * @see #fill(String, Map, JRDataSource)
 */
public static JasperPrint fillReport(
    String sourceFileName, 
    Map<String,Object> params,
    JRDataSource dataSource
    ) throws JRException
{
    return getDefaultInstance().fill(sourceFileName, params, dataSource);
}
....
    /**
 * Fills the compiled report design loaded from the specified file and returns
 * the generated report object.
 * 
 * @param sourceFileName source file containing the compile report design
 * @param params     report parameters map
 * @param dataSource     data source object
 * @return generated report object
 */
public JasperPrint fill(
    String sourceFileName, 
    Map<String,Object> params,
    JRDataSource dataSource
    ) throws JRException
{
    File sourceFile = new File(sourceFileName);

    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);

    JasperReportsContext lcJrCtx = getLocalJasperReportsContext(sourceFile);

    return JRFiller.fill(lcJrCtx, jasperReport, params, dataSource);
}

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM