Instanciate Excel into JSP page

N

nashaccount

Hi all,

I'm new in JSP and Excel matters.

Background:
My J2EE application utilises JASPER REPORTS and generates a PDF
without any problems but I still have problems displaying my report in
Excel format. Anyway I need to display a XLS file within a JSP page.
Jasper already generates the XLS file but I'm not able to open the
file within my web browser.

My current code:

<%@ page language="java" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="net.sf.jasperreports.view.JasperViewer" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="import java.sql.SQLException" %>
<%@ page import="import java.util.HashMap" %>
<%@ page import="import java.util.Map" %>
<%@ page import="import net.sf.jasperreports.engine.JRException" %>
<%@ page import="import
net.sf.jasperreports.engine.JasperCompileManager" %>
<%@ page import="import
net.sf.jasperreports.engine.JasperExportManager" %>
<%@ page import="import net.sf.jasperreports.engine.JasperFillManager"
%>
<%@ page import="import net.sf.jasperreports.engine.JasperPrint" %>
<%@ page import="import net.sf.jasperreports.engine.JasperReport" %>
<%@ page import="import net.sf.jasperreports.view.JasperViewer" %>
<%@ page import="net.sf.jasperreports.engine.design.JasperDesign" %>
<%@ page import="net.sf.jasperreports.engine.xml.JRXmlLoader" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>

<%
String filename = "CondensadosSiniestros.jrxml";
String reporttype = request.getParameter("formato");
Map params = new HashMap();
params.put("pFechaIni","01012007");
params.put("pFechaFin","31012007");
params.put("pUsuario","GUHD4624");
params.put("pOrigen","1");

//DATABASE
CONNECTION*******************************************************
Class.forName("oracle.jdbc.driver.OracleDriver");
java.sql.Connection conn =
DriverManager.getConnection("jdbc:eek:racle:thin:mad:10.0.00.00:1522:RG",
"login", "pwd");

//PATH
GENERATION***********************************************************
String path = application.getRealPath("/reports/templates/");
String reportSource = path + "/" + filename;
String pdfExportFile = path + "/exported.pdf";
String xlExportFile = path + "/exported.xls";

System.out.println("pdfExportFile:" + pdfExportFile);
System.out.println("xlExportFile: " + xlExportFile);

//JASPER
STUFF**************************************************************
JasperReport jasperReport =
JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint =
JasperFillManager.fillReport(jasperReport, params, conn);

OutputStream ouputStream = response.getOutputStream();

JRExporter exporter = null;

if( "pdf".equalsIgnoreCase(reporttype) ) {

response.setContentType("application/pdf");
response.setHeader("Content-disposition","inline; filename=
\"exported.pdf\"");
response.setHeader("Cache-Control", "max-age=260");

exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
ouputStream);
JasperExportManager.exportReportToPdfFile(jasperPrint,
pdfExportFile);

DataInputStream is=
new DataInputStream(
new FileInputStream(pdfExportFile));

DataOutputStream os =
new DataOutputStream(response.getOutputStream());

try {
while(true) os.write(is.readByte());
} catch(EOFException eofexc) {
//ready
} catch(IOException ioexc) {

System.out.println("Caught io exception:" + ioexc);
}

is.close();
os.close();

exporter.exportReport();

} else if( "xls".equalsIgnoreCase(reporttype) ) {

response.setContentType("application/xls");
response.setHeader("Content-disposition","inline; filename=
\"exported3.xls\"");
//response.setHeader("Cache-Control", "max-
age=260");


ByteArrayOutputStream arrayOutputStream = new
ByteArrayOutputStream();

//OutputStream out = response.getOutputStream();
//exporterXLS = new JRXlsExporter();

//JRXlsExporter exporterXLS = new JRXlsExporter();
exporter = new JRXlsExporter();
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT,
jasperPrint);
exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,
ouputStream);
//exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,
arrayOutputStream);

exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE,
Boolean.TRUE);

exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
Boolean.FALSE);

exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
Boolean.TRUE);
exporter.exportReport();


}
try {
exporter.exportReport();
} catch (JRException e) {
throw new ServletException(e);
} finally {
if (ouputStream != null) {
try{
ouputStream.close();
} catch (IOException ex) {}
}
}
%>


I appreciate your help,



David ;-)
 
N

nashaccount

Hi all,

I'm new inJSPand Excel matters.

