• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Jasper Reports Excel Output

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%
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 ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
My, my, aren't you a big fella. Here, have a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!