繫結至已載入 ReportDocument 類別中的非內嵌報表

物件模型

這個報表繫結案例使用 ReportDocument (請參閱 「與 ReportDocument 物件模型繫結的報表」)。

報表位置

報表位於檔案目錄中。

說明

這個報表繫結案例允許您使用功能更強的 ReportDocument 物件模型處理非內嵌的報表 (專案的外部報表)。

「繫結至程式碼中的檔案目錄路徑」中,雖是藉由檔案目錄路徑字串繫結至報表,卻並非直接將目錄路徑繫結至檢視器,而是傳遞該字串給 ReportDocument.Load() 方法。如此便可以讓您使用 ReportDocument 物件模型。

Note附註

如需詳細資訊,請參閱 「我應該使用哪一個物件模型?」

此外,由於每個外部報表都會載入至通用類別 ReportDocument,您也可以開發一個報表選擇程序,共用所有報表的通用報表繫結程式碼。這類似於報表繫結案例繫結至向上轉型為「繫結至向上轉型為 ReportDocument 的內嵌報表類別」<實作>一節中的程式碼。

優點

  • 維護需求低:不需重新編譯應用程式,即可加入、移除或修改報表。
  • 進階的程式設計互動:提供方法存取功能強大的 ReportDocument 物件模型。
  • 透過程式碼共用而減少程式碼編寫:類似於使用報表繫結案例「繫結至向上轉型為 ReportDocument 的內嵌報表類別」,報表可以共用程式碼,因為所有報表都載入至通用 ReportDocument 類別。

缺點

  • 受限的散發:報表必須位於與應用程式相同的電腦上(從 Web 伺服器的 ASPNET 使用者帳戶對網路上其他伺服器的存取通常會受到限制)。
  • 額外的部署工作:報表必須以正確的相對路徑隨同應用程式一起散發。
  • 缺乏安全的報表來源:在執行階段,會有報表在部署電腦上改變位置或移除的風險。

使用 ReportDocument 物件模型繫結至非內嵌報表

Note附註

這個程序只能與「專案設定」一節中建立的專案搭配使用。「專案設定」包含特定命名空間參考以及本程序所需的程式碼組態;您必須具備這個組態,否則將無法完成此程序。因此,在您開始本程序前,必須先遵循「專案設定」中的步驟進行。

  1. 在 CrystalDecisions.CrystalReports.Engine 命名空間的類別上方,加入 "Imports" [Visual Basic] 或 "using" [C#] 陳述式。
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images\36bhtx7w.alert_note(zh-tw,VS.90).gif" alt="Note" class="note" />附註</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>請宣告此命名空間,以便存取 ReportDocument 類別時不需使用到命名空間前置字元。</p></td>
</tr>
</tbody>
</table>

``` vb
Imports CrystalDecisions.CrystalReports.Engine
```

``` csharp
using CrystalDecisions.CrystalReports.Engine;
```
  1. 在特定 Crystal Reports 版本的 General Business 子目錄中尋找 World Sales Report.rpt 檔案。如需範例報表的詳細資訊,請參閱「範例報表的目錄」

  2. 將包含 World Sales Report.rpt 的完整檔案目錄路徑複製到剪貼簿。

  3. 在 ConfigureCrystalReports() 方法 (「專案設定」 中建立) 內,宣告 reportPath 字串變數,並指派一個字串,其中包含上一步驟所複製的 World Sales Report 檔案目錄路徑。

    Dim reportPath As String = _
    "C:\Program Files\Microsoft Visual Studio 9.0\" _ & "Crystal Reports\Samples\zh-cht\Reports\General Business\" _ & "World Sales Report.rpt"
    
    string reportPath =
    "C:\\Program Files\\Microsoft Visual Studio 9.0\\" + "Crystal Reports\\Samples\\zh-cht\\Reports\\General Business\\" + "World Sales Report.rpt";
    
  4. 在字串宣告下方,宣告 ReportDocument 的執行個體。

    Dim myReportDocument As ReportDocument = New ReportDocument()
    
    ReportDocument reportDocument = new ReportDocument();
    
  5. 將字串變數(已設定為非內嵌報表的檔案目錄路徑) 載入 ReportDocument。

    myReportDocument.Load(reportPath)
    
    reportDocument.Load(reportPath);
    
  6. 將 ReportDocument 執行個體 (如今含有已載入的內嵌報表) 指派給 CrystalReportViewer 控制項的 ReportSource 屬性。

    myCrystalReportViewer.ReportSource = myReportDocument
    
    crystalReportViewer.ReportSource = reportDocument;
    
  7. 若要檢視報表,請建置並執行專案。

請參閱