表开发工具FastReport.NET使用教程:如何在 Visual Basic 的 ASP .NET MVC 应用程序中创建 表

本篇文章将为大家带来 表开发工具FastReport.NET使用教程:如何在 Visual Basic 的 ASP .NET MVC 应用程序中创建 表。

Visual Basic .NET 编程语言被许多人视为入门级语言。但当然,事实并非如此。我们已经习惯知道 Visual Basic.NET 最常用于桌面应用程序。然而,这种语言允许做更多的事情——例如,在 ASP .NET 框架上创建 Web 应用程序。

ASP .NET 支持 MVC(模型-视图-控制器)编程模式,与常规 ASP .NET 相比,它极大地简化了应用程序扩展和测试。此模板有多种变体,但主要思想保持不变 – 界定业务逻辑和表示之间的责任区域。所有现代 Web 框架都建立在这种结构之上。因此,使用 Visual Basic .NET,您可以轻松地在 Angular 或任何其他框架中创建前端 Web 应用程序。

是的,这种 告生成方式需要对产品有很好的了解,即 告的结构、对象及其属性。因此,开发商应具备足够的资质。

让我们停止这些争论,开始创建一个演示程序,该程序从 VisualBasic 语言的 ASP .NET MVC Web 应用程序的代码生成 告。

首先,您需要为此创建一个合适的项目。

 表开发工具FastReport.NET使用教程:如何在 Visual Basic 的 ASP .NET MVC 应用程序中创建 表

Visual Studio 将为您精心创建一个现成的演示应用程序,并具有现成的结构。您所要做的就是连接 FastRport .NET 库并添加用于创建 告的代码。

让我们将库添加到项目的引用(References)中:FastReport.dll、FastReport.Web.dll。您将在已安装的 FastReport .NET 程序的根目录中找到这些库。

让我们使用一个现成的控制器来编写主逻辑——HomeController:

‘Linking of necessary librariesImports System.DrawingImports FastReportImports FastReport.DataImports FastReport.UtilsImports FastReport.Web‘altering the IndexFunction 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:UsersFRsourcereposWebAppVBWebAppVBApp_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>
<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.NET使用教程:如何在 Visual Basic 的 ASP .NET MVC 应用程序中创建 表

就是这样:我们在程序代码中创建了一个完整的 告。它看起来像是由特殊的视觉设计师创建的 告。但是,当然,这在代码中更难做到。因此,我们建议仅在无法在 表内部实现复杂逻辑的特殊情况下使用此方法。


FastReport 技术交流群:702295239    欢迎一起进群讨论

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年3月8日
下一篇 2022年3月8日

相关推荐

发表回复

登录后才能评论