来自Java的jasper报告的白色报告

时间:2015-06-12 16:17:42

标签: java report

我的jasper报告有问题: 我用iReport 5.6.0创建了一个报告,它运行正常。它只包含参数。 我的问题来自java:当我尝试从我的java类创建一个pdf文件时,它会创建一个白色大小为1 kb的文件。 从IReport它正常工作。 我试图通过iReport和java编译“.jasper”文件。编译ID正确完成,但当我尝试传递参数是错误的。 PDF创建最终是成功的,但页面全是白色的。 Helppppp !!!

P.S。对不起,可能是英语不好!!

我的java课程:

package it.minervaice.report;

import it.minervaice.utility.T;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class ReportUtility {              
String JASPER_REPORT_FOLDER         = "C:\\Users\\Minerva\\Desktop\\Dropbox\\workspace\\MinervaICE\\WebContent\\report\\";
String JASPER_FILENAME_MAIN_DATE    = "distintaCompletaPerData";
String JASPER_FILENAME_MAIN_ID      = "distintaCompletaPerID";
String JASPER_FILENAME_CHILD        = "dettaglioRicetta";   

JasperPrint jp = null;

public JasperPrint getReportCompilatoPerFlusso () {
    return jp;
}


// II° STEP:    rendering del report
public boolean renderingReportDaID(Integer id_distinta) {
    //Connection conn = DBUtility.getConnection();

    try {
        //Parametri da passare al jasper.
        Map<String, Object> parameters = new HashMap<> ();

        parameters.put("id_distinta", id_distinta);

        T.nota("inizio rendering");
        jp = JasperFillManager.fillReport(JASPER_REPORT_FOLDER + "java.jasper", parameters);
        T.nota("fine rendering");

    } catch (JRException e) {
        T.nota("errore rendering");
        e.printStackTrace();
        T.nota("errore rendering");
    }
    return true;
}



// III° STEP:   creazione pdf
public boolean creaPDF(String nomePDF) { 
    if ( nomePDF.equals("") ) {
        nomePDF = "report";
    }

    // generazione del file PDF
    try {
        T.nota("inizio creazione pdf");
        JasperExportManager.exportReportToPdfFile(jp, JASPER_REPORT_FOLDER + nomePDF + ".pdf");
        T.nota("fine creazione pdf");
    } catch (JRException e) {
        T.nota("errore creazione pdf"); e.printStackTrace();T.nota("errore creazione pdf");
    }

    return true;
}


// III° bis STEP:   scarica pdf con la finestra di download
public boolean downloadPDF(String nomePDF, HttpServletResponse resp) { 
    if ( nomePDF.equals("") ) {
        nomePDF = "report";
    }

    // generazione del file PDF
    try {
        T.nota("inizio creazione download pdf");

        String header = String.format("Attachment; Filename=\"%s.pdf\"", nomePDF );
        ServletOutputStream sos=resp.getOutputStream();
        resp.setContentType("APPLICATION/OCTET-STREAM");
        resp.setHeader("Content-Disposition", header);
        JasperExportManager.exportReportToPdfStream(jp, sos);
        sos.close();
        T.nota("fine creazione download pdf");
    } catch (JRException e) {


    } catch (IOException e) {
        T.nota("errore creazione download pdf");
        e.printStackTrace();
        T.nota("errore creazione download pdf");
    }

    return true;
}   


public static void main(String[] args) {

    ReportUtility ru = new ReportUtility();
    //ru.creaJasper();
    ru.renderingReportDaID(1);
    ru.creaPDF("ciccio");



}

}

1 个答案:

答案 0 :(得分:0)

可能是您通过HashMap对象传递给Jasper Report的空值。因此,Jasper Report创建没有数据的PDF文件。你必须检查值是否通过。 Bellow是Jasper Report:Query-

中的快照代码和查询

SELECT * FROM testTable where id=$P{EMPID}

。 Heare EMPID是在Jasper Report中创建的参数。现在,Java代码调用Jasper Report并将值传递给它,如下所示......

HashMap<String, Object> hm=new HashMap<String, Object>();
hm.put("EMPID", objEmp.getEmpid());

String jasperreportpath="salaryslip.jrxml";    
String jasperReport =JasperCompileManager.compileReportToFile(jasperreportpath);

String exportPath=output_file_location_path+File.separator+"outputFileName.pdf";
JasperExportManager.exportReportToPdfFile(jasperPrint, exportPath); 

请记住,Jasper Report中的HashMap对象和参数中的键与'EMPID'相同。