struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Meenakshi Singh" <meenakshi.si...@mind-infotech.com>
Subject Xls & CSV format reports with Jasper reports
Date Fri, 28 Oct 2005 07:03:16 GMT

hi all,

please help.
I am unable to generate csv & xls reports with Jasper reports.
Problems:
System Doesn't recognize the format for csv & xls. It gives the download
file box with save & cancel buttons & then open & associate with box.

In case of CSV: I selects the excel prg to associate the file with It
downloads a o byte files, opens nothing & saves a .csv file. I need to open
the file not just save it on the hard disk.

In case of XLS: It downloads a 0 byte file & opens nothing. Also, when I try
to open the file from the hard disk it shows few colmns from the report &
not the whole report.


Thanks& Regards,
Meenakshi.

This is my code:

Xls servlet
--------------

public class XlsServlet extends HttpServlet{

	public void service(
			HttpServletRequest request,
			HttpServletResponse response
			) throws IOException, ServletException
		{
			ServletContext context = this.getServletConfig().getServletContext();

			response.setContentType("application/excel");
			PrintWriter out = response.getWriter();


				File reportFile = new
File(context.getRealPath("/reports/ReportForPrime.jasper"));
				JasperReport jasperReport =
(JasperReport)JRLoader.loadObject(reportFile.getPath());

				Map parameters = new HashMap();
				parameters.put("ReportTitle", "Report");
				parameters.put("BaseDir", reportFile.getParentFile());


				Class.forName("oracle.jdbc.driver.OracleDriver");

				Connection conn=
DriverManager.getConnection("jdbc:oracle:thin:@172.29.32.79:1521:tmsusa","tm
s","tms");


				JasperPrint jasperPrint =
					JasperFillManager.fillReport(
						jasperReport,
						parameters,
						conn
						);

				JRXlsExporter exporter = new JRXlsExporter();

				File destFile = new File(reportFile.getParent(), jasperPrint.getName() +
".xls");


				exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
				exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
destFile.toString());
				exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
Boolean.FALSE);

				exporter.exportReport();
			}
			catch (JRException e)
			{

			}
		}



Code for CSV report servlet
----------------------------

public void service(
			HttpServletRequest request,
			HttpServletResponse response
			) throws IOException, ServletException
		{
			ServletContext context = this.getServletConfig().getServletContext();

			response.setContentType("application/excel");
			PrintWriter out = response.getWriter();

			try
			{
				File reportFile = new
File(context.getRealPath("/reports/ReportForPrime.jasper"));
				JasperReport jasperReport =
(JasperReport)JRLoader.loadObject(reportFile.getPath());

				Map parameters = new HashMap();
				parameters.put("ReportTitle", "Report");
				parameters.put("BaseDir", reportFile.getParentFile());

				Class.forName("oracle.jdbc.driver.OracleDriver");
				Connection conn=
DriverManager.getConnection("jdbc:oracle:thin:@172.29.32.79:1521:tmsusa","tm
s","tms");


				JasperPrint jasperPrint =
					JasperFillManager.fillReport(
						jasperReport,
						parameters,
						conn
						);

				JRCsvExporter exporter = new JRCsvExporter();

				File destFile = new File(reportFile.getParent(), jasperPrint.getName() +
".csv");

				exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
				exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
destFile.toString());

				exporter.exportReport();
			}
			catch (JRException e)
			{

			}
		}







---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message