Bom Dia
Estou estudando o IReport porque ele atende exatamente ao que preciso na minha aplicação.
Vi alguns exemplos, criei no IReport um exemplo de um relatório simples e (no próprio IReport) consigo executa-lo e visualizar os registros do banco. Mas quando executo a minha servlet, o arquivo abre vazio!
Por favor, alguém me ajude. Já pesquisei a respeito, mas desde ontem não evoluo.
Segue abaixo minha servlet:
package com.ericsson.report;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ericsson.dao.GerContratoDao;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JExcelApiExporter;
import net.sf.jasperreports.engine.export.JExcelApiExporterParameter;
public class ProgramacaoReport extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "SELECT * FROM livros";
try {
connection = GerContratoDao.getConnection();
pstmt = connection.prepareStatement(sql);
rs = pstmt.executeQuery(sql);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JRDataSource jrDS = new JRResultSetDataSource(rs);
Map parametros = new HashMap();
String pathJasper = getServletContext().getRealPath(
"/WEB-INF/classes/com/ericsson/report/")
+ "/";
JasperPrint jasperPrint;
try {
jasperPrint = JasperFillManager.fillReport(pathJasper
+ "report1.jasper", parametros, jrDS);
//-------------------------------------------------
//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.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JExcelApiExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.TRUE);
exporter.setParameter(JExcelApiExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
exporter.exportReport();
byte[] bytes = xlsReport.toByteArray();
res.setContentType("application/vnd.ms-excel");
res.setContentLength(bytes.length);
xlsReport.close();
OutputStream ouputStream = res.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
} catch (JRException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
doPost(arg0, arg1);
}
}
Obrigada
