原創|使用教程|編輯:龔雪|2016-05-09 17:40:07.000|閱讀 984 次
概述:在這篇文章中,主要講述FastReport .Net直接從用戶應用程序中創建報表的能力,如果你不想產生一堆個人文件的報告模板或要在應用程序內隱藏一個報告模板,以避免損壞或修改模板,它就派上大用場了。此外,你可以根據應用邏輯來完全掌控self-report的創建、改變報表對象的外觀。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
FastReport .Net是一款強大且應用十分廣泛的報表工具,我最喜歡的特點之一是它直接從用戶應用程序中創建報表的能力。在這篇文章中,我們來看看這個功能的例子,當你不需要一堆的.exe文件的時候它就能派上用場了。此外,你可以根據應用邏輯來完全掌控self-report的創建、改變報表對象的外觀。
首先,我將展示從用戶應用程序的代碼中構建報表和在特殊設計器中報表模板經典開發的區別。
通常情況下,報表生成器提供了一個特殊的設計器來設計報表模板。這可能是IDE的組件或僅僅是外部程序。開發人員將組件放在報表的頁上,并指定它們的屬性,這類似于在窗體應用程序中設計表單。
除了這些經典的方法來創建一個報表模板外,FastReport允許你在應用程序代碼的幫助下使用相同的組件來創建模板,你同樣能夠創建報表對象并添加組件以及配置數據源。實踐表明,從代碼中創建報表比在可視化設計器中時間會稍稍長一點,但有趣的是,這樣得到的報表模板可以在同一個可視化編輯器(設計器)中查看并保存到文件中。
讓我們來看看例子。
用C#語言創建Windows窗體應用程序(當然你應該先安裝FastReport .Net),在表單上放置一個按鈕來啟動報表。接下來,我要說的是,我們不僅僅要在預覽模式下展示報表,還要讓它導出到PDF文件。因此添加復選框:
創建一個按鈕單擊事件處理程序,這里是整個應用程序的代碼。
首先,添加應用到FastReport.dll(在FastReport .Net包中)。
同樣,添加FastReport庫、 FastReport.Utils以及FastReport.Data。
創建報表示例:
private void RunBtn_Click(object sender, EventArgs e) { //Create instance of class Report Report report = new Report(); }
我們的報表將從數據庫中展示數據,因此需要創建數據源:
//load data DataSet ds = new DataSet(); ds.ReadXml(AppFolder + "\\nwind.xml");
現在需要在報表中注冊數據源:
//Register data source report.RegisterData(ds);
要使用已注冊的數據源表,你需要給它授權:
//Enable data table report.GetDataSource("Products").Enabled = true;
籌備工作做好了,現在轉移到報表模板的創建上來,創建報表頁面:
//Add report page ReportPage page = new ReportPage();
并將其添加到報表中:
report.Pages.Add(page);
報表的所有對象都需要有一個特定的名稱,你可以用他們自帶的并指定屬性名稱,或者你可以使用一個函數生成一個唯一的名稱:
page.CreateUniqueName();
準備填充報表頁面。創建一個“Group Header”帶(band):
//Create GroupHeader band GroupHeaderBand group = new GroupHeaderBand();
并將帶添加到頁面:
page.Bands.Add(group); group.CreateUniqueName();
設置帶的高度:
group.Height = Units.Centimeters * 1;
分組條件和排序順序:
group.Condition = "[Products.ProductName].Substring(0,1)"; group.SortOrder = FastReport.SortOrder.Ascending;
現在用數據填充帶。要做到這一點,從數據源創建一個文本對象以引用該字段:
// create group text TextObject groupTxt = new TextObject();
重要的父參數指示該帶,放置文本對象:
groupTxt.Parent = group; groupTxt.CreateUniqueName();
設置文本對象的大小和范圍:
groupTxt.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 1);
設置文本大小和范圍:
groupTxt.Font = new Font("Arial", 14, FontStyle.Bold);
其他設置視文本的外觀而定:
groupTxt.Text = "[[Products.ProductName].Substring(0,1)]"; groupTxt.VertAlign = VertAlign.Center; groupTxt.Fill = new LinearGradientFill(Color.LightGoldenrodYellow, Color.Gold, 90, 0.5f, 1);
現在最有趣的部分是創建帶“數據”:
// create data band DataBand data = new DataBand();
為該組分配數據帶:
group.Data = data; data.CreateUniqueName();
為帶“數據”指定數據源:
data.DataSource = report.GetDataSource("Products"); data.Height = Units.Centimeters * 0.5f;
在這里你可以通過屬性過濾器設置過濾器帶。現在用文本對象填充帶:
// create product name text TextObject productText = new TextObject(); productText.Parent = data; productText.CreateUniqueName(); productText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f); productText.Text = "[Products.ProductName]";
組頁腳最適宜特定的分組實例,即使有多個組頭也不會混淆。創建組頁腳:
// create group footer group.GroupFooter = new GroupFooterBand(); group.GroupFooter.CreateUniqueName(); group.GroupFooter.Height = Units.Centimeters * 1;
向組頁腳中添加總數,它將顯示該組的產品計數:
// create total Total groupTotal = new Total(); groupTotal.Name = "TotalRows";
設置類型計算,計算所用的帶,以及顯示結果的帶,因為我們定義了項目的數量就不需要指定特定字段來計算(使用groupTotal.Expression)。
groupTotal.TotalType = TotalType.Count; groupTotal.Evaluator = data; groupTotal.PrintOn = group.GroupFooter;
我們需要在報表匯總字典中添加總計的總數。注冊:
report.Dictionary.Totals.Add(groupTotal);
像任何表達式展示那樣,結果會通過文本對象展示:
// show total in the group footer TextObject totalText = new TextObject(); totalText.Parent = group.GroupFooter; totalText.CreateUniqueName(); totalText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f); totalText.Text = "Rows: [TotalRows]"; totalText.HorzAlign = HorzAlign.Right; totalText.Border.Lines = BorderLines.Top;
報表準備好了,現在我們可以顯示它或者在設計器里面運行,并且你可以立即將其導出以自己想要的數據格式。讓我們用一個復選框,添加到窗體:
if (PDFCheckBox.Checked) { report.Prepare(); FastReport.Export.Pdf.PDFExport export = new FastReport.Export.Pdf.PDFExport(); export.Export(report); } else report.Show();
如果復選框被選中,你會獲得對話框保存的PDF文件,反之,該報表將在預覽模式下運行。這里值得注意的是,即使不顯示對話框,也可以進行導出,像某種“安靜模式”。導出會像這樣:
export.Export(report, @"C:\Temp\ReportFromCode.pdf");
第一選項-報表示例以及第二選項-結果文件。
我們得到的結果:
//Create instance of class Report Report report = new Report(); //load data DataSet ds = new DataSet(); ds.ReadXml(AppFolder + "\\nwind.xml"); //Register data source report.RegisterData(ds); //Enable data table report.GetDataSource("Products").Enabled = true; //Add report page ReportPage page = new ReportPage(); report.Pages.Add(page); page.CreateUniqueName(); //Create GroupHeader band GroupHeaderBand group = new GroupHeaderBand(); page.Bands.Add(group); group.CreateUniqueName(); group.Height = Units.Centimeters * 1; group.Condition = "[Products.ProductName].Substring(0,1)"; group.SortOrder = FastReport.SortOrder.Ascending; // create group text TextObject groupTxt = new TextObject(); groupTxt.Parent = group; groupTxt.CreateUniqueName(); groupTxt.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 1); groupTxt.Text = "[[Products.ProductName].Substring(0,1)]"; groupTxt.Font = new Font("Arial", 14, FontStyle.Bold); groupTxt.VertAlign = VertAlign.Center; groupTxt.Fill = new LinearGradientFill(Color.LightGoldenrodYellow, Color.Gold, 90, 0.5f, 1); // create data band DataBand data = new DataBand(); group.Data = data; data.CreateUniqueName(); data.DataSource = report.GetDataSource("Products"); data.Height = Units.Centimeters * 0.5f; // create product name text TextObject productText = new TextObject(); productText.Parent = data; productText.CreateUniqueName(); productText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f); productText.Text = "[Products.ProductName]"; // create group footer group.GroupFooter = new GroupFooterBand(); group.GroupFooter.CreateUniqueName(); group.GroupFooter.Height = Units.Centimeters * 1; // create total Total groupTotal = new Total(); groupTotal.Name = "TotalRows"; groupTotal.TotalType = TotalType.Count; groupTotal.Evaluator = data; groupTotal.PrintOn = group.GroupFooter; report.Dictionary.Totals.Add(groupTotal); // show total in the group footer TextObject totalText = new TextObject(); totalText.Parent = group.GroupFooter; totalText.CreateUniqueName(); totalText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f); totalText.Text = "Rows: [TotalRows]"; totalText.HorzAlign = HorzAlign.Right; totalText.Border.Lines = BorderLines.Top; if (PDFCheckBox.Checked) { report.Prepare(); FastReport.Export.Pdf.PDFExport export = new FastReport.Export.Pdf.PDFExport(); export.Export(report); //export.Export(report, @"C:\Temp\ReportFromCode.pdf"); } else report.Show();
報表本身:
總結一下,從代碼中創建報表是FastReport .Net的又一令人驚訝的功能。什么時候有用呢?如果你不想產生一堆個人文件的報告模板或要在應用程序內隱藏一個報告模板,以避免損壞或修改模板。同樣的它在應用程序的執行過程中更改報表模板也非常方便,這賦予了報表很大的靈活性以及使用單個模板的能力、根據程序邏輯修改它的能力。
希望此功能也可以為大家帶來便利,提升工作效率。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn