翻譯|使用教程|編輯:凌霄漢|2022-04-11 15:56:48.100|閱讀 462 次
概述:本篇文章將為大家?guī)韴?bào)表開發(fā)工具FastReport.NET使用教程:如何在 Visual Basic 的 ASP .NET MVC 應(yīng)用程序中創(chuàng)建報(bào)表。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Visual Basic .NET 編程語言被許多人視為入門級語言。但當(dāng)然,事實(shí)并非如此。我們已經(jīng)習(xí)慣知道 Visual Basic.NET 最常用于桌面應(yīng)用程序。然而,這種語言允許做更多的事情——例如,在 ASP .NET 框架上創(chuàng)建 Web 應(yīng)用程序。
ASP .NET 支持 MVC(模型-視圖-控制器)編程模式,與常規(guī) ASP .NET 相比,它極大地簡化了應(yīng)用程序擴(kuò)展和測試。此模板有多種變體,但主要思想保持不變 - 界定業(yè)務(wù)邏輯和表示之間的責(zé)任區(qū)域。所有現(xiàn)代 Web 框架都建立在這種結(jié)構(gòu)之上。因此,使用 Visual Basic .NET,您可以輕松地在 Angular 或任何其他框架中創(chuàng)建前端 Web 應(yīng)用程序。
但是讓我們回到本文的主題——報(bào)告生成。報(bào)告生成程序在“2000 年代”之初就已經(jīng)強(qiáng)勢進(jìn)入我們的生活,而現(xiàn)在只有少數(shù)人敢于“從頭開始”創(chuàng)建報(bào)告。這應(yīng)該是一份真正獨(dú)特的報(bào)告,超出任何發(fā)電機(jī)的能力。這聽起來很諷刺,但這種情況確實(shí)會(huì)發(fā)生。在使用報(bào)表生成器之前,生成報(bào)表的具體邏輯確實(shí)會(huì)成為障礙。但是,也許您只是不知道它們的所有可能性。例如,很少有人知道并使用從FastReport .NET報(bào)告生成器中的用戶應(yīng)用程序代碼創(chuàng)建報(bào)告模板的能力。這種創(chuàng)建報(bào)告的方式為您提供了直接在程序代碼中控制模板結(jié)構(gòu)和生成過程的獨(dú)特機(jī)會(huì)。
是的,這種報(bào)告生成方式需要對產(chǎn)品有很好的了解,即報(bào)告的結(jié)構(gòu)、對象及其屬性。因此,開發(fā)商應(yīng)具備足夠的資質(zhì)。
讓我們停止這些爭論,開始創(chuàng)建一個(gè)演示程序,該程序從 VisualBasic 語言的 ASP .NET MVC Web 應(yīng)用程序的代碼生成報(bào)告。
首先,您需要為此創(chuàng)建一個(gè)合適的項(xiàng)目。
Visual Studio 將為您精心創(chuàng)建一個(gè)現(xiàn)成的演示應(yīng)用程序,并具有現(xiàn)成的結(jié)構(gòu)。您所要做的就是連接 FastRport .NET 庫并添加用于創(chuàng)建報(bào)告的代碼。
讓我們將庫添加到項(xiàng)目的引用(References)中:FastReport.dll、FastReport.Web.dll。您將在已安裝的 FastReport .NET 程序的根目錄中找到這些庫。
讓我們使用一個(gè)現(xiàn)成的控制器來編寫主邏輯——HomeController:
‘Linking of necessary libraries Imports System.Drawing Imports FastReport Imports FastReport.Data Imports FastReport.Utils Imports FastReport.Web ‘a(chǎn)ltering the Index Function Index() As ActionResult Dim AppFolder As String Dim report As New WebReport() 'create instance of class Report Dim ds As New DataSet() 'create dataset object AppFolder = "C:\Users\FR\source\repos\WebAppVB\WebAppVB\App_Data" 'load data ds.ReadXml(AppFolder + "\nwind.xml") report.RegisterData(ds) report.Report.GetDataSource("Products").Enabled = True 'create report page Dim page As New ReportPage() report.Report.Pages.Add(page) 'add created page to report page collection page.CreateUniqueName() 'with generated name 'create group header band Dim group As New GroupHeaderBand() page.Bands.Add(group) 'add the band to band collection group.CreateUniqueName() 'with generated name group.Height = Units.Centimeters * 1 group.Condition = "[Products.ProductName].Substring(0,1)" 'set the group condition group.SortOrder = FastReport.SortOrder.Ascending 'and set sort order 'create text object Dim groupTxt As New TextObject() groupTxt.Parent = group 'set the object on whitch the text will be shown groupTxt.CreateUniqueName() groupTxt.Bounds = New RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 1) 'set the text object bounds groupTxt.Text = "[[Products.ProductName].Substring(0,1)]" 'set the text value groupTxt.Font = New Font("Arial", 14, FontStyle.Bold) 'set the font style groupTxt.VertAlign = VertAlign.Center ' set the text align groupTxt.Fill = New LinearGradientFill(Color.LightGoldenrodYellow, Color.Gold, 90, 0.5F, 1) 'set the text object fill 'create data band Dim data As New DataBand() group.Data = data 'set the group data data.CreateUniqueName() data.DataSource = report.Report.GetDataSource("Products") 'set data band source data.Height = Units.Centimeters * 0.5F 'set data band height 'create one more text object Dim productText As New TextObject() productText.Parent = data 'add the text object to data band productText.CreateUniqueName() productText.Bounds = New RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5F) 'set the text object bounds productText.Text = "[Products.ProductName]" 'set the text value 'create group footer band group.GroupFooter = New GroupFooterBand() group.GroupFooter.CreateUniqueName() group.GroupFooter.Height = Units.Centimeters * 1 'set the group footer height 'create total object Dim groupTotal As New Total() groupTotal.Name = "TotalRows" 'set total object name groupTotal.TotalType = TotalType.Count 'set total type groupTotal.Evaluator = data 'set the band for which the total will be calculated groupTotal.PrintOn = group.GroupFooter 'set the total place report.Report.Dictionary.Totals.Add(groupTotal) 'add the total object to totals collection 'create text object Dim totalText As New TextObject() totalText.Parent = group.GroupFooter 'set the object on whitch the text will be shown totalText.CreateUniqueName() totalText.Bounds = New RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5F) 'set the text object bounds totalText.Text = "Rows: [TotalRows]" 'set the text value totalText.HorzAlign = HorzAlign.Right 'set the text align totalText.Border.Lines = BorderLines.Top 'set the border lines type ViewBag.WebReport = report Return View() End Function
從注釋到代碼,很明顯我們正在創(chuàng)建報(bào)表對象,以及構(gòu)建清晰的層次結(jié)構(gòu)和依賴關(guān)系。例如,必須將所有帶區(qū)添加到頁面中,并且文本字段放置在帶區(qū)上或其他對象(例如表格或矩陣)內(nèi)。重要的是不僅要建立依賴關(guān)系,而且要正確設(shè)置對象的屬性——它們的大小、相對于父對象的位置等等。所有這些細(xì)節(jié)都形成了一份報(bào)告。因此,此方法需要對 FastReport.NET 產(chǎn)品有很好的了解。
我們演示應(yīng)用程序中的每個(gè) Web 方法都已經(jīng)有一個(gè)視圖。在 Views 文件夾中,找到 Index.vbhtml。將以下代碼行粘貼到方便的位置:
@ViewBag.WebReport.GetHtml()
在這里,我們正在執(zhí)行將 Web 報(bào)表轉(zhuǎn)換為 HTML 的方法。當(dāng)然,除了 html,它還會(huì)包含樣式、腳本、圖片——一般來說,是在網(wǎng)頁上顯示報(bào)告所需的一切。
在 Views 文件夾中有一個(gè) Web.config 前端應(yīng)用程序的配置文件。讓我們?yōu)槠涮砑有碌拿臻g:
<namespaces>
<add namespace="FastReport" />
<add namespace="FastReport.Web" />
</namespaces>
在同一個(gè)文件夾中,還有另一個(gè)需要編輯的文件——Views/Home/Shared/_Layout.vbhtml。讓我們添加 FastReport 庫的腳本和樣式的使用:
<head>
@WebReportGlobals.Scripts()
@WebReportGlobals.Styles()
</head>
項(xiàng)目的根目錄下還有另一個(gè) Web.config。在</system.web> 部分之后添加:
<system.webServer> <handlers> <add name="FastReportHandler" path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport"/> </handlers> </system.webServer>
我們的應(yīng)用程序已準(zhǔn)備好運(yùn)行。讓我們確保它有效。
就是這樣:我們在程序代碼中創(chuàng)建了一個(gè)完整的報(bào)告。它看起來像是由特殊的視覺設(shè)計(jì)師創(chuàng)建的報(bào)告。但是,當(dāng)然,這在代碼中更難做到。因此,我們建議僅在無法在報(bào)表內(nèi)部實(shí)現(xiàn)復(fù)雜邏輯的特殊情況下使用此方法。
FastReport 技術(shù)交流群:702295239 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn