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