logo
  • Home
  • Acerca
  • Autores
  • Faq
  • Rede
  Twitter   Feed-me! RSS!

TUTORIAL JAVA + FLEX + IREPORT NA PRÁTICA (11)

Colocado por Janderson Cardoso na(s) categoria(s): #JAVA + FLEX NA PRÀTICA, Dica, Frameworks, html, Java, relatório, Ria’s Geral em 08 3rd, 2010 | Sem comentários


 TUTORIAL JAVA + FLEX + IREPORT NA PRÁTICA (11)

TIPO DE SAÍDAS DE RELATÓRIOS COM IREPORT

No último post deste tutorial fiz um screencast explicando um modo de criar relatórios usando ireport + java + flex. Nesse exemplo usei nossas próprias entidades para criar os relatórios assim evitando até em nossos relatórios a necessidade de usar instruções sql. Para quem não assistiu vale a pena conferir (TUTORIAL JAVA + FLEX + IREPORT NA PRÁTICA (10).{obrigado a todos que assitiram esse screencast, até o momento já obtive + de 1100 visualizações ;) }

Obs.: Esse post mesmo que sendo continuação de um tutorial pode ser analisado e aproveitado sem a necessidade de acompahar todo o tutorial e também sem a necessidade de ter conhecimento de Flex. Então bom proveito para todos ;)

o assunto de hoje é uma continuação sobre relatórios com ireport, como muitas pessoas me pediram por email e até via comentário no blog onde eu cometi o desrespeito de não responder :( venho através deste post colocar uma listagem de tipo de saídas que podemos ter para os nosso relatórios, tudo com pouco ou nada de esforço, só pelo fato de ter escolhida a ferramenta certa para a situação certa. Assim como tem a saída pdf no nosso projeto poderemos ter essas listadas abaixos(só listei as que normalmente são utilizadas).

vamos nos basear no ServletReport que foi feito no tutorial. Para te ajudar está logo abaixo:

PLAIN TEXT
JAVA:

  1. public class ServletReport extends HttpServlet {
  2.  
  3.   private ServletContext sc;
  4.  
  5.   private FactoryService factoryService = new FactoryService();
  6.  
  7.   public void init(ServletConfig config) throws ServletException {
  8.     super.init(config);
  9.  
  10.     sc = config.getServletContext();
  11.  
  12.     WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);
  13.  
  14.     AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext
  15.            .getAutowireCapableBeanFactory();
  16.  
  17.     autowireCapableBeanFactory.configureBean(factoryService,“FactoryService”);
  18.  
  19.   }
  20.  
  21.   @SuppressWarnings(“unchecked”)
  22.   protected void service(HttpServletRequest request,
  23.     HttpServletResponse response) throws ServletException, IOException {
  24.  
  25.     List<?> dados = new ArrayList();
  26.     File reportFile = null;
  27.     HashMap parameters = new HashMap();
  28.  
  29.     String acao = request.getParameter(“acao”);
  30.     String where = request.getParameter(“where”);
  31.  
  32.     if (acao.equals(“estado”)) {
  33.         dados = factoryService.estadoService.filterReport(where);
  34.         reportFile = new File(sc.getRealPath(“WEB-INF/reports/relatorioEstado.jasper”));
  35.     }
  36.  
  37.     byte[] bytes = null;
  38.  
  39.     JRDataSource jrds = new
  40.     JRBeanCollectionDataSource(dados);
  41.  
  42.     try {
  43.       bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),parameters, jrds);
  44.     } catch (JRException e) { 
  45.       e.printStackTrace();
  46.     }
  47.  
  48.     if (bytes != null && bytes.length> 0) {
  49.       response.setContentType(“application/pdf”);
  50.       response.setContentLength(bytes.length);
  51.       ServletOutputStream ouputStream = response.getOutputStream();
  52.       ouputStream.write(bytes, 0, bytes.length);
  53.       ouputStream.flush();
  54.       ouputStream.close();
  55.     }
  56.   }
  57. }

no servlet acima temos o exemplo de um relatório saindo em um pdf, agora do try catch para baixo podemos mudar nosso tipo de saída, vamos a listagem abaixo:

XLS – Excel

PLAIN TEXT
JAVA:

  1. try {
  2.  
  3.   JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);
  4.   JExcelApiExporter xlsExporter = new JExcelApiExporter();
  5.   ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
  6.   xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
  7.   xlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,xlsReport);
  8.   xlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.TRUE);
  9.   xlsExporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
  10.  
  11.   xlsExporter.exportReport();
  12.   bytes = xlsReport.toByteArray();
  13.  
  14.   if (bytes != null && bytes.length> 0) {
  15.     response.setContentType(“application/vnd.ms-excel”);
  16.     response.setContentLength(bytes.length);
  17.     xlsReport.close();
  18.     ServletOutputStream ouputStream = response.getOutputStream();
  19.     ouputStream.write(bytes, 0, bytes.length);
  20.     ouputStream.flush();
  21.     ouputStream.close();
  22.   }
  23. } catch (Exception e) {
  24.   e.printStackTrace();
  25.   return;
  26. }

