Estou trabalhando com o Ireport e estou conseguindo gerar relatorios no formato .pdf numa boa
só que na empresa estamos precisando de um relatorio com a extensao .xls e nao estou conseguindo de forma alguma
o metodo que estou usando para gerar o pdf é o seguinte.
Estive com este mesmo problema, mas consegui solucionar assim.
publicvoidgeraRelatorioExcel()throwsJRException,Exception{FacesContextcontext=FacesContext.getCurrentInstance();HttpServletResponseresponse=(HttpServletResponse)context.getExternalContext().getResponse();ServletContextservletContext=(ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();JRDataSourcejrds=newJRBeanCollectionDataSource(solicitacoes);java.net.URLimagemLogo=servletContext.getResource(Constantes.CAMINHO_LOGO);/* HashMap de parametros utilizados no relatório. Sempre instanciados */Map<String,Object>parameters=newHashMap<String,Object>();parameters.put("imagemLogo",imagemLogo);// Busca o RelatórioStringpathRelatorio=servletContext.getRealPath("/relatorios/relatorio.jrxml");//compila o relatório JasperReportrelatorio=JasperCompileManager.compileReport(pathRelatorio);//Preenche o relatório com os parametros e o data source JasperPrintimpressao=JasperFillManager.fillReport(relatorio,parameters,jrds);//------------------------------------------------- //XLS OUTPUT STREAM //------------------------------------------------- JRExporterexporter=newJRXlsExporter();ByteArrayOutputStreamxlsReport=newByteArrayOutputStream();//Informar o objeto JasperPrint que será exportado para excel// exportação para excel exporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT,impressao);exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,xlsReport);exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.TRUE);exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.TRUE);exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.FALSE);exporter.exportReport();//Informa o diretorio + nome do arquivo e gera // exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "c:\\relatorio.xls"); // converte para bytes byte[]bytes=xlsReport.toByteArray();response.setContentType("application/vnd.ms-excel");response.setContentLength(bytes.length);xlsReport.close();enviarArquivoPeloOutputStream(bytes,"Relatorio.xls");}