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:
-
public class ServletReport extends HttpServlet {
-
-
private ServletContext sc;
-
-
private FactoryService factoryService = new FactoryService();
-
-
public void init(ServletConfig config) throws ServletException {
-
super.init(config);
-
-
sc = config.getServletContext();
-
-
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);
-
-
AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext
-
.getAutowireCapableBeanFactory();
-
-
autowireCapableBeanFactory.configureBean(factoryService,“FactoryService”);
-
-
}
-
-
@SuppressWarnings(“unchecked”)
-
protected void service(HttpServletRequest request,
-
-
File reportFile = null;
-
-
-
if (acao.equals(“estado”)) {
-
dados = factoryService.estadoService.filterReport(where);
-
}
-
-
byte[] bytes = null;
-
-
JRDataSource jrds = new
-
JRBeanCollectionDataSource(dados);
-
-
try {
-
bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),parameters, jrds);
-
} catch (JRException e) {
-
e.printStackTrace();
-
}
-
-
if (bytes != null && bytes.length> 0) {
-
response.setContentType(“application/pdf”);
-
response.setContentLength(bytes.length);
-
ServletOutputStream ouputStream = response.getOutputStream();
-
ouputStream.write(bytes, 0, bytes.length);
-
ouputStream.flush();
-
ouputStream.close();
-
}
-
}
-
}
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
-
try {
-
-
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);
-
JExcelApiExporter xlsExporter = new JExcelApiExporter();
-
xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
-
xlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,xlsReport);
-
-
xlsExporter.exportReport();
-
bytes = xlsReport.toByteArray();
-
-
if (bytes != null && bytes.length> 0) {
-
response.setContentType(“application/vnd.ms-excel”);
-
response.setContentLength(bytes.length);
-
xlsReport.close();
-
ServletOutputStream ouputStream = response.getOutputStream();
-
ouputStream.write(bytes, 0, bytes.length);
-
ouputStream.flush();
-
ouputStream.close();
-
}
-
e.printStackTrace();
-
return;
-
}
RTF
-
try {
-
-
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);
-
-
JRRtfExporter rtfExporter = new JRRtfExporter();
-
rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
-
rtfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,rtfByte);
-
-
rtfExporter.exportReport();
-
bytes = rtfByte.toByteArray();
-
-
if (bytes != null && bytes.length> 0) {
-
response.setContentType(“application/rtf”);
-
response.setContentLength(bytes.length);
-
rtfByte.close();
-
ServletOutputStream ouputStream = response.getOutputStream();
-
ouputStream.write(bytes, 0, bytes.length);
-
ouputStream.flush();
-
ouputStream.close();
-
}
-
e.printStackTrace();
-
return;
-
}
HTML
-
try {
-
-
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile, parameters, jrds);
-
-
JRHtmlExporter htmlExporter = new JRHtmlExporter();
-
htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
-
htmlExporter.setParameter(
-
JRExporterParameter.OUTPUT_STREAM, htmlByte);
-
htmlExporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,false);
-
-
htmlExporter.exportReport();
-
bytes = htmlByte.toByteArray();
-
-
if (bytes != null && bytes.length> 0) {
-
response.setContentType(“text/html;charset=UTF-8″);
-
response.setContentLength(bytes.length);
-
htmlByte.close();
-
ServletOutputStream ouputStream = response.getOutputStream();
-
ouputStream.write(bytes, 0, bytes.length);
-
ouputStream.flush();
-
ouputStream.close();
-
}
-
e.printStackTrace();
-
return;
-
}
CSV
-
try {
-
-
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);
-
-
JRCsvExporter csvExporter = new JRCsvExporter();
-
csvExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
-
csvExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,csvByte);
-
-
csvExporter.exportReport();
-
bytes = csvByte.toByteArray();
-
-
if (bytes != null && bytes.length> 0) {
-
response.setContentType(“application/csv”);
-
response.setContentLength(bytes.length);
-
csvByte.close();
-
ServletOutputStream ouputStream = response.getOutputStream();
-
ouputStream.write(bytes, 0, bytes.length);
-
ouputStream.flush();
-
ouputStream.close();
-
}
-
e.printStackTrace();
-
return;
-
}
TXT
-
try {
-
-
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);
-
-
JRTxtExporter txtExporter = new JRTxtExporter();
-
txtExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
-
txtExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,txtByte);
-
-
txtExporter.exportReport();
-
bytes = txtByte.toByteArray();
-
-
if (bytes != null && bytes.length> 0) {
-
response.setContentType(“application/txt”);
-
response.setContentLength(bytes.length);
-
txtByte.close();
-
ServletOutputStream ouputStream = response.getOutputStream();
-
ouputStream.write(bytes, 0, bytes.length);
-
ouputStream.flush();
-
ouputStream.close();
-
}
-
e.printStackTrace();
-
return;
-
}
DOC – Word
-
try {
-
-
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);
-
-
JRRtfExporter docExporter = new JRRtfExporter();
-
docExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
-
docExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,docByte);
-
-
docExporter.exportReport();
-
bytes = docByte.toByteArray();
-
-
if (bytes != null && bytes.length> 0) {
-
response.setContentType(“application/msword”);
-
response.setContentLength(bytes.length);
-
docByte.close();
-
ServletOutputStream ouputStream = response.getOutputStream();
-
ouputStream.write(bytes, 0, bytes.length);
-
ouputStream.flush();
-
ouputStream.close();
-
}
-
e.printStackTrace();
-
return;
-
}
ODT – OpenOffice(brOffice) Word
-
try {
-
-
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);
-
-
JROdtExporter odtExporter = new JROdtExporter();
-
odtExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
-
odtExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,odtByte);
-
-
odtExporter.exportReport();
-
bytes = odtByte.toByteArray();
-
-
if (bytes != null && bytes.length> 0) {
-
response.setContentType(“application/vnd.oasis.opendocument.text”);
-
response.setContentLength(bytes.length);
-
odtByte.close();
-
ServletOutputStream ouputStream = response
-
.getOutputStream();
-
ouputStream.write(bytes, 0, bytes.length);
-
ouputStream.flush();
-
ouputStream.close();
-
}
-
e.printStackTrace();
-
return;
-
}
ODS – OpenOffice(brOffice) Planilha tipo Excel (SpreadSheet)
-
try {
-
-
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);
-
-
JROdsExporter odsExporter = new JROdsExporter();
-
odsExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
-
odsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,odsByte);
-
-
odsExporter.exportReport();
-
bytes = odsByte.toByteArray();
-
-
if (bytes != null && bytes.length> 0) {
-
response.setContentType(“application/vnd.oasis.opendocument.spreadsheet”);
-
response.setContentLength(bytes.length);
-
odsByte.close();
-
ServletOutputStream ouputStream = response.getOutputStream();
-
ouputStream.write(bytes, 0, bytes.length);
-
ouputStream.flush();
-
ouputStream.close();
-
}
-
e.printStackTrace();
-
return;
-
}
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)







