在 Lightswitch HTML 客户端中使用 Visual Studio 2013 创建 .rdlc 报表
本文介绍了如何在不使用 WCF RIA Services 的情况下,在 Lightswitch HTML 客户端中简单创建 .rdlc 报表。
引言
本文介绍了如何使用 Visual Studio 2013 在 Lightswitch HTML 客户端中创建 .rdlc 报表。本文灵感来源于 Michael Washington 关于“在 Lightswitch HTML 客户端中创建报表”的文章。在他的文章中,他使用 Visual Studio 2012 通过 WCF RIA Services 生成了报表数据。然而,使用 Visual Studio 2013 为 Lightswitch 项目创建报表更加容易,可以使用 ServerApplicationContext
和 Visual Studio 内置的一些方法。本文使用的一些想法来源于 Michael Washington 的上述文章,因此我在此感谢他。
你需要的东西
运行此代码需要 Visual Studio 2013。
最终报表将如下所示
A. 设计表
本文将创建的报表来自假设的 LOB(行业务)数据,涉及股票的接收。有两个表。第一个是 Products
表,仅包含 ProductName
字段。第二个是 ProductStocks
表,包含如下字段:
B. 在服务器项目中创建报表文件夹
在服务器项目中创建下图所示的文件夹。
-
右键单击 ReportSource 文件夹。单击新建项,然后从新建项的数据菜单中选择数据集,并将其命名为 ReportSource.xsd 或您喜欢的任何名称。
-
将
DataTable
从工具箱拖到 .xsd 设计器上,并为其命名。然后,为要创建的报表添加所需的字段作为列,如下所示:注意:.xsd 文件将在 ReportSource.Designer.vb 或 ReportSource.Designer.cs 文件中生成类,我们将仅使用
ProductSummaryDataTable
类、ProductSummaryRow
类和AddProductSummaryRow
方法。下面是Dataset
设计器生成的代码。Partial Public Class ProductSummaryDataTable Inherits Global.System.Data.TypedTableBase(Of ProductSummaryRow) ......... Public Overloads Function AddProductSummaryRow(ByVal ProductName As String, ByVal TotalStock As Integer, ByVal TotalQuantitySold As Integer, ByVal TotalQuantityReturned As Integer, ByVal Balance as Integer, ByVal Status As String) As ProductSummaryRow .......
-
在 Reports 文件夹中添加一个 .rdlc 报表。请勿使用报表向导。
-
从报表的工具箱中,向报表设计器添加一个表格,屏幕上将出现“数据集属性”对话框。为数据集命名 (1),并在数据源框 (2) 中选择 ReportSource.xsd 数据集。然后,在可用数据集框 (3) 中选择
ProductSummary
表。表格的列将显示在字段 (4) 框中,如下所示。单击确定按钮。 - 设计您的报表,并根据需要添加任意多的功能,如下所示:1- 表格报表;2- 条形图报表;3- 饼图报表
C. 将 .rdlc 文件包含在生成中
-
右键单击项目文件并选择“编辑项目文件”。
-
将光标移到屏幕上出现的文件的末尾。您会看到一个
<Build File Include=”…./>
部分。复制并粘贴另一部分下的部分,并在其中添加服务器上 .rdlc 文件的位置,如下所示:<_BuidFile Include="LSReportApp.Server\default.aspx"> <SubFolder> </SubFolder> <PublishType> </PublishType> </_BuildFile> <_BuidFile Include="LSReportApp.Server\Reports\ProductSummaryReport.rdlc"> <SubFolder> </SubFolder> <PublishType> </PublishType> </_BuildFile>
保存并关闭文件。然后再次右键单击
Project
文件并选择重新加载项目文件。
D. 创建报表的 .aspx 页面并编写代码生成报表记录
-
右键单击 ReportAspx 文件夹并添加一个 Web 窗体;将其命名为 ProductStockSummary.aspx。
-
将
ReportViewer
控件从工具箱添加到<div/>
元素中,并将其命名为ProductStockReportViewer
。设置您需要的必要属性。 -
在
<div/>
元素下方添加一个ScriptManager
。
注意:.aspx.desiner.vb 或 .cs 可能会要求您包含 reportviewer 文件的文件属性。只需打开提示并单击要添加的文件。这将添加 reportviewer
和 scriptmanager
工作所需的必要程序集。
-
右键单击 aspx 页面并选择“查看代码”。
-
遵循下面代码的模式:
Imports Microsoft.Reporting.WebForms
Imports ReportSource
'In the PageLoad Event
Public Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack() Then
ShowReport()
End If
End Sub
'In the ShowReport Method, Load the .rdlc report and Load the report data
Private Sub ShowReport()
'Load the .rdlc report as a local report
Me.ProductStockReportViewer.ProcessingMode = ProcessingMode.Local
Me.ProductStockReportViewer.LocalReport.ReportPath = _
Server.MapPath("~/ProductSummaryReport.rdlc")
'Load the report data
Me.ProductStockReportViewer.LocalReport.DataSources.Add( _
New ReportDataSource("ProductSummaryDataSet", LoadDataList()))
'The LoadDataList Method is where you use ServerApplicationContext to access your
'product stock table and process your summary for the report. Then add the summary data
'to the ProductSummaryRow using the AddProductSummaryRow() method
'of the ProductSummaryDataTable.
Private Function LoadDataList() As List(Of ProductSummaryRow)
Using ctx As ServerApplicationContext = ServerApplicationContext.CreateContext
'Get the Product Stock Query as IDataServiceQueryable.
'This makes fetching of record very fast
Dim pStock As IDataServiceQueryable(Of ProductStock) = _
ctx.DataWorkspace.ApplicationData.ProductStocks
If pStock IsNot Nothing AndAlso pStock.Count > 0 Then
'Get All the product names as a list of String
Dim ProductNameList As List(Of String) = _
ctx.DataWorkspace.ApplicationData.Products.Select_
(Function(a) a.ProductName).Execute().ToList()
If ProductNameList IsNot Nothing AndAlso ProductNameList.Count > 0 Then
Dim ProductSummaryList As New List(Of ProductSummaryRow)
'Iterate through the ProductName List
For Each ProductName In ProductNameList
'Instantiate the summary variables
Dim Product As String = Nothing
Dim TotalStock As Integer = 0
Dim TotalQuantitySold As Integer = 0
Dim TotalQuantityRtd As Integer = 0
Dim StockBalance As Integer = 0
Dim StockStatus As String = Nothing
'Set the variables
Product = productName
TotalStock = pStock.Sum(Function(a) a.Product.ProductName.Trim = productName.Trim _
And a.QuantityInStock)
TotalQuantitySold = pStock.Sum(Function(a) a.ProductName.Trim = productName.Trim _
And a.QuantitySold)
TotalQuantityRtd = pStock.Sum(Function(a) a.ProductName.Trim = productName.Trim _
And a.QuantityReturned)
StockBalance = TotalStock - TotalQuantitySold + TotalQuantityRtd
If StockBalance >= 2000 Then
StockStatus = "Active"
ElseIf StockBalance > 1000 And StockBalance < 2000 Then
StockStatus = "Reorder Stock"
Else
StockStatus = "Critical Stock"
End If
'Add the summary variables to the data-row of the table in the dataset
Dim SummaryRow As ProductSummaryRow = _
New ProductSummaryDataTable().AddProductSummaryRow( _
Product, TotalStock, TotalQuantitySold, TotalQuantityRtd, StockBalance, StockStatus)
If Not SummaryRow.HasErrors Then
ProductSummaryList.Add(SummaryRow)
End If
Next
If ProductSummaryList.Count>0
Return ProductSummaryList
End If
End If
End If
End Using
Return Nothing
End Function
注意:用于生成报表摘要的代码可以是:
- 一个单独的类,用于生成列表中的摘要记录。
- 一个单独的
Controller
类,用于生成列表中的摘要记录。 - 一个 WCF RIA 服务,用于生成摘要记录。
因此,您可以创建一个带有 GetProductSummary
等方法的单独类,并在 LoadDataList
函数中调用此方法,然后将摘要变量分配给 DataTable
的 AddProductSummaryRow
方法。
E. 查看您的报表
- 创建
Products
表的屏幕,并向Products
表中添加一些记录。 - 创建
ProductStock
表的屏幕,并向表中添加一些记录。 - 在
ProductStock
的浏览屏幕上,在产品库存行布局下创建一个新的选项卡行布局,并将自定义控件添加到行布局中,如下所示: - 单击自定义控件属性上的“编辑呈现代码”并在呈现执行函数中写入下面代码:
myapp.BrowseProductStocks.ScreenContent_render = function (element, contentItem){ //Show Loading Message var HtmlContent = $("<div></div>").html("<object width='800px' height='800px' data='../reportsAspx/ProductStockSummary.aspx' />"); HtmlContent.appendTo($(element)); };
运行您的应用程序并浏览到报表页面。您可以将报表导出为 PDF、Word 或 Excel,如下所示:
致谢
我想感谢 Micheal Worshinton 的文章启发我写这篇文章,感谢 Beth Massy 的“分享美好”系列文章,以及 Lightswitch 社区和论坛对论坛上提问的各种解答。
历史
- 2014 年 8 月 3 日:初始版本