简体   繁体   English

带有Java的jasper报告的White Report

[英]White Report with jasper report from Java

I have a problem with jasper report: I created a report with iReport 5.6.0 and it is operating normally. jasper报表存在问题:我使用iReport 5.6.0创建了一个报表,并且运行正常。 It contains only parameters. 它仅包含参数。 My problem comes from java: when I try to create a pdf file from my java class, it creates a file with all white size 1 kb. 我的问题来自java:当我尝试从java类创建pdf文件时,它会创建全部为1 kb的白色文件。 From IReport it works properly. 从IReport,它可以正常工作。 I tried to compile the ".jasper" file by iReport and also from java. 我试图通过iReport以及从Java编译“ .jasper”文件。 The compilation id done properly but when I try to pass parameters is wrong. 编译ID正确完成,但是当我尝试传递参数时出错。 PDF creation is successful in the end but the page is all white. PDF创建最终成功,但是页面全为白色。 Helppppp !!! 求助!

ps Excuse me for may bad english!! ps对不起,英语不好!

My java class: 我的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");



}

}

May be empty value that you are passing to Jasper Report via HashMap object. 您可能是通过HashMap对象传递给Jasper Report的空值。 Therefore, Jasper Report creates PDF file without data. 因此,Jasper Report将创建不含数据的PDF文件。 You have to check the values is passing or not. 您必须检查值是否通过。 Bellow is the snap code and query in the Jasper Report : Query- 波纹管是Jasper报表中的快照代码和查询:Query-

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

. Heare EMPID is a parameter created at Jasper Report. Heare EMPID是在Jasper Report中创建的参数。 Now the Java code to call the Jasper Report and passing the value to it as bellow.... 现在,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); 

Remember, the key in HashMap object and parameter at Jasper Report are the same 'EMPID'. 请记住,HashMap对象中的键和Jasper Report中的参数是相同的“ EMPID”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM