Jump to content
We've recently updated our Privacy Statement, available here ×

Help !! Export to excel is not working


Umakanth

Recommended Posts

Hi. I am using ireport 2.0.0. I want some data to be exported to excel(2007 version).

 

But empty excel is getting generated..

 

Can anyone help in this regard ?

 

I use the below code in my jsp using scriptlet.

 

File reportFile = new File(getServletConfig().getServletContext()

.getRealPath("/jasper/TrendAnalysisReportExcel.jrxml"));

InputStream input= new FileInputStream(reportFile);

JasperDesign design = JRXmlLoader.load(input);

JasperReport report = JasperCompileManager.compileReport(design);

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

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

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

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

Map<String,Object> parameters = new HashMap<String,Object>();

parameters.put("P_StartDate", new java.util.Date(startDate));

parameters.put("P_EndDate", new java.util.Date(endDate) );

parameters.put("P_BU_UNIT_CODE", bUnit );

parameters.put("P_CU_COMPANY_CODE", customer );

 

Connection conn = ConnectionManager.getConnection();

JasperPrint print = JasperManager.fillReport(report, parameters, conn);

 

ByteArrayOutputStream output = new ByteArrayOutputStream();

 

OutputStream outputfile= new FileOutputStream(new

 

File(getServletConfig().getServletContext()

.getRealPath("/spooler/Testing.xls")));

JExcelApiExporter exporterXLS = new JExcelApiExporter();

 

response.setContentType("application/vnd.ms-excel");

 

exporterXLS.setParameter(JExcelApiExporterParameter.JASPER_PRINT, print);

exporterXLS.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, output);

exporterXLS.setParameter(JExcelApiExporterParameter.IS_ONE_PAGE_PER_SHEET,

 

Boolean.FALSE);

exporterXLS.setParameter(JExcelApiExporterParameter.IS_DETECT_CELL_TYPE,

 

Boolean.TRUE);

exporterXLS.setParameter(JExcelApiExporterParameter.IS_WHITE_PAGE_BACKGROUND,

 

Boolean.FALSE);

exporterXLS.setParameter(JExcelApiExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,

 

Boolean.TRUE);

exporterXLS.exportReport();

 

cool regards,

uk

:(

Link to comment
Share on other sites

  • Replies 7
  • Created
  • Last Reply

Top Posters In This Topic

ByteArrayOutputStream output = new ByteArrayOutputStream();

OutputStream outputfile= new FileOutputStream(new

File(getServletConfig().getServletContext()
.getRealPath("/spooler/Testing.xls")));

response.setContentType("application/vnd.ms-excel");

exporterXLS.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, output);

 

So you're doing the following:

  • Create an in-memory output stream.

Create a file output stream.

Set the servlet response content type to Excel.

Export the report to the in-memory buffer.[/ul]

 

To me, this doesn't make any sense so I'm not surprised that it doesn't work. I would suggest you to decide first where you want to export the report.

 

Regards,

Lucian

Link to comment
Share on other sites

hi thanx for the reply.

Ya. iam not an expert in java. I want to export some data to excel. i managed to export to pdf(code is not in this format). I want the same to export to excel to the end user... i am using office 2007 version.

 

pls help...

Link to comment
Share on other sites

If I may give some advice. Code like this is really bad because it doesnt throw any exceptions or report any errors. For example if the file cant be found what then? Is the error reported to a log file?

 

If you can implement a decent logging system and throw exceptions and report errors then you won't have to post here because the error/exception is displayed for you and you can determine what to do then.

 

For example:

Code:

File reportFile = new File(getServletConfig().getServletContext()

.getRealPath("/jasper/TrendAnalysisReportExcel.jrxml"«»));

InputStream input= new FileInputStream(reportFile);

JasperDesign design = JRXmlLoader.load(input);

What if the file cant be found? Where is the exception printed?

 

If I also may give another tip. Working with

Code:
[code]new java.util.Date(startDate))

is very very bad. It has been deprecated since jdk 1.1!!. Instead use Calendar object and pass the date as timeinmillis which is a long.

 

Ya. iam not an expert in java

You might want to learn about javadoc. In the official Date javadoc it clearly says:

 

public Date(String s)
Deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).

 

new Date(String) has been deprecated for 10 years!!

Link to comment
Share on other sites

  • 4 weeks later...

i am trying to Export Excel reports on browser but I am getting bytes code Excel and and i am not able to export reports in Ms-Excel..... when i tryinh to export excel Disk it working fine plz... help me

 

code type-1:

------------------

JasperPrint print= (JasperPrint)request.getAttribute("jasperPrint");

ByteArrayOutputStream output1 = new ByteArrayOutputStream();

OutputStream outputfile= new FileOutputStream(new File("c:/reports/emergency.xls"));

JExcelApiExporter exporterXLS = new JExcelApiExporter();

response.setContentType("application/vnd.ms-excel");

exporterXLS.setParameter(JExcelApiExporterParameter.JASPER_PRINT, print);

exporterXLS.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, output1);

exporterXLS.setParameter(JExcelApiExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);

exporterXLS.setParameter(JExcelApiExporterParameter.IS_DETECT_CELL_TYPE,Boolean.TRUE);

exporterXLS.setParameter(JExcelApiExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);

exporterXLS.setParameter(JExcelApiExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);

exporterXLS.exportReport();

 

 

code type-2:

-------------

 

erPrint print= (JasperPrint)request.getAttribute("jasperPrint");

JExcelApiExporter xlsExporter = new JExcelApiExporter();

ServletOutputStream ouputStream;

 

byte[] output;

ByteArrayOutputStream baos = new ByteArrayOutputStream(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);

exporter.exportReport();

output = baos.toByteArray();

 

 

 

 

//output = exportReportToBytes(print, xlsExporter);

response.setContentLength(output.length);

ouputStream = response.getOutputStream();

ouputStream.write(output);

ouputStream.flush();

ouputStream.close();

*/

 

}

 

 

catch(Exception ex)

{

ex.printStackTrace();

}

/*

private byte[] exportReportToBytes(JasperPrint jasperPrint, JRExporter exporter) throws JRException {

byte[] output;

ByteArrayOutputStream baos = new ByteArrayOutputStream(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);

exporter.exportReport();

output = baos.toByteArray();

return output;

}

*/

%>

</html:html>

 

 

code type3:

-----------------

 

* System.out.println("before printss");

JasperPrint print= (JasperPrint)request.getAttribute("jasperPrint");

System.out.println("before export object create");

JExcelApiExporter xlsExporter = new JExcelApiExporter();

System.out.println("before stream");

ServletOutputStream servletOutputStream=response.getOutputStream();

// OutputStream outputfile= new FileOutputStream(new File("c:/JasperReport.xls"));

 

 

System.out.println("before jasper print");

xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, print);

System.out.println("before out stream");

xlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream);

System.out.println("before export");

servletOutputStream.flush();

xlsExporter.exportReport();

System.out.println("after export"); **/

it is working fine to Disk but not in Browser

 

 

Any body help me please i am truble from three days... plz help i need to display Excel reports on ..client side (or)browser

Link to comment
Share on other sites

Hi Lucian,

 

Im new to this Jasper Reports and to this forum. I saw the previous threads in the forum.

 

It is running into issues like above wht i've mentioned. Briefly my req. is export the report into excel.My code exist in struts action and jsp files. This exporting code is there in jsp file.

 

My problem is When i export the jrxml file it is writting byte-code values into excel(so ms-excel is not identifying this byte-code values).

 

Im using windows-xp and office 2000.

 

pls give any suggession or solution.It'll be gr8.

 

Thanks,

Lakshmi

Link to comment
Share on other sites

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...