Background:
My J2EE application utilises JASPER REPORTS and generates a PDF
without any problems but I still have problems displaying my report in
Excel format. Anyway I need to display a XLSfilewithin aJSPpage.
Jasper already generates the XLSfilebut I'm not able toopenthefilewithin my web browser.

My current code:

<%@ page language="java" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="net.sf.jasperreports.view.JasperViewer" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="import java.sql.SQLException" %>
<%@ page import="import java.util.HashMap" %>
<%@ page import="import java.util.Map" %>
<%@ page import="import net.sf.jasperreports.engine.JRException" %>
<%@ page import="import
net.sf.jasperreports.engine.JasperCompileManager" %>
<%@ page import="import
net.sf.jasperreports.engine.JasperExportManager" %>
<%@ page import="import net.sf.jasperreports.engine.JasperFillManager"
%>
<%@ page import="import net.sf.jasperreports.engine.JasperPrint" %>
<%@ page import="import net.sf.jasperreports.engine.JasperReport" %>
<%@ page import="import net.sf.jasperreports.view.JasperViewer" %>
<%@ page import="net.sf.jasperreports.engine.design.JasperDesign" %>
<%@ page import="net.sf.jasperreports.engine.xml.JRXmlLoader" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>

<%
String filename = "CondensadosSiniestros.jrxml";
String reporttype = request.getParameter("formato");
Map params = new HashMap();
params.put("pFechaIni","01012007");
params.put("pFechaFin","31012007");
params.put("pUsuario","GUHD4624");
params.put("pOrigen","1");

//DATABASE
CONNECTION*******************************************************
Class.forName("oracle.jdbc.driver.OracleDriver");
java.sql.Connection conn =
DriverManager.getConnection("jdbc:eek:racle:thin:mad:10.0.00.00:1522:RG",
"login", "pwd");

//PATH
GENERATION***********************************************************
String path = application.getRealPath("/reports/templates/");
String reportSource = path + "/" + filename;
String pdfExportFile = path + "/exported.pdf";
String xlExportFile = path + "/exported.xls";

System.out.println("pdfExportFile:" + pdfExportFile);
System.out.println("xlExportFile: " + xlExportFile);

//JASPER
STUFF**************************************************************
JasperReport jasperReport =
JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint =
JasperFillManager.fillReport(jasperReport, params, conn);

OutputStream ouputStream = response.getOutputStream();

JRExporter exporter = null;

if( "pdf".equalsIgnoreCase(reporttype) ) {

response.setContentType("application/pdf");
response.setHeader("Content-disposition","inline; filename=
\"exported.pdf\"");
response.setHeader("Cache-Control", "max-age=260");

exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
ouputStream);
JasperExportManager.exportReportToPdfFile(jasperPrint,
pdfExportFile);

DataInputStream is=
new DataInputStream(
new FileInputStream(pdfExportFile));

DataOutputStream os =
new DataOutputStream(response.getOutputStream());

try {
while(true) os.write(is.readByte());
} catch(EOFException eofexc) {
//ready
} catch(IOException ioexc) {

System.out.println("Caught io exception:" + ioexc);
}

is.close();
os.close();

exporter.exportReport();

} else if( "xls".equalsIgnoreCase(reporttype) ) {

response.setContentType("application/xls");
response.setHeader("Content-disposition","inline; filename=
\"exported3.xls\"");
//response.setHeader("Cache-Control", "max-
age=260");

ByteArrayOutputStream arrayOutputStream = new
ByteArrayOutputStream();

//OutputStream out = response.getOutputStream();
//exporterXLS = new JRXlsExporter();

//JRXlsExporter exporterXLS = new JRXlsExporter();
exporter = new JRXlsExporter();
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT,
jasperPrint);
exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,
ouputStream);
//exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,
arrayOutputStream);

exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE,
Boolean.TRUE);

exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
Boolean.FALSE);

exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
Boolean.TRUE);
exporter.exportReport();

}
try {
exporter.exportReport();
} catch (JRException e) {
throw new ServletException(e);
} finally {
if (ouputStream != null) {
try{
ouputStream.close();
} catch (IOException ex) {}
}
}
%>

I appreciate your help,

David ;-)

This is already solved. Nobody helped out with this one which is kind
of crappy :'(. Anyway, if you are looking for how I worked around
this please let me know ;)
 
Joined
Nov 4, 2009
Messages
3
Reaction score
0
Hi,

Can you let me know , how you were solved this issue.? I have a similar requirement. You help would be highly appreciated.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top