RTF

PLAIN TEXT
JAVA:

  1. try {
  2.  
  3.   JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);
  4.  
  5.   JRRtfExporter rtfExporter = new JRRtfExporter();
  6.   ByteArrayOutputStream rtfByte = new ByteArrayOutputStream();
  7.   rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
  8.   rtfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,rtfByte);
  9.  
  10.   rtfExporter.exportReport();
  11.   bytes = rtfByte.toByteArray();
  12.  
  13.   if (bytes != null && bytes.length> 0) {
  14.     response.setContentType(“application/rtf”);
  15.     response.setContentLength(bytes.length);
  16.     rtfByte.close();
  17.     ServletOutputStream ouputStream = response.getOutputStream();
  18.     ouputStream.write(bytes, 0, bytes.length);
  19.     ouputStream.flush();
  20.     ouputStream.close();
  21.   }
  22. } catch (Exception e) {
  23.   e.printStackTrace();
  24.   return;
  25. }

HTML

PLAIN TEXT
JAVA:

  1. try {
  2.  
  3.   JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile, parameters, jrds);
  4.  
  5.   JRHtmlExporter htmlExporter = new JRHtmlExporter();
  6.   ByteArrayOutputStream htmlByte = new ByteArrayOutputStream();
  7.   htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
  8.   htmlExporter.setParameter(
  9.   JRExporterParameter.OUTPUT_STREAM, htmlByte);
  10.   htmlExporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,false);
  11.  
  12.   htmlExporter.exportReport();
  13.   bytes = htmlByte.toByteArray();
  14.  
  15.   if (bytes != null && bytes.length> 0) {
  16.     response.setContentType(“text/html;charset=UTF-8″);
  17.     response.setContentLength(bytes.length);
  18.     htmlByte.close();
  19.     ServletOutputStream ouputStream = response.getOutputStream();
  20.     ouputStream.write(bytes, 0, bytes.length);
  21.     ouputStream.flush();
  22.     ouputStream.close();
  23.   }
  24. } catch (Exception e) {
  25.   e.printStackTrace();
  26.   return;
  27. }

CSV

PLAIN TEXT
JAVA:

  1. try {
  2.  
  3.   JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);
  4.  
  5.   JRCsvExporter csvExporter = new JRCsvExporter();
  6.   ByteArrayOutputStream csvByte = new ByteArrayOutputStream();
  7.   csvExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
  8.   csvExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,csvByte);
  9.  
  10.   csvExporter.exportReport();
  11.   bytes = csvByte.toByteArray();
  12.  
  13.   if (bytes != null && bytes.length> 0) {
  14.     response.setContentType(“application/csv”);
  15.     response.setContentLength(bytes.length);
  16.     csvByte.close();
  17.     ServletOutputStream ouputStream = response.getOutputStream();
  18.     ouputStream.write(bytes, 0, bytes.length);
  19.     ouputStream.flush();
  20.     ouputStream.close();
  21.   }
  22. } catch (Exception e) {
  23.   e.printStackTrace();
  24.   return;
  25. }

TXT

PLAIN TEXT
JAVA:

  1. try {
  2.  
  3.   JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);
  4.  
  5.   JRTxtExporter txtExporter = new JRTxtExporter();
  6.   ByteArrayOutputStream txtByte = new ByteArrayOutputStream();
  7.   txtExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
  8.   txtExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,txtByte);
  9.  
  10.   txtExporter.exportReport();
  11.   bytes = txtByte.toByteArray();
  12.  
  13.   if (bytes != null && bytes.length> 0) {
  14.     response.setContentType(“application/txt”);
  15.     response.setContentLength(bytes.length);
  16.     txtByte.close();
  17.     ServletOutputStream ouputStream = response.getOutputStream();
  18.     ouputStream.write(bytes, 0, bytes.length);
  19.     ouputStream.flush();
  20.     ouputStream.close();
  21.   }
  22. } catch (Exception e) {
  23.   e.printStackTrace();
  24.   return;
  25. }

DOC – Word

PLAIN TEXT
JAVA:

  1. try {
  2.  
  3.   JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);
  4.  
  5.   JRRtfExporter docExporter = new JRRtfExporter();
  6.   ByteArrayOutputStream docByte = new ByteArrayOutputStream();
  7.   docExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
  8.   docExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,docByte);
  9.  
  10.   docExporter.exportReport();
  11.   bytes = docByte.toByteArray();
  12.  
  13.   if (bytes != null && bytes.length> 0) {
  14.     response.setContentType(“application/msword”);
  15.     response.setContentLength(bytes.length);
  16.     docByte.close();
  17.     ServletOutputStream ouputStream = response.getOutputStream();
  18.     ouputStream.write(bytes, 0, bytes.length);
  19.     ouputStream.flush();
  20.     ouputStream.close();
  21.   }
  22. } catch (Exception e) {
  23.   e.printStackTrace();
  24.   return;
  25. }

