I tried to generate Excel report, using the below code.
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. (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. (ContextClassLoaderObjectInputStream.java:57) at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:126) ... 4 more
Parameter of
exporter.setExporterInput()
must be based on JasperPrint object (not jrxml file).You can see it at
net.sf.jasperreports.export.SimpleExporterInput
class. Some code from this class:JasperPrint object is result of execution method fillReport. For example:
In this code jasperReportFile is object which corresponds with compiled report file (not jrxml). In your case FirstReport.jasper
Some code from
net.sf.jasperreports.engine.JasperFillManager
:HTH