Olá estou fazendo a minha aplicação que usa JSF exportar para excel.
Mas estou encontrando 2 problemas:
1)Quando ele pede para eu salvar o arquivo vem a extenção html ao invez de xls, sendo necessário eu renomear o arquivo para conseguir abrir em xls.
2)Como já aconteceu com muitos, o excel vem meio bugado com algumas coisas não aparecendo e tal.
e a terceira pergunta
3)Em que versão do excel, o jasperreport se baseou?
Olhei essa documentção, mas não me ajudou muito
http://www.jasperforge.org/jaspersoft/opensource/business_intelligence/jasperreports/faq.html#FAQ26
Se alguém tiver algum sugestão fico agradecido
O meu código:
public void gerarXLS() throws JRException, IOException
{
FacesContext context = FacesContext.getCurrentInstance();
ServletContext servletContext = (ServletContext) context.getExternalContext().getContext();
String path = servletContext.getRealPath("/");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(path + "/relatorios/produtos.jasper");
SimpleDateFormat dateformatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
Date dataDe = (Date)calendarioDe.getValue();
Date dataAte = (Date)calendarioAte.getValue();
Map parameters = new HashMap();
parameters.put("dataIni",dateformatYYYYMMDD.format(dataDe));
parameters.put("dataFim",dateformatYYYYMMDD.format(dataAte));
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, Coneccao.getConnection());
//-------------------------------------------------
//XLS OUTPUT STREAM
//-------------------------------------------------
JExcelApiExporter exporter = new JExcelApiExporter();
ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
exporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, xlsReport);
exporter.setParameter(JExcelApiExporterParameter.OUTPUT_FILE_NAME, "relatorioDeProdutosMensais.xls");
exporter.setParameter(JExcelApiExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JExcelApiExporterParameter.IS_AUTO_DETECT_CELL_TYPE, Boolean.TRUE);
exporter.setParameter(JExcelApiExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.TRUE);
exporter.setParameter(JExcelApiExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
exporter.setParameter(JExcelApiExporterParameter.IS_COLLAPSE_ROW_SPAN, Boolean.TRUE);
exporter.setParameter(JExcelApiExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE);
exporter.exportReport();
byte[] bytes = xlsReport.toByteArray();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
response.setContentType("application/vnd.ms-excel");
response.setContentLength(bytes.length);
xlsReport.close();
OutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
context.responseComplete();
ouputStream.flush();
ouputStream.close();
}