将数据导出到PDF JAVA,Eclipse,Jasper

时间:2018-07-14 20:00:47

标签: java jasper-reports

我试图将数据导出为PDF,但是当我尝试将其导出时,pdf不会像“未找到数据”那样显示

此代码在bean上

    public JasperPrint exportTo() {     
    if(this.listReportMaster == null || this.listReportMaster.isEmpty()){           
        FacesMessage messageFailed = new FacesMessage(FacesMessage.SEVERITY_INFO,"Info","No data found");           
        RequestContext.getCurrentInstance().showMessageInDialog(messageFailed);         
        return null;
    }
    String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/report/PRPKReportPDF.jasper");     
    JRBeanCollectionDataSource  beanCollectionDataSource = new JRBeanCollectionDataSource(this.listReportMaster);       
    try {           
        JasperPrint jasperPrint = JasperFillManager.fillReport(path, null, beanCollectionDataSource);           
        return jasperPrint;         
    } catch (JRException e) {           
        e.printStackTrace();
        return null;
    }
}   

public void exportToPdf(ActionEvent actionEvent){   
    if(this.lazyMasterReportDataModel != null){
        System.out.println("masuk exporttopdf");
        String sql = ((LazyMasterReportDataModel) this.lazyMasterReportDataModel).getSqlReportPrint();
        List<Object> listObject = ((LazyMasterReportDataModel) this.lazyMasterReportDataModel).getObjectSqlListReportPrint();
        this.listReportMaster = reportMasterPRPKController.getPRPKForReport(sql, listObject);           

        JasperPrint jasperPrint = exportTo();
        String fileName = "PRPKNew_Report".concat("_").concat(".pdf");
        if(jasperPrint != null) reportMasterPRPKController.exportToPDF(fileName, jasperPrint);
        else System.out.println("jasperprint null");
    }else{
        System.out.println("keluar exporttopdf");
        FacesMessage messageFailed = new FacesMessage(FacesMessage.SEVERITY_INFO,"Info","No data found");           
        RequestContext.getCurrentInstance().showMessageInDialog(messageFailed);
    }
}

每当我尝试导出它时,总是显示“未找到数据”,这是程序运行此代码FacesMessage messageFailed = new FacesMessage(FacesMessage.SEVERITY_INFO,"Info","No data found");的结果,并且“ this.lazyMasterReportDataModel”为空,但是当我再次检查时,没有任何问题在代码上,我不知道它是否有错误的代码或缺陷代码

这是惰性代码

List<ReportMasterPRPK> listMasterPRPK = new ArrayList<>();
ReportMasterPRPKQuery reportMasterPRPKQuery = new ReportMasterPRPKQuery();
Page page = new Page();
String order = "GROUP a.prpk_number, a.prpk_type_id, a.created_date, a.pic_prpk_id, a.business_unit_id, a.pic_department_id, a.prpk_desc, a.prpk_request, a.prpk_background, a.prpk_analysis_benefit, a.priority_level_id, a.cost, b.prpk_type_name, c.business_unit, d.department_name, e.priority_name, f.user_name ORDER BY a.created_date ";
String columname = "";
String sql = "";    
List<Object> objectSqlList = new ArrayList<>();     

String sqlReport = "";
String sqlReportPrint = "";
List<Object> objectSqlListReport = new ArrayList<>();
List<Object> objectSqlListReportPrint = new ArrayList<>();
String flag;

public LazyMasterReportDataModel() {        
}

public LazyMasterReportDataModel(String flag) {     //ini
    this.flag = flag;
}

public LazyMasterReportDataModel(String sqlReport, List<Object> objectSqlListReport) {      
    this.sqlReport = sqlReport;
    this.objectSqlListReport = objectSqlListReport;
}

@Override
public List<ReportMasterPRPK> load(int first, int pageSize, String sortField, SortOrder sortOrder,
        Map<String, Object> filters) {

    page.setLimit(pageSize);
    page.setOffset(first);  

    if(this.sqlReport != null){
        this.sql = this.sqlReport;
        this.objectSqlList = this.objectSqlListReport;
    }else{          
        sql = "";
        objectSqlList = new ArrayList<>();
        //objectSqlList.clear();
    }

    if(flag != null){ //ini
        if(flag.equals("no selected")){ 
            sql = sql+" AND c.is_selected = 'n' ";
        }
    }

    if (filters != null){
        for(String key: filters.keySet()){
            String filterColumnName = "";
            for(Field field : ReportMasterPRPK.class.getDeclaredFields()){
                if(field.getName().equals(key)) filterColumnName = field.getAnnotation(Column.class).value();
            }
            if(filters.get(key) instanceof String){                         
                if("receivedDate".equals(key)){
                    if(((String)filters.get(key)).trim().length() > 20){
                        String startDate = "'" + filters.get(key).toString().substring(0, 10) + "'";
                        String endDate = "'" + filters.get(key).toString().substring(11, 21) + "'";
                        sql = sql + " AND " + filterColumnName + " BETWEEN " + startDate + " AND " + endDate+ " ";                          
                    }
                }else{
                    if(((String) filters.get(key)).trim().length() > 0){
                        sql = sql+"AND "+filterColumnName+" ILIKE ? ";
                        String value = "%"+filters.get(key)+"%";                    
                        objectSqlList.add(value);                       
                    }
                }

            }else{                  
                if(((String[]) filters.get(key)).length > 0){
                    sql = sql+" AND "+filterColumnName+" in ";
                    String value = "(";
                    for(String string : (String[]) filters.get(key)){
                        value = value+"'"+string+"',";
                    }
                    value = value.substring(0, value.length()-1)+") ";                                  
                    sql = sql + value;
                }
            }               
        }
    }

    if(sortField != null){
        for(Field field : ReportMasterPRPK.class.getDeclaredFields()){
            if(field.getName().equals(sortField)) columname = field.getAnnotation(Column.class).value();
        }
        if(sortOrder.toString().equals("ASCENDING")) order = " ASC";
        else order = " DESC";
        sql = sql+" GROUP a.prpk_number, a.prpk_type_id, a.created_date, a.pic_prpk_id, a.business_unit_id, a.pic_department_id, a.prpk_desc, a.prpk_request, a.prpk_background, a.prpk_analysis_benefit, a.priority_level_id, a.cost, b.prpk_type_name, c.business_unit, d.department_name, e.priority_name, f.user_name  ORDER BY "+columname+" "+order;
        System.out.println("sql sort: "+sql+" : "+objectSqlList.size());
    }else{
        sql = sql + order;
    }

    sqlReportPrint = sql;
    objectSqlListReportPrint = objectSqlList;
    this.listMasterPRPK = reportMasterPRPKQuery.retrivePage(page, sql, objectSqlList.toArray());

    int dataSize = reportMasterPRPKQuery.retrieveMaxRow(sql, objectSqlList.toArray());      
    this.setRowCount(dataSize);             

    //objectSqlList.clear();
    if(this.sqlReport != null){
        this.sql = this.sqlReport;
        this.objectSqlList = this.objectSqlListReport;
    }else{          
        sql = "";
        objectSqlList.clear();
    }

    order = "GROUP a.prpk_number, a.prpk_type_id, a.created_date, a.pic_prpk_id, a.business_unit_id, a.pic_department_id, a.prpk_desc, a.prpk_request, a.prpk_background, a.prpk_analysis_benefit, a.priority_level_id, a.cost, b.prpk_type_name, c.business_unit, d.department_name, e.priority_name, f.user_name ORDER BY a.created_date ";

    return listMasterPRPK;
}

public List<ReportMasterPRPK> calculateRownum(List<ReportMasterPRPK> listMasterPRPK, int first){
    int i = 1;
    for (ReportMasterPRPK masterPRPK : listMasterPRPK) {
        masterPRPK.setRownum(first + i);
        i++;
    }       
    return listMasterPRPK;
}

public String getSqlReportPrint() {
    return sqlReportPrint;
}

public void setSqlReportPrint(String sqlReportPrint) {
    this.sqlReportPrint = sqlReportPrint;
}

public List<Object> getObjectSqlListReportPrint() {
    return objectSqlListReportPrint;
}

public void setObjectSqlListReportPrint(List<Object> objectSqlListReportPrint) {
    this.objectSqlListReportPrint = objectSqlListReportPrint;
}

对不起,如果我的英语不好,但是我希望你能理解我的意思...

感谢...

0 个答案:

没有答案