[Resolvido] Gerar relatório utilizando somente parâmetros passados

Boa tarde, galera :smiley:

Gostaria de saber se isto é possível:

Tenho um objeto do tipo Cliente e um relatório .jasper que fiz usando o Ireport.

Neste relatório, coloquei alguns parâmetros de entrada como Nome, codigo etc…

Porém quando estou gerando o relatório via código ele diz q não existem páginas a serem exibidas :cry:

Existe algum modo de passar somente os parametros dos campos do relatório ao invés de utilizar SQL?

O métood que estou utilizando é este:

public void geraRelatorioAssociado(Cliente cliente) throws JRException{
		
		// Endereço do relatório
		URL arquivo = getClass().getResource("/model/reports/jasper/RelatorioCliente.jasper");  
			
		// Mapa de parâmetros do relatório
		Map<String, Object> parametros = new HashMap<String,Object>();
		
		parametros.put("ClienteID",cliente.getClienteID());
		parametros.put("Classificacao", cliente.getTipoPessoa());

		JasperReport report = (JasperReport) JRLoader.loadObject(arquivo);
		
		JasperPrint relatorio = JasperFillManager.fillReport(report, parametros); 
		
		JasperViewer viewer = new JasperViewer(relatorio,false);
		viewer.setTitle("Ficha do associado");
		viewer.setVisible(true);
		
	
		
	}

Abrs

Existe e, não é através de parâmetros, mas, utilizando fields.
A diferença entre eles é que:

$P{param}

É utilizado na parte “baixa” da coisa, para ações que o engine do iReport irá executar, como uma consulta ao banco de dados.

$F{field}

É o que é exibido, como resultado do processamento do engine do iReport (como o resultado de uma consulta ao banco de dados).

Se, de forma correta, pretende deixar de usar query no relatório, pode optar por JRBeanCollectionDataSource (se não me engano é isso) e, mapear os objetos que serão colocados nele no relatório, cada atributo a ser exibido deve ter um $F{field} correspondente.

drsmachado:

Estou passando os parâmetros via hashmap mas mesmo assim ele insiste dizer que o “Documento não possui páginas”

Será que a estrutura do relatório está errada?

O código gerado no iReport foi este:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="RelatorioAssociado" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="38e1576f-90fd-4c7e-b7fe-8aa478d6258f">
	<property name="ireport.zoom" value="1.5"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<parameter name="associadoID" class="java.lang.Long"/>
	<parameter name="classificacao" class="java.lang.String"/>
	<parameter name="tipoAssociado" class="java.lang.String"/>
	<parameter name="documento1" class="java.lang.String"/>
	<parameter name="documento2" class="java.lang.String"/>
	<parameter name="nome1" class="java.lang.String"/>
	<parameter name="nome2" class="java.lang.String"/>
	<parameter name="nomeResponsavel" class="java.lang.String"/>
	<parameter name="nomeContato" class="java.lang.String"/>
	<parameter name="dataCadastro" class="java.lang.String"/>
	<parameter name="senha" class="java.lang.String"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="31" splitType="Stretch">
			<staticText>
				<reportElement uuid="5df61920-847a-4e83-8493-aae5ce003301" x="216" y="0" width="139" height="20"/>
				<textElement textAlignment="Justified" verticalAlignment="Middle">
					<font size="14" isBold="true"/>
				</textElement>
				<text><![CDATA[Ficha do associado]]></text>
			</staticText>
		</band>
	</title>
	<detail>
		<band height="149">
			<staticText>
				<reportElement uuid="910ab696-1535-411c-9299-c1ee4b51280c" x="0" y="0" width="45" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Código:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="7755c5f5-bc1a-450f-9bc6-13b4cc84906e" x="85" y="0" width="69" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Classificação:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="7b0eeb5c-4b4e-44f5-9f96-6b2bbcc66322" x="211" y="0" width="30" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Tipo:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="dc13fd2d-0cc2-4d7c-882d-db61067cc03b" x="0" y="20" width="57" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[CNPJ/CPF:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="d1c7af46-45a7-49b6-a4c4-63b5c743c5fc" x="169" y="20" width="100" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Inscrição estadual:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="f8b8e074-9cb6-448f-980d-6c91507d7cfa" x="0" y="40" width="105" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Razão social / Nome:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="948a011f-90d6-480f-8c6e-29620967a672" x="0" y="60" width="75" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Nome fantasia:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="e0f3f7e4-9c84-48ba-8922-e2287c79a56a" x="0" y="80" width="70" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Responsável:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="c77b262c-ca13-4813-80db-b564343237c9" x="0" y="100" width="45" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Contato:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="61f5ae8c-8faf-4ab3-923a-4cc1862d11c7" x="0" y="120" width="85" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Data de cadastro:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="75b84a1b-bc89-4e10-b794-ee74f72f8235" x="141" y="120" width="34" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Senha:]]></text>
			</staticText>
			<textField>
				<reportElement uuid="86b942e1-3694-4968-af32-4aac6ba5ee85" x="45" y="0" width="40" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{associadoID}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="9be07289-021f-407c-b858-72db09ec5439" x="154" y="0" width="52" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{classificacao}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="88c24068-2eb0-41fc-a145-d7e44a17deca" x="241" y="0" width="314" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{tipoAssociado}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="246704f1-1650-4663-9292-ab5b8f2f3586" x="57" y="20" width="112" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{documento1}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="035ab36c-de84-4bc4-9a60-bbd10170ecda" x="273" y="20" width="282" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{documento2}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="800cd806-4141-4ad4-b3fa-5a1c1344f2d7" x="105" y="40" width="450" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{nome1}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="7b180c1d-d44b-401b-b0c1-72a61b4d880d" x="75" y="60" width="480" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{nome2}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="deaed97c-2076-4d61-8277-de958ec79919" x="70" y="80" width="485" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{nomeResponsavel}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="a95383a3-973c-47e9-a2a4-5428084250db" x="45" y="100" width="510" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{nomeContato}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="60e59d06-86d5-43cf-b7ec-6751a2cce36b" x="85" y="120" width="56" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{dataCadastro}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="141c8acd-84f9-4afa-9ac4-4905c0910cdd" x="175" y="120" width="209" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{senha}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
