在本文中,将探讨如何使用Visual Studio2013创建LightswitchHTML客户端的报告。将从设计表格开始,然后创建报告文件夹,接着包含.rdlc文件,并编写代码来生成报告记录。最后,将学习如何查看报告。
将创建一个假设的业务线(LOB)数据报告,涉及股票数据。报告涉及两个表格。第一个是“产品”表,仅包含“产品名称”字段。第二个是“产品库存”表,包含以下字段:
在服务器项目中创建以下文件夹结构。右键点击“ReportSource”文件夹,选择“新建项目”,然后从“数据”菜单中选择“数据集”,并将其命名为“ReportSource.xsd”或喜欢的其他名称。
从工具箱中拖动一个“DataTable”到.xsd设计器,并为其命名。然后添加所需的字段作为报告的列,如下所示:
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
' ...
End Function
End Class
右键点击项目文件,选择“编辑项目文件”。将光标移动到屏幕上显示的文件末尾。将看到一个<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>
保存文件并关闭它。然后再次右键点击项目文件,选择“重新加载项目文件”。
右键点击“ReportAspx”文件夹,添加一个Web表单;将其命名为“ProductStockSummary.aspx”。从工具箱中添加一个“ReportViewer”控件到元素,并将其命名为“ProductStockReportViewer”。设置需要的属性。
在元素下方添加一个“ScriptManager”。注意:.aspx.desiner.vb或.cs可能会要求包含reportviewer文件的属性。只需打开提示并点击文件以添加。这将添加reportviewer和scriptmanager所需的必要程序集以工作。
右键点击aspx页面,选择“查看代码”。按照以下代码模式:
Imports Microsoft.Reporting.WebForms
Imports ReportSource
Public Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack() Then
ShowReport()
End If
End Sub
Private Sub ShowReport()
Me.ProductStockReportViewer.ProcessingMode = ProcessingMode.Local
Me.ProductStockReportViewer.LocalReport.ReportPath = _
Server.MapPath("~/ProductSummaryReport.rdlc")
Me.ProductStockReportViewer.LocalReport.DataSources.Add( _
New ReportDataSource("ProductSummaryDataSet", LoadDataList()))
End Sub
Private Function LoadDataList() As List(Of ProductSummaryRow)
Using ctx As ServerApplicationContext = ServerApplicationContext.CreateContext
Dim pStock As IDataServiceQueryable(Of ProductStock) = _
ctx.DataWorkspace.ApplicationData.ProductStocks
If pStock IsNot Nothing AndAlso pStock.Count > 0 Then
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)
For Each ProductName In ProductNameList
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
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
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 Then
Return ProductSummaryList
End If
End If
End If
End Using
Return Nothing
End Function
为“产品”表创建屏幕,并添加一些记录到“产品”表。为“产品库存”表创建屏幕,并添加一些记录到该表。在“产品库存”的浏览屏幕上,创建一个新的标签行布局在“产品库存行布局”下,并添加自定义控件到行布局,如下所示:
myapp.BrowseProductStocks.ScreenContent_render = function(element, contentItem){
//Show Loading Message
var HtmlContent = $(
""
).html(
""
);
HtmlContent.appendTo($(element));
};