Home » excel » Large jasper report to Excel causes a corrupt file

Large jasper report to Excel causes a corrupt file

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m using jasperreports 3.5.3 for generating a large (but simple) report. Only a table with strings.

When I list a big enough selection the generated file is corrupt. Excel alerts the user and some data is corrupted. But if I filter the data to show some rows including the offending one it’s generated normally.

Does someone have experience with this corrupt excel file results?

Tip: it happens in a Linux/Apache+JBoss server, but the same code on a local Windows/Jboss works fine. I don’t think the Apache in the middle has something to do. It must be something in the generation itself.

How to&Answers:

It seems that jasperreports 3.5.3 can use two kinds of “excel writers”. The by default exporter is JRXlsExporter and doesn’t work very well with big files (at least in my Spring MVC project).

A workaround is using the other exporter, based in JExcelAPI. I could export the data without trouble with this one.

For making jasperreport use JExcelAPI into a Spring MVC instalation you have to write a personalized class. Its very simple:

import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.export.JExcelApiExporter;

import org.springframework.web.servlet.view.jasperreports.AbstractJasperReportsSingleFormatView;

// this is the view class you'll use, instead of JasperReportsXlsView
public class JasperReportsJExcelApiView extends AbstractJasperReportsSingleFormatView
{
    // copied from JasperReportsXlsView
    public JasperReportsJExcelApiView()
    {
        setContentType("application/vnd.ms-excel");
    }

    protected JRExporter createExporter()
    {
        // we create the JExcelAPIExporter, not the JRXlsExporter
        return new JExcelApiExporter();
    }

    // copied from JasperReportsXlsView (I think it says: write binary data, not text)
    protected boolean useWriter()
    {
        return false;
    }
}

You’ll need the jxl.jar in your classpath from the JExcelAPI 2.6 distribution.