Jump to content
Changes to the Jaspersoft community edition download ×

Exporting to XLS & Embeding report in jsp

2004 IR Help

Recommended Posts

By: Raj - mageshraj

Exporting to XLS & Embeding report in jsp

2005-07-07 18:27

I am having problem of exporting the reports to Excel file.


Java Code:


public byte[] xlsReportToArray(JasperPrint jasperPrint){

byte[] bytes = null;


JRXlsExporter jasperXlsExportMgr = new JRXlsExporter();


ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();

jasperXlsExportMgr.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);

jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_AUTO_DETECT_CELL_TYPE, java.lang.Boolean.TRUE);

jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, java.lang.Boolean.FALSE);

jasperXlsExportMgr.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport);


bytes = xlsReport.toByteArray();

}catch (JRException jex){

// throw new Exception ("Unable to create the XLS file" + jex);



return bytes;



public byte[] loadReportXLS()


//Get the connection, parameters...


byte[] bytes = xlsReportToArray(jasperPrint);

return bytes;


JSP code


byte[] bytes = loadReportXLS();

response.setHeader("Content-Disposition", "attachment; filename="+filename+".XLS");




if (bytes.length > 0)



ServletOutputStream ouputStream = response.getOutputStream();

ouputStream.write(bytes, 0, bytes.length);



}catch (IOException ioex){








When i open the jsp page, it opens a popup window , with options of "Open","Save" etc.. if i click on open, it opens the report in the excel, which works great.


BUT I need to open the excel file without the popup screen.. I tried uncommenting the "response.setHeader" property which stops the popup window, but the data is not displayed properly(column is missing, data scattered around)..


2. How to open(embed) the report in JSP without saving/exporting it in a html file...


Any help to solve this would be appreciated..







By: sanforum - sanforum

not showing image when exporting to excel

2006-01-05 01:21

The following is the code to generate report in excel format.

It works fine for reports having texts only..

But when it produce reports having image it is not showing the image...

the image file path is written in the .JRXML file which is compiled to .JASPER file from which the report is generated in Excel in jsp


The code for generating the reports in Jsp in Excel format......


<%@page contentType="application/vnd.ms-excel"%>

<%@ page errorPage="error.jsp" %>

<%@ page import="net.sf.jasperreports.engine.*" %>

<%@ page import="net.sf.jasperreports.engine.util.*" %>

<%@ page import="net.sf.jasperreports.engine.export.*" %>

<%@ page import="java.util.*" %>

<%@ page import="java.io.*" %>

<%@ page import="java.sql.*" %>






//CreateXml cml=(CreateXml) request.getAttribute("REPORT_BEAN");

//String reportName = cml.getReportName();

String reportName=request.getParameter("reportName");





Connection conn=DriverManager.getConnection("jdbc:mysql://sfindiajv4:3306/track_master","root","admin");

// File reportFile = new File("./usertemplates/test.jasper");

File reportFile = new File("./usertemplates/"+reportName+".jasper");

String abspath = reportFile.getAbsolutePath(); //tried getAbsolutePath()

String path = reportFile.getPath();

System.out.println("Absolute Path = " + reportFile.getAbsolutePath());

System.out.println(" Path = " + reportFile.getPath());

Map parameters = new HashMap();

parameters.put("BaseDir", reportFile.getParentFile());

//parameters.put("myparam",new Integer(2));

JasperPrint jasperPrint = JasperManager.fillReport ( path,parameters, new net.sf.jasperreports.engine.JREmptyDataSource());

byte bytes[] = new byte[10000];

//String result = JasperRunManager.runReportToHtmlFile("./usertemplates/test.jasper" , parameters, conn);

JRXlsExporter exporter = new JRXlsExporter();


ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport);


System.out.println("Size of byte array:"+xlsReport.size());

bytes = xlsReport.toByteArray();


System.out.println("After JasperPrint = 1");


System.out.println("After JasperPrint = 2");


System.out.println("After JasperPrint = 3");


OutputStream ouputStream = response.getOutputStream();

System.out.println("After JasperPrint = 4");

ouputStream.write(bytes, 0, bytes.length);




catch(Exception e)




Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...