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("jdbcracle:thin10.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 ;-)
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("jdbcracle:thin10.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 ;-)