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

标签 java jasper-reports report

我尝试使用以下代码生成 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();
    }

}
}

该程序正在生成以下异常

net.sf.jasperreports.engine.JRRuntimeException:net.sf.jasperreports.engine.JRException:从文件加载对象时出错:FirstReport.jrxml 在net.sf.jasperreports.export.SimpleExporterInput。(SimpleExporterInput.java:157) 在 FirstReport.main(FirstReport.java:33) 引起原因:net.sf.jasperreports.engine.JRException:从文件加载对象时出错:FirstReport.jrxml 在 net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:131) 在 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 更多 引起原因:java.io.StreamCorruptedException:无效的流头:3C3F786D 在 java.io.ObjectInputStream.readStreamHeader(来源未知) 在 java.io.ObjectInputStream.(来源未知) 在net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream。 (ContextClassLoaderObjectInputStream.java:57) 在 net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:126) ... 4 更多

最佳答案

exporter.setExporterInput() 的参数必须基于 JasperPrint 对象(而不是 jrxml 文件)。

您可以在 net.sf.jasperreports.export.SimpleExporterInput 类中看到它。此类的一些代码:

/**
 * 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对象是fillReport方法执行的结果。例如:

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

在此代码中,jasperReportFile 是与编译的报告文件(不是 jrxml)相对应的对象。在你的情况下 FirstReport.jasper

来自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

关于java - 无法使用 Jasper 报告库生成 Excel 工作表报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28792460/

相关文章:

java - 没有必要将 super() 放在构造函数中吗?

java - 阅读 DER 应用程序特定 (asn1-java-bouncycaSTLe)

java - 使用 Spring MVC 的 Hibernate validator 组

javascript - 在 conf.js 的插件配置中使用 Protractor 参数值

java - 如何使用 selenium 和 Java 从 Jenkins 获取测试运行 ID

java - 当不同的线程访问一个静态方法时,在该方法中声明的对象是本地的还是在java中共享的

java - Jasper 报告 PDF 印地文版

java - 如何从 Java 代码在 JasperReport 中传递整数参数值?

java - 我需要下载 jasper reports 专业试用版

java - 用主报表字段值填充子报表数据