ODT – OpenOffice(brOffice) Word

PLAIN TEXT
JAVA:

  1. try {
  2.  
  3.   JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);
  4.  
  5.   JROdtExporter odtExporter = new JROdtExporter();
  6.   ByteArrayOutputStream odtByte = new ByteArrayOutputStream();
  7.   odtExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
  8.   odtExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,odtByte);
  9.  
  10.   odtExporter.exportReport();
  11.   bytes = odtByte.toByteArray();
  12.  
  13.   if (bytes != null && bytes.length> 0) {
  14.     response.setContentType(“application/vnd.oasis.opendocument.text”);
  15.     response.setContentLength(bytes.length);
  16.     odtByte.close();
  17.     ServletOutputStream ouputStream = response
  18.     .getOutputStream();
  19.     ouputStream.write(bytes, 0, bytes.length);
  20.     ouputStream.flush();
  21.     ouputStream.close();
  22.   }
  23. } catch (Exception e) {
  24.   e.printStackTrace();
  25.   return;
  26. }

ODS – OpenOffice(brOffice) Planilha tipo Excel (SpreadSheet)

PLAIN TEXT
JAVA:

  1. try {
  2.  
  3.   JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);
  4.  
  5.   JROdsExporter odsExporter = new JROdsExporter();
  6.   ByteArrayOutputStream odsByte = new ByteArrayOutputStream();
  7.   odsExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
  8.   odsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,odsByte);
  9.  
  10.   odsExporter.exportReport();
  11.   bytes = odsByte.toByteArray();
  12.  
  13.   if (bytes != null && bytes.length> 0) {
  14.     response.setContentType(“application/vnd.oasis.opendocument.spreadsheet”);
  15.     response.setContentLength(bytes.length);
  16.     odsByte.close();
  17.     ServletOutputStream ouputStream = response.getOutputStream();
  18.     ouputStream.write(bytes, 0, bytes.length);
  19.     ouputStream.flush();
  20.     ouputStream.close();
  21.   }
  22. } catch (Exception e) {
  23.   e.printStackTrace();
  24.   return;
  25. }

bem, aí está uma lista bem completa com 9 tipos de saídas(contando com o pdf) para seus relatórios que tenho certeza que vai ser útil!

Cumps. e até a próxima ;)

Similar Posts:

  • TUTORIAL JAVA + FLEX NA PRÁTICA (8) – Datas
  • Pegar Código Fonte de Sites Remotos
  • TUTORIAL JAVA + FLEX NA PRÁTICA 6/6
  • TUTORIAL JAVA + FLEX NA PRÁTICA 7/6 – Bônus
  • TUTORIAL JAVA + FLEX + IREPORT NA PRÁTICA (10)

 TUTORIAL JAVA + FLEX + IREPORT NA PRÁTICA (11)



Veja o post original no blog do autor aqui!  

Janderson Cardoso

Escrito por Janderson Cardoso @ http://www.jandersonfc.com/
Saiba mais sobre o autor na sua pagina de perfil
Outros posts do autor:
» O que falta para as empresas de TI brasileiras?
» Rio 40°, aí vou eu!
» TUTORIAL JAVA + FLEX NA PRÁTICA 5/6

Deixe um comentário



Spam Protection by WP-SpamFree

ACERCA

O que é o RedeRIA ?

O redeRIA não é nada mais que um agregador de feed's que disponibiliza o conteudo de varios blogs e autores ao redor do mundo RIA, actualmente agregamos mais de 2750 entradas vindas de 53 blogs especializados em ria’s, pelo que só fica a ganhar em assinar o feed ou seguir a comunidade no twitter.

Se acha que o seu blog ou um blog de um amigo é interessante e util para os leitores o redeRIA, faça a sua submissão aqui.

Feed: assine já
Twitter: siga-nos

GOOGLE

Votação


Deveria o RedeRia agregar conteúdo em inglês?
Ver Resultados

AUTORES


Eduardo KrausAlexandre TadashiBindableCognitiva SoluçõesDaniel LopesDaniel SchmitzDanielPedrinhaDClick TeamEbercomEdgard DavidsonElvis FernandesErko BrideeFabiel PrestesFábio Batista da SilvaFabio da SilvaFabriccio BernardesFelipe BorellaFlavia MoreiraGabriel VersalliniGabriela T. PerryIgor MusardoJanderson CardosoJoão AugustoJose Carlos FielKelps SousaLeonardo FrançaLucas MarçalLuis MessiasLuiz TarabalMario JuniorMário SantosMauro MartinsPablo SouzaPedro ClaudioreneRia BrazilriaPTRicardo CerqueiraRobson FernandesRodrigo Pereira FragaSaintBrSamuelFacchinelloSergio SouzaSilva DeveloperStefan HorochovecTech CaffeTecinforThiago BuenoVedVinícius SandimWillian ManoXAML Cast

PUBLICIDADE








Powered by Wordpress & msdevstudio.com