</jasperReport>

e no método de geração do relatório uso este:


public void geraRelatorioAssociado(Associado associado) throws JRException{
		
		// Endereço do relatório
		URL arquivo = getClass().getResource("/model/reports/jasper/RelatorioAssociado.jasper");  
			
		// Mapa de parâmetros do relatório
		Map<String, Object> parametros = new HashMap<String,Object>();
		
		parametros.put("associadoID",associado.getAssociadoID());
		parametros.put("classificacao", associado.getTipoPessoa());
		parametros.put("tipoAssociado", associado.getTipoAssociado().getDescricao());

		JasperReport report = (JasperReport) JRLoader.loadObject(arquivo);
		
		JasperPrint relatorio = JasperFillManager.fillReport(report, parametros); 
		
		JasperViewer viewer = new JasperViewer(relatorio,false);
		viewer.setTitle("Ficha do associado");
		viewer.setVisible(true);
		//viewer.show(); 
		
		
		
		
	}

vlw :smiley:

[quote=zerone]drsmachado:

Estou passando os parâmetros via hashmap mas mesmo assim ele insiste dizer que o “Documento não possui páginas”

Será que a estrutura do relatório está errada?

O código gerado no iReport foi este:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="RelatorioAssociado" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="38e1576f-90fd-4c7e-b7fe-8aa478d6258f">
	<property name="ireport.zoom" value="1.5"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<parameter name="associadoID" class="java.lang.Long"/>
	<parameter name="classificacao" class="java.lang.String"/>
	<parameter name="tipoAssociado" class="java.lang.String"/>
	<parameter name="documento1" class="java.lang.String"/>
	<parameter name="documento2" class="java.lang.String"/>
	<parameter name="nome1" class="java.lang.String"/>
	<parameter name="nome2" class="java.lang.String"/>
	<parameter name="nomeResponsavel" class="java.lang.String"/>
	<parameter name="nomeContato" class="java.lang.String"/>
	<parameter name="dataCadastro" class="java.lang.String"/>
	<parameter name="senha" class="java.lang.String"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="31" splitType="Stretch">
			<staticText>
				<reportElement uuid="5df61920-847a-4e83-8493-aae5ce003301" x="216" y="0" width="139" height="20"/>
				<textElement textAlignment="Justified" verticalAlignment="Middle">
					<font size="14" isBold="true"/>
				</textElement>
				<text><![CDATA[Ficha do associado]]></text>
			</staticText>
		</band>
	</title>
	<detail>
		<band height="149">
			<staticText>
				<reportElement uuid="910ab696-1535-411c-9299-c1ee4b51280c" x="0" y="0" width="45" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Código:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="7755c5f5-bc1a-450f-9bc6-13b4cc84906e" x="85" y="0" width="69" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Classificação:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="7b0eeb5c-4b4e-44f5-9f96-6b2bbcc66322" x="211" y="0" width="30" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Tipo:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="dc13fd2d-0cc2-4d7c-882d-db61067cc03b" x="0" y="20" width="57" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[CNPJ/CPF:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="d1c7af46-45a7-49b6-a4c4-63b5c743c5fc" x="169" y="20" width="100" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Inscrição estadual:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="f8b8e074-9cb6-448f-980d-6c91507d7cfa" x="0" y="40" width="105" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Razão social / Nome:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="948a011f-90d6-480f-8c6e-29620967a672" x="0" y="60" width="75" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Nome fantasia:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="e0f3f7e4-9c84-48ba-8922-e2287c79a56a" x="0" y="80" width="70" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Responsável:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="c77b262c-ca13-4813-80db-b564343237c9" x="0" y="100" width="45" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Contato:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="61f5ae8c-8faf-4ab3-923a-4cc1862d11c7" x="0" y="120" width="85" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Data de cadastro:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="75b84a1b-bc89-4e10-b794-ee74f72f8235" x="141" y="120" width="34" height="20"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Senha:]]></text>
			</staticText>
			<textField>
				<reportElement uuid="86b942e1-3694-4968-af32-4aac6ba5ee85" x="45" y="0" width="40" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{associadoID}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="9be07289-021f-407c-b858-72db09ec5439" x="154" y="0" width="52" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{classificacao}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="88c24068-2eb0-41fc-a145-d7e44a17deca" x="241" y="0" width="314" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{tipoAssociado}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="246704f1-1650-4663-9292-ab5b8f2f3586" x="57" y="20" width="112" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{documento1}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="035ab36c-de84-4bc4-9a60-bbd10170ecda" x="273" y="20" width="282" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{documento2}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="800cd806-4141-4ad4-b3fa-5a1c1344f2d7" x="105" y="40" width="450" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{nome1}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="7b180c1d-d44b-401b-b0c1-72a61b4d880d" x="75" y="60" width="480" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{nome2}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="deaed97c-2076-4d61-8277-de958ec79919" x="70" y="80" width="485" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{nomeResponsavel}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="a95383a3-973c-47e9-a2a4-5428084250db" x="45" y="100" width="510" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{nomeContato}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="60e59d06-86d5-43cf-b7ec-6751a2cce36b" x="85" y="120" width="56" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{dataCadastro}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="141c8acd-84f9-4afa-9ac4-4905c0910cdd" x="175" y="120" width="209" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$P{senha}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
