翻譯|使用教程|編輯:凌霄漢|2022-04-11 15:56:48.100|閱讀 462 次
概述:本篇文章將為大家帶來報表開發工具FastReport.NET使用教程:如何在 Visual Basic 的 ASP .NET MVC 應用程序中創建報表。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Visual Basic .NET 編程語言被許多人視為入門級語言。但當然,事實并非如此。我們已經習慣知道 Visual Basic.NET 最常用于桌面應用程序。然而,這種語言允許做更多的事情——例如,在 ASP .NET 框架上創建 Web 應用程序。
ASP .NET 支持 MVC(模型-視圖-控制器)編程模式,與常規 ASP .NET 相比,它極大地簡化了應用程序擴展和測試。此模板有多種變體,但主要思想保持不變 - 界定業務邏輯和表示之間的責任區域。所有現代 Web 框架都建立在這種結構之上。因此,使用 Visual Basic .NET,您可以輕松地在 Angular 或任何其他框架中創建前端 Web 應用程序。
但是讓我們回到本文的主題——報告生成。報告生成程序在“2000 年代”之初就已經強勢進入我們的生活,而現在只有少數人敢于“從頭開始”創建報告。這應該是一份真正獨特的報告,超出任何發電機的能力。這聽起來很諷刺,但這種情況確實會發生。在使用報表生成器之前,生成報表的具體邏輯確實會成為障礙。但是,也許您只是不知道它們的所有可能性。例如,很少有人知道并使用從FastReport .NET報告生成器中的用戶應用程序代碼創建報告模板的能力。這種創建報告的方式為您提供了直接在程序代碼中控制模板結構和生成過程的獨特機會。
是的,這種報告生成方式需要對產品有很好的了解,即報告的結構、對象及其屬性。因此,開發商應具備足夠的資質。
讓我們停止這些爭論,開始創建一個演示程序,該程序從 VisualBasic 語言的 ASP .NET MVC Web 應用程序的代碼生成報告。
首先,您需要為此創建一個合適的項目。
Visual Studio 將為您精心創建一個現成的演示應用程序,并具有現成的結構。您所要做的就是連接 FastRport .NET 庫并添加用于創建報告的代碼。
讓我們將庫添加到項目的引用(References)中:FastReport.dll、FastReport.Web.dll。您將在已安裝的 FastReport .NET 程序的根目錄中找到這些庫。
讓我們使用一個現成的控制器來編寫主邏輯——HomeController:
‘Linking of necessary libraries Imports System.Drawing Imports FastReport Imports FastReport.Data Imports FastReport.Utils Imports FastReport.Web ‘altering 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
從注釋到代碼,很明顯我們正在創建報表對象,以及構建清晰的層次結構和依賴關系。例如,必須將所有帶區添加到頁面中,并且文本字段放置在帶區上或其他對象(例如表格或矩陣)內。重要的是不僅要建立依賴關系,而且要正確設置對象的屬性——它們的大小、相對于父對象的位置等等。所有這些細節都形成了一份報告。因此,此方法需要對 FastReport.NET 產品有很好的了解。
我們演示應用程序中的每個 Web 方法都已經有一個視圖。在 Views 文件夾中,找到 Index.vbhtml。將以下代碼行粘貼到方便的位置:
@ViewBag.WebReport.GetHtml()
在這里,我們正在執行將 Web 報表轉換為 HTML 的方法。當然,除了 html,它還會包含樣式、腳本、圖片——一般來說,是在網頁上顯示報告所需的一切。
在 Views 文件夾中有一個 Web.config 前端應用程序的配置文件。讓我們為其添加新的命名空間:
<namespaces>
<add namespace="FastReport" />
<add namespace="FastReport.Web" />
</namespaces>
在同一個文件夾中,還有另一個需要編輯的文件——Views/Home/Shared/_Layout.vbhtml。讓我們添加 FastReport 庫的腳本和樣式的使用:
<head>
@WebReportGlobals.Scripts()
@WebReportGlobals.Styles()
</head>
項目的根目錄下還有另一個 Web.config。在</system.web> 部分之后添加:
<system.webServer> <handlers> <add name="FastReportHandler" path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport"/> </handlers> </system.webServer>
我們的應用程序已準備好運行。讓我們確保它有效。
就是這樣:我們在程序代碼中創建了一個完整的報告。它看起來像是由特殊的視覺設計師創建的報告。但是,當然,這在代碼中更難做到。因此,我們建議僅在無法在報表內部實現復雜邏輯的特殊情況下使用此方法。
FastReport 技術交流群:702295239 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn