JasperReports - Saida em planilha excel

2 respostas
Marques

Lista,

Como eu faço para, ao invés de gerar um pdf, gerar uma planilha excel?
Para gerar em pdf estou usando o trecho abaixo.

Você pode me passar um exeemplo de como conseguir ou é possível alterando o código abaixo????

Muito obrigado,

José Marques
// gera o relatório
		HttpSession session = request.getSession();
		ServletContext context = session.getServletContext();
		byte[] bytes = null;
		
		try {
			// carrega o arquivo jasper
			JasperReport relatorioJasper = null;
						
				relatorioJasper = (JasperReport) JRLoader
						.loadObject(context.getRealPath("/WEB-INF/reports/EstimativaColheitaCaixas.jasper"));

			// parâmetros
			Map parametros = new HashMap();

			
			// direciona a saída do relatório para um stream
			bytes = JasperRunManager.runReportToPdf(relatorioJasper, parametros, conn);
			
		} catch (JRException e) {
			e.printStackTrace();
		}

		if (bytes != null && bytes.length > 0 && mensagens.isEmpty()) {
			// envia o relatório em formato PDF para o browser
			response.setContentType("application/pdf");			
			response.setContentLength(bytes.length);
			ServletOutputStream ouputStream = response.getOutputStream();
			ouputStream.write(bytes, 0, bytes.length);
			ouputStream.flush();
			ouputStream.close();
			
		}

2 Respostas

J

Boa tarde!
Não fiz o teste, será que basta trocar

response.setContentType("application/pdf");

por

response.setContentType("application/vnd.ms-excel");

?

G
JasperPrint print = JasperManager.fillReport(input, new HashMap(), jasperReports);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        OutputStream outputfile= new FileOutputStream(new File("c:/output/JasperReport.xls"));

        // coding For Excel:
         JRXlsExporter exporterXLS = new JRXlsExporter();
         exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, print);
         exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, outputByteArray);
         exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
         exporterXLS.setParameter(JRXlsExporterParameter.IS_AUTO_DETECT_CELL_TYPE, Boolean.TRUE);
         exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
         exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
         exporterXLS.exportReport();
         outputfile.write(outputByteArray.toByteArray());

Extraído de
http://p2p.wrox.com/topic.asp?TOPIC_ID=40353

Criado 9 de outubro de 2006
Ultima resposta 9 de out. de 2006
Respostas 2
Participantes 3