</jasperReport>

e no método de geração do relatório uso este:


public void geraRelatorioAssociado(Associado associado) throws JRException{
		
		// Endereço do relatório
		URL arquivo = getClass().getResource("/model/reports/jasper/RelatorioAssociado.jasper");  
			
		// Mapa de parâmetros do relatório
		Map<String, Object> parametros = new HashMap<String,Object>();
		
		parametros.put("associadoID",associado.getAssociadoID());
		parametros.put("classificacao", associado.getTipoPessoa());
		parametros.put("tipoAssociado", associado.getTipoAssociado().getDescricao());

		JasperReport report = (JasperReport) JRLoader.loadObject(arquivo);
		
		JasperPrint relatorio = JasperFillManager.fillReport(report, parametros); 
		
		JasperViewer viewer = new JasperViewer(relatorio,false);
		viewer.setTitle("Ficha do associado");
		viewer.setVisible(true);
		//viewer.show(); 
		
		
		
		
	}

vlw :smiley: [/quote]
Ler o que eu escrevi, não leu?
Eu disse que $P{} não funciona, tem que ser $F{} e não é através de hashmap, é usando uma classe que recebe uma collection como argumento em seu construtor e se chama JRBeanCollectionDataSource que é provida pelo iReport (uma rápida pesquisa no google e você terá várias opções de tutoriais sobre o uso desta classe).

Entendi kra, vlw :slight_smile:

Outra dúvida que surgiu:

Nesse relatório já estou trazendo os dados do cliente, porém criei um subreport chamado contatos_cliente onde irão ser apresentados os contatos (email, telefone…)

Como faço para passar o datasource de contatos para este subrelatorio que fiz dentro do relatório principal.

PS: vlw mesmo, se estivessemos no yahoo respostas te daria 10 points :lol: :lol:

Não sei como é teu inglês, mas aí vai um tuto bem fácil de como fazer.
http://www.richardnichols.net/2010/02/simple-guide-to-sub-reports-in-jasperreports-ireport/
Não me preocupo com nota, apenas compartilho conhecimento.

Resolvido!

Passei os endereços e contatos como parâmetros pro subrelatório e deu certo! :smiley:

JRBeanCollectionDataSource end = new JRBeanCollectionDataSource(enderecos);
JRBeanCollectionDataSource cont = new JRBeanCollectionDataSource(contatos);

parametros.put("enderecos", end);
parametros.put("contatos", cont);

Uma última dúvida, tem como redimensionar automaticamente as “bands” dos subrelatórios, de modo que eles não sobreponham os demais que estão abaixo?
Não me importo em ter que imprimir mais de uma folha se for necessário…

vlw :smiley:


Resolvido,

basta modificar no ireport a propriedade Position Type para float

abrs galera :lol: