Subreport Jasper com Bean

0 respostas
fabion

Olá,

Sou iniciante no Java e estou tentando aprender a gerar um relatório que contém um subrelatório com o Jasper e o iReport. Eu consegui criar uma versão desse relatório conectando numa base Oracle. Porém, preciso utilizar como fonte de dados uma lista de beans e não uma consulta de banco. Confesso que não sei ainda como lidar com a questão dos subreports no código Java. Alguém poderia me dar uma dica de como fazer isso ou me mandar um exemplo simples? Segue meu código, esta meio bagunçado porque ja fiz muitas experiências com o mesmo. Obrigado.

public class Exportador {

public static void main(String[] args) throws ExcecaoDeAgenda, ClassNotFoundException, SQLException, IOException , JRException{
	
	Connection Conexao=null;
	Statement Comando=null;
	Statement Comando_detahes=null;
	ResultSet Resultado=null;
	ResultSet detalhes=null;
	
	try
	{
		Class.forName ("oracle.jdbc.driver.OracleDriver");
	}
		catch(java.lang.ClassNotFoundException E)
	{
			ExcecaoDeAgenda.indique
			(ExcecaoDeAgenda.IMPOSSIVEL_CARREGAR_DRIVER);
	}
	try
	{
//		 Se o serviror for a propria maquina, IPSERVIDOR=127.0.0.1

//		 Normalmente, PORTA=1521

//		 A menos que tenha sido criada outra, INSTANCIA=orcl

Conexao = DriverManager.getConnection(jdbc:oracle:thin:@mocho:1527:mocho9i1,sgtp, sstptst);

}

catch(SQLException E)

{

ExcecaoDeAgenda.indique

(ExcecaoDeAgenda.IMPOSSIVEL_ESTABELECER_CONEXAO);

}

try

{

Comando =Conexao.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

Comando_detahes = Conexao.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

}

catch(SQLException E)

{

ExcecaoDeAgenda.indique

(ExcecaoDeAgenda.IMPOSSIVEL_CRIAR_COMANDO);

}

try

{

Resultado = Comando.executeQuery("select nomeholding  from "+

holding order by nomeholding desc);

//segunda consulta

detalhes = Comando_detahes.executeQuery(select c.desccodexc tiporeg,+

count(*) registros,+

sum(numcreditos) numcreditos,sum(numpulsos) numpulsos,+

"sum(duracaocham) duracaocham "+

"from decompexterna d, codigoexclusao c "+

"where d.codcontestacao=c.codexclusao "+

group by d.codcontestacao,c.codexclusao,c.desccodexc);

}

catch (SQLException E)

{

ExcecaoDeAgenda.indique

(ExcecaoDeAgenda.IMPOSSIVEL_EXECUTAR_COMANDO);

}

try

{

Resultado.first ();

}

catch (SQLException E)

{

ExcecaoDeAgenda.indique

(ExcecaoDeAgenda.IMPOSSIVEL_POSICIONAR_EM_LINHA);

}

//exportador para PDF

JRExporter exporter;

//exportador para XLS

JRExporter exporterXLS;

//exportador para PDF

exporter = new JRPdfExporter();

//exportador para XLS

exporterXLS = new JRXlsExporter();

JasperPrint print;

OutputStream fos = null;

List beans = new ArrayList();

List beans_detalhes =  new ArrayList();

Rel_geral rel_geral;

Detalhes detalhesBean ;

//MeuBean meuBean;

//preenche os beans para serem utilizados no relatório

rel_geral = new Rel_geral();

//preenche holding credora

String credora = Resultado.getString(nomeholding);

System.out.println(credora);

rel_geral.setCredora(Resultado.getString(nomeholding));

//pega proximo nome de holding

Resultado.next();

// //preenche holding devedora

String devedora = Resultado.getString(nomeholding);

System.out.println(devedora);

rel_geral.setDevedora(Resultado.getString(nomeholding));
while (detalhes.next()){
	       	detalhesBean = new Detalhes();
	       	//preenche
	       	String tiporeg = detalhes.getString("tiporeg");
			System.out.println(tiporeg);
			detalhesBean.setTiporegistro(detalhes.getString("tiporeg"));
			///preenche
			String numcreditos = detalhes.getString("numcreditos");
			System.out.println(numcreditos);
			detalhesBean.setCreditos(detalhes.getString("numcreditos"));
			//preenche
			String numpulsos = detalhes.getString("numpulsos");
			System.out.println(numpulsos);
			detalhesBean.setPulsos(detalhes.getString("numpulsos"));
			//preenche
			String duracaocham = detalhes.getString("duracaocham");
			System.out.println(duracaocham);
			detalhesBean.setMinutos(detalhes.getString("duracaocham"));
//				preenche

String registros = detalhes.getString(registros);

System.out.println(registros);

detalhesBean.setRegistros(detalhes.getString(registros));

//insere na lista de beans

beans_detalhes.add(detalhesBean);

}

<a href="//beans.add">//beans.add</a>(meuBean);

beans.add(rel_geral);
//cria Jasper Report Source do Master
		JRDataSource jrds = new  
                         JRBeanArrayDataSource(beans.toArray()); 
		//cria Jasper Report Source do detalhe
		JRDataSource jrds_detalhe = new  
                         JRBeanArrayDataSource(beans_detalhes.toArray()); 
		try{
			
			//compila o XML para o Jasper do detalhe
			JasperCompileManager.compileReportToFile(
					"D:/jasper/layouts_excel/detalhes.jrxml", 
					"D:/jasper/layouts_excel/detalhes.jasper");
			//compila o XML para o Jasper do master
			JasperCompileManager.compileReportToFile(
					"D:/jasper/layouts_excel/contestacao_DETUP.jrxml", 
					"D:/jasper/layouts_excel/contestacao_DETUP.jasper");
//				Preenche o Relatorio

print = JasperFillManager.fillReport(

D:/jasper/layouts_excel/detalhes.jasper,

null,

jrds_detalhe);

//Preenche o Relatorio

print = JasperFillManager.fillReport(

D:/jasper/layouts_excel/contestacao_DETUP.jasper,

null,

jrds);

//Exporta o Relatorio para PDF

fos = new FileOutputStream(D:/jasper/layouts_excel/contestacao_DETUP.pdf);				

exporter.setParameter(

JRExporterParameter.OUTPUT_STREAM,fos);

exporter.setParameter(

JRExporterParameter.JASPER_PRINT,print);

exporter.exportReport();

fos.close();

//Exporta o Relatorio para XLS

fos = new FileOutputStream(D:/jasper/layouts_excel/contestacao_DETUP.xls);				

exporterXLS.setParameter(

JRExporterParameter.OUTPUT_STREAM,fos);

exporterXLS.setParameter(

JRExporterParameter.JASPER_PRINT,print);

exporterXLS.exportReport();

fos.close();

}catch(Exception e){

e.printStackTrace();

}finally{

try{

if(fos!=null)fos.close();

}catch(Exception e){}

}	

}

}
Criado 13 de junho de 2005
Respostas 0
Participantes 1