在本文中,我將向你展示如何使用應用程序的代碼,將兩個或者更多的報表合並到一個報表中。當你想根據類別合並類似的報表時,這可能很有用。
要將報表附加到上一個,請使用報表對象的Prepare方法。必須提供TRUE值作為該方法的參數。我們看看下面這個例子。
創建一個WinForms應用程序。在項目中添加對FastReport.dll庫的引用。
接下來,在表單中添加三個按鈕:報表1、報表2、組合報表。然后雙擊第一個按鈕。
現在我們使用FastReport庫:
using FastReport;
設置報表路徑:
string report_path = @"K:\MyDocuments\";
現在添加第一個按鈕的代碼:
private void button1_Click(object sender, EventArgs e)
{
Report report1 = new Report();
Report1.Load(report_path + "report1.frx");
Report1.Prepare();
Report1.ShowPrepared();
}
在這里,我們創建了一個報表對象的實例,加載報表,准備報表並顯示。報表模板如下所示:

雙擊第二個按鈕:
private void button2_Click(object sender, EventArgs e)
{
Report report2 = new Report();
Report2.Load(report_path+"report2.frx");
Report2.Prepare();
Report2.ShowPrepared();
}
接下來的流程和第一個按鈕一樣。報表模板也是類似的:

我們添加第三個按鈕的代碼:
private void button3_Click(object sender, EventArgs e)
{
Report report1 = new Report();
report1.Load(report_path + "report1.frx");
report1.Prepare();
report1.Load(report_path + "report2.frx");
report1.Prepare(true);
report1.ShowPrepared();
}
如你所見,我們創建了報表對象“report1”。接下來,我們加載第一份報表並做好准備。然后加載第二份報表。在最后一行代碼中,我們顯示了報表對象。注意行report1.Prepare(true)。我們將true的值傳遞給函數參數。這意味着當前的報表將附在前一份報表上。而且,只要你喜歡,你可以合並任意數量的報表。
現在啟動我們的應用程序:

如果我們點擊Report 1按鈕,我們會獲得第一個報表:

按下按鈕Report 2.我們得到第二個報表:

最后,我們按下第三個按鈕:

在這種情況下,我們得到了一份有兩頁的報表。第一頁顯示第一個報表,第二顯示第二個。就是這樣,將兩個報表合並為一個報表不會有任何困難,並且不會創建額外的報表對象。
