Problema Ireport - Arquivo XLS

1 resposta
gilluan

Boa tarde,

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.

public void GeraPdf(byte [] relatorio, HttpServletResponse response)
	{
		try
		{
			
			ServletOutputStream out = response.getOutputStream();
			response.setContentType("application/pdf");
			out.write(relatorio);
			
		}catch(IOException e){
			e.printStackTrace();
		}
	}

agora nao sei nem por onde começar para gerar o xls…

Olhei outros topicos no forum mais ainda nao consegui esclarecer minhas duvidas.

Será que alguem poderia me ajudar…

Obrigado… :smiley:

1 Resposta

javaJje

Estive com este mesmo problema, mas consegui solucionar assim.

public void geraRelatorioExcel() throws JRException, Exception  
	{  
		 FacesContext context = FacesContext.getCurrentInstance();
		 HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();  
		 ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
	  
		 
	 	JRDataSource jrds = new JRBeanCollectionDataSource(solicitacoes);
	 	java.net.URL imagemLogo = servletContext.getResource(Constantes.CAMINHO_LOGO);
	 	
	 	
		 /* HashMap de parametros utilizados no relatório. Sempre instanciados */  
	 	 Map<String, Object> parameters = new HashMap<String, Object>(); 
	    parameters.put("imagemLogo", imagemLogo);
		
		 // Busca o Relatório
		 String pathRelatorio = servletContext.getRealPath("/relatorios/relatorio.jrxml");
	    
	    //compila o relatório  
	    JasperReport relatorio = JasperCompileManager.compileReport(pathRelatorio); 
	                      
	    //Preenche o relatório com os parametros e o data source  
	    JasperPrint impressao = JasperFillManager.fillReport(relatorio , parameters, jrds); 
	    
	    //-------------------------------------------------  
	    //XLS OUTPUT STREAM  
	    //-------------------------------------------------           
	                      
	    JRExporter exporter = new JRXlsExporter(); 
	    ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();  
	    
	    //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");
	}

Abs.

Criado 26 de setembro de 2011
Ultima resposta 4 de jun. de 2012
Respostas 1
Participantes 2