2013-01-22 44 views
0

我已在桌面應用程序中成功地將Java Crystal Reports與Crystal Report連接起來。 現在我想以編程方式向報告添加一行。通過Java SDK將CrystalObject添加到Crystal Report

我試着用下面的代碼。

try { 
    ReportDefController rdc = reportClientDoc.getReportDefController(); 
} catch (ReportSDKException ex) { 
    Logger.getLogger(PurchaseReport.class.getName()).log(Level.SEVERE, null, ex); 
} 

Tables tables = null; 
try { 
    tables = reportClientDoc.getDatabase().getTables(); 
} catch (ReportSDKException ex) { 
    Logger.getLogger(PurchaseReport.class.getName()).log(Level.SEVERE, null, ex); 
} 

LineObject lineObject = new LineObject(); 
lineObject.clone(false); 
lineObject.setSectionCode(0); 
lineObject.setLineThickness(10); 
lineObject.setLeft(50); 
lineObject.setWidth(50); 
lineObject.setHeight(10); 
lineObject.setRight(20); 
lineObject.setTop(10); 
lineObject.setLineColor(Color.BLUE); 

ReportObjectController roc = null; 
try { 
    roc = reportClientDoc.getReportDefController().getReportObjectController(); 
} catch (ReportSDKException ex) { 
    Logger.getLogger(PurchaseReport.class.getName()).log(Level.SEVERE, null, ex); 
} 

ReportDefController reportDefController = null; 
try { 
    reportDefController = reportClientDoc.getReportDefController(); 
    ISection section = reportDefController.getReportDefinition().getDetailArea().getSections().getSection(0); 
    lineObject.setSectionName(section.getName()); 
    //roc.add(fieldObject, section, 0); 
    if(roc.canAddReportObject(lineObject, section)) 
    { 
     roc.add(lineObject, section, -1); 
    } 
} catch (ReportSDKException ex) { 
    Logger.getLogger(PurchaseReport.class.getName()).log(Level.SEVERE, null, ex); 
} 

這將引發在roc.add(lineObject, section, -1)

錯誤我怎樣才能解決這個錯誤,並適當加行?

回答

0

行和框的添加與使用SDK的常規報表對象略有不同,因爲它們可以跨越部分/區域。您需要指定'bottom'(或'right')和'endSection'屬性,其中'bottom'是距離結尾部分頂部的偏移量。高度和寬度屬性對這些繪圖對象(使用「右」與「左」地組合寬度)

到您的問題沒有影響: 首先,你要刪除的通話setHeight & setWidth

然後,嘗試你最後try塊內的類似下面的代碼:

reportDefController = reportClientDoc.getReportDefController(); 
ISection section = reportDefController.getReportDefinition().getDetailArea().getSections().getSection(0); 
lineObject.setSectionName(section.getName()); 

// BEGIN ADDED CODE 

// specify section you want line to end in 
lineObject.setEndSectionName(section.getName()); 

// how far down in the end section you want your line object - if you want a horizontal line, use setRight instead 
lineObject.setBottom(100);      

// specify some style so it appears (I think default is noLine) 
lineObject.setLineStyle(LineStyle.singleLine); 


// END ADDED CODE 

if(roc.canAddReportObject(lineObject, section)) { 
    roc.add(lineObject, section, -1); 
} 

這將增加始於10(根據您的代碼機頂盒調用),並在100結束的小豎線在第一個細節部分。您可能需要調整數字和定位以滿足您的需求。

+0

謝謝@Chris。它完全適用於我。 – chintan