File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Open Source Projects and the fly likes Jasper Reports Excel Output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Jasper Reports Excel Output" Watch "Jasper Reports Excel Output" New topic
Author

Jasper Reports Excel Output

PJ Crump
Ranch Hand

Joined: Feb 06, 2002
Posts: 51
Trying the render a report to excel from a servlet - the servlet code runs fine but when excel opens I get a 'Unable to open file'. Any suggestions?? Here is the servelet code
-----
ServletContext context = this.getServletConfig().getServletContext();
File reportFile = new File(context.getRealPath("/reports/WebappReport.jasper"));
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
Map parameters = new HashMap();
parameters.put("ReportTitle", "Address Report");
parameters.put("BaseDir", reportFile.getParentFile());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new WebappDataSource());
JRXlsExporter exporter = new JRXlsExporter();
ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport);
exporter.exportReport();
System.out.println("Sixe of byte array:"+xlsReport.size());
bytes = xlsReport.toByteArray();
response.setContentType("application/vnd.ms-excel");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
- Thanks in advance
mohankrishna madeti
Greenhorn

Joined: Sep 23, 2003
Posts: 1
<%
try
{
//Class.forName(oracle.jdbc.driver.OracleDriver);
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = ........
File reportFile = new File("C:\\jsp\\sample.jasper");
String path = reportFile.getAbsolutePath(); //tried getAbsolutePath()
//System.out.println("Absolute Path = " + reportFile.getAbsolutePath());
Map parameters = new HashMap();
parameters.put("myparam",new Integer(2));
JasperPrint jasperPrint = JasperManager.fillReport ( path,parameters, conn);
byte bytes[] = new byte[10];
String result = JasperRunManager.runReportToHtmlFile("C:\\jsp\\sample.jasper" , parameters, conn);
JRXlsExporter exporter = new JRXlsExporter();
ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE, "C:\\JSP\\");
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "sample.xls");
exporter.exportReport();
System.out.println("Sixe of byte array:"+xlsReport.size());
bytes = xlsReport.toByteArray();
response.setContentType("application/vnd.ms-excel");
System.out.println("After JasperPrint = "+result);
response.setContentLength(bytes.length);
xlsReport.close();
OutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
}
catch(Exception e)
{e.printStackTrace();}
%>
even i m getting exception but i m able to see my report data on excel file/page and i m able to save it.but there is an exception saying getOutputStream() has already been opened for the response.....
if any thing i forgot or done wrong plese do let me know
thank you.
[ September 23, 2003: Message edited by: mohankrishna madeti ]
Shan Sun
Greenhorn

Joined: Oct 20, 2003
Posts: 1
The above code is fine.
I need one help..
i want to create a xlx file using the jasper report.
i am able to create the report but in the excel sheet i am getting the extra empty line can any one help me to solve this proble.
my id s.sundaram@ebusinessware.com
thanks
shan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jasper Reports Excel Output