参数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