export reports.jasper (ireport) in excel format

0

I have not been able to find the solution to be able to export a report to excel. I managed to export it in pdf format in a dynamic web project in eclipse.

    File reportfile = new File (application.getRealPath("empleado.jasper"));

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

    String valor1 = request.getParameter("txtparametro1");
    parameter.put("valo",new String(valor1));
    String valor2 = request.getParameter("txtparametro2");
    parameter.put("valor",new String(valor2));

    byte[] bytes = JasperRunManager.runReportToPdf(reportfile.getPath(), parameter,con);

    response.setContentType("application/pdf");
    response.setContentLength(bytes.length);
    ServletOutputStream outputstream = response.getOutputStream();
    outputstream.write(bytes,0,bytes.length);

    outputstream.flush();
    outputstream.close();
    %>

All of the above, including the connection, are made within the WEB-INF in .jsp files and I would like the export in excel to be also in .jsp.

Someone who can help me, I would really appreciate it.

    
asked by alexis almaran 23.08.2017 в 01:47
source

1 answer

0

I will put my example to generate an Excel from a web application, it would be similar for you:

jasperPrint = JasperFillManager.fillReport(reportePath, parametros, conn);

// Mostrando el documento
httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
httpServletResponse.setHeader("Content-disposition", "attachment; filename=" + nombreReporte + ".xls");
httpServletResponse.setContentType("application/vnd.ms-excel");
//httpServletResponse.setContentLength(arrayOutputStream.toByteArray().length);

JRXlsExporter exporterXLS = new JRXlsExporter();

exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, servletOutputStream);
exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporterXLS.exportReport();

FacesContext.getCurrentInstance().responseComplete();
    
answered by 30.08.2017 в 19:12