Advanced Linq (VB.Net / netframework)
Note
This demo is available in your FlexCel installation at <FlexCel Install Folder>\samples\vb\VS2022\netframework\20.Reports\22b.Advanced Linq and also at https://github.com/tmssoftware/TMS-FlexCel.NET-demos/tree/master/vb/VS2022/netframework/Modules/20.Reports/22b.Advanced Linq
Overview
THIS DEMO REQUIRES .NET 3.5 OR NEWER.
Most of the demos here use datasets as datasources. This is just for convenience, because Linq is not supported in .NET 2.0 and so if we used Linq those demos wouldn't work for everybody, and also because the focus is in the Excel templates, not so much in the data layer. But you can use any IQueryable<T> collection as a datasource in a FlexCel report, and this is what we will show here.
This demo shows some features not shown in the Linq example.
Concepts
How to do a master-detail report when the details are nested many levels inside the master. In this case, the class Country has a People class, and the People class has a list of Language objects. If People was a List<> inside Country and you wanted to use that list, you would just define a __People__ band (this is shown in the Linq example). But as the List<> is inside People which in turn is inside Country, you need to define a __People.Language__ band.
How to reference a table with dots using [square brackets]. If you write in a cell <#tablename.section.field> FlexCel will interpret this as table "tablename", field "section.field". The text up to the first dot is always the table, and the rest is the field. But sometimes you might want this to being interpreted as table "tablename.section", field "field". To do so, you need to write <#[tablename.section].field>. In this particular case, we have a table "People.Language" which we defined in the previous point. If we wrote in cell B1: "<#people.language.speakers.percent> FlexCel would interpret this is the table "people", not "people.language" which is what we need. To make FlexCel understand that we want a table "people.language" we use <#[people.language].speakers.percent>
Files
AssemblyInfo.vb
Imports System.Reflection
Imports System.Runtime.CompilerServices
'
' General Information about an assembly is controlled through the following
' set of attributes. Change these attribute values to modify the information
' associated with an assembly.
'
<Assembly: AssemblyTitle("")>
<Assembly: AssemblyDescription("")>
<Assembly: AssemblyConfiguration("")>
<Assembly: AssemblyCompany("")>
<Assembly: AssemblyProduct("")>
<Assembly: AssemblyCopyright("(c) 2002 - 2014 TMS Software")>
<Assembly: AssemblyTrademark("")>
<Assembly: AssemblyCulture("")>
'
' Version information for an assembly consists of the following four values:
'
' Major Version
' Minor Version
' Build Number
' Revision
'
' You can specify all the values or you can default the Revision and Build Numbers
' by using the '*' as shown below:
<Assembly: AssemblyVersion("6.2.1.0")>
'
' In order to sign your assembly you must specify a key to use. Refer to the
' Microsoft .NET Framework documentation for more information on assembly signing.
'
' Use the attributes below to control which key is used for signing.
'
' Notes:
' (*) If no key is specified, the assembly is not signed.
' (*) KeyName refers to a key that has been installed in the Crypto Service
' Provider (CSP) on your machine. KeyFile refers to a file which contains
' a key.
' (*) If the KeyFile and the KeyName values are both specified, the
' following processing occurs:
' (1) If the KeyName can be found in the CSP, that key is used.
' (2) If the KeyName does not exist and the KeyFile does exist, the key
' in the KeyFile is installed into the CSP and used.
' (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility.
' When specifying the KeyFile, the location of the KeyFile should be
' relative to the project output directory which is
' %Project Directory%\obj\<configuration>. For example, if your KeyFile is
' located in the project directory, you would specify the AssemblyKeyFile
' attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
' (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
' documentation for more information on this.
'
<Assembly: AssemblyDelaySign(False)>
<Assembly: AssemblyKeyFile("")>
<Assembly: AssemblyKeyName("")>
Form1.Designer.vb
Imports System.Collections
Imports System.ComponentModel
Imports System.IO
Imports System.Reflection
Imports FlexCel.Core
Imports FlexCel.XlsAdapter
Imports FlexCel.Report
Namespace AdvancedLinq
Partial Public Class mainForm
Inherits System.Windows.Forms.Form
Private WithEvents button1 As System.Windows.Forms.Button
Private saveFileDialog1 As System.Windows.Forms.SaveFileDialog
Private label1 As System.Windows.Forms.Label
Private WithEvents btnCancel As System.Windows.Forms.Button
''' <summary>
''' Required designer variable.
''' </summary>
Private components As System.ComponentModel.Container = Nothing
''' <summary>
''' Clean up any resources being used.
''' </summary>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If components IsNot Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#Region "Windows Form Designer generated code"
''' <summary>
''' Required method for Designer support - do not modify
''' the contents of this method with the code editor.
''' </summary>
Private Sub InitializeComponent()
Me.button1 = New System.Windows.Forms.Button()
Me.saveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
Me.label1 = New System.Windows.Forms.Label()
Me.btnCancel = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
' button1
'
Me.button1.Anchor = (CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles))
Me.button1.BackColor = System.Drawing.Color.Green
Me.button1.ForeColor = System.Drawing.Color.White
Me.button1.Location = New System.Drawing.Point(152, 88)
Me.button1.Name = "button1"
Me.button1.Size = New System.Drawing.Size(112, 23)
Me.button1.TabIndex = 0
Me.button1.Text = "GO!"
Me.button1.UseVisualStyleBackColor = False
' Me.button1.Click += New System.EventHandler(Me.button1_Click)
'
' saveFileDialog1
'
Me.saveFileDialog1.Filter = "Excel Files|*.xlsx"
Me.saveFileDialog1.RestoreDirectory = True
'
' label1
'
Me.label1.Location = New System.Drawing.Point(24, 24)
Me.label1.Name = "label1"
Me.label1.Size = New System.Drawing.Size(272, 24)
Me.label1.TabIndex = 2
Me.label1.Text = "Press ""GO"" to create a report from a List<> of objects."
'
' btnCancel
'
Me.btnCancel.Anchor = (CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles))
Me.btnCancel.BackColor = System.Drawing.Color.FromArgb((CInt((CByte(192)))), (CInt((CByte(0)))), (CInt((CByte(0)))))
Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.btnCancel.ForeColor = System.Drawing.Color.White
Me.btnCancel.Location = New System.Drawing.Point(272, 88)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.Size = New System.Drawing.Size(112, 23)
Me.btnCancel.TabIndex = 3
Me.btnCancel.Text = "Cancel"
Me.btnCancel.UseVisualStyleBackColor = False
' Me.btnCancel.Click += New System.EventHandler(Me.btnCancel_Click)
'
' mainForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(416, 133)
Me.Controls.Add(Me.btnCancel)
Me.Controls.Add(Me.label1)
Me.Controls.Add(Me.button1)
Me.Name = "mainForm"
Me.Text = "Advanced Linq Reports"
Me.ResumeLayout(False)
End Sub
#End Region
End Class
End Namespace
Form1.vb
Imports System.Collections
Imports System.ComponentModel
Imports System.IO
Imports System.Reflection
Imports FlexCel.Core
Imports FlexCel.XlsAdapter
Imports FlexCel.Report
Namespace AdvancedLinq
''' <summary>
''' Summary description for Form1.
''' </summary>
Partial Public Class mainForm
Inherits System.Windows.Forms.Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
AutoRun()
End Sub
Public Sub AutoRun()
Using report As New FlexCelReport(True)
LoadTables(report)
Dim DataPath As String = Path.Combine(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), ".."), "..") & Path.DirectorySeparatorChar
If saveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
report.Run(DataPath & "Advanced Linq.template.xlsx", saveFileDialog1.FileName)
If MessageBox.Show("Do you want to open the generated file?", "Confirm", MessageBoxButtons.YesNo) = System.Windows.Forms.DialogResult.Yes Then
Process.Start(saveFileDialog1.FileName)
End If
End If
End Using
End Sub
Private Sub LoadTables(ByVal report As FlexCelReport)
Dim Countries = New List(Of Country)()
Countries.Add(New Country("China", New People(1384688986), New Geography(New Area(270550, 9326410))))
Dim country = Countries(Countries.Count - 1)
country.People.Language.Add(New Language(New LanguageName("Md", "Mandarin"), New LanguageSpeakers(0, 66.2)))
country.People.Language.Add(New Language(New LanguageName("Yue", "Yue"), New LanguageSpeakers(0, 4.9)))
country.People.Language.Add(New Language(New LanguageName("Wu", "Wu"), New LanguageSpeakers(0, 6.1)))
country.People.Language.Add(New Language(New LanguageName("Mb", "Minbei"), New LanguageSpeakers(0, 6.2)))
country.People.Language.Add(New Language(New LanguageName("Mn", "Minnan"), New LanguageSpeakers(0, 5.2)))
country.People.Language.Add(New Language(New LanguageName("Xi", "Xiang"), New LanguageSpeakers(0, 3.0)))
country.People.Language.Add(New Language(New LanguageName("Gan", "Gan"), New LanguageSpeakers(0, 4.0)))
Countries.Add(New Country("India", New People(1296834042), New Geography(New Area(314070, 2973193))))
country = Countries(Countries.Count - 1)
country.People.Language.Add(New Language(New LanguageName("Hi", "Hindi"), New LanguageSpeakers(0, 43.6)))
country.People.Language.Add(New Language(New LanguageName("Bg", "Bengali"), New LanguageSpeakers(0, 8)))
country.People.Language.Add(New Language(New LanguageName("Ma", "Marath"), New LanguageSpeakers(0, 6.9)))
country.People.Language.Add(New Language(New LanguageName("Te", "Telugu"), New LanguageSpeakers(0, 6.7)))
country.People.Language.Add(New Language(New LanguageName("Ta", "Tamil"), New LanguageSpeakers(0, 5.7)))
country.People.Language.Add(New Language(New LanguageName("Gu", "Gujarati"), New LanguageSpeakers(0, 4.6)))
country.People.Language.Add(New Language(New LanguageName("Ur", "Urdu"), New LanguageSpeakers(0, 4.2)))
country.People.Language.Add(New Language(New LanguageName("Ka", "Kannada"), New LanguageSpeakers(0, 3.6)))
country.People.Language.Add(New Language(New LanguageName("Od", "Odia"), New LanguageSpeakers(0, 3.1)))
country.People.Language.Add(New Language(New LanguageName("Ma", "Malayalam"), New LanguageSpeakers(0, 2.9)))
country.People.Language.Add(New Language(New LanguageName("Pu", "Punjabi"), New LanguageSpeakers(0, 2.7)))
country.People.Language.Add(New Language(New LanguageName("As", "Assamese"), New LanguageSpeakers(0, 1.3)))
country.People.Language.Add(New Language(New LanguageName("Mi", "Maithili"), New LanguageSpeakers(0, 1.1)))
country.People.Language.Add(New Language(New LanguageName("O", "Other"), New LanguageSpeakers(0, 5.6)))
Countries.Add(New Country("United States", New People(329256465), New Geography(New Area(685924, 9147593))))
country = Countries(Countries.Count - 1)
country.People.Language.Add(New Language(New LanguageName("En", "English"), New LanguageSpeakers(0, 78.2)))
country.People.Language.Add(New Language(New LanguageName("Sp", "Spanish"), New LanguageSpeakers(0, 13.4)))
country.People.Language.Add(New Language(New LanguageName("Ch", "Chinese"), New LanguageSpeakers(0, 1.1)))
country.People.Language.Add(New Language(New LanguageName("O", "Other"), New LanguageSpeakers(0, 7.3)))
report.AddTable("country", Countries)
End Sub
Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Close()
End Sub
End Class
Public Class Country
Private privateName As String
Public Property Name() As String
Get
Return privateName
End Get
Private Set(ByVal value As String)
privateName = value
End Set
End Property
Public Property People() As People
Public Property Geography() As Geography
Public Sub New(ByVal name As String, ByVal people As People, ByVal geography As Geography)
Me.Name = name
Me.People = people
Me.Geography = geography
End Sub
End Class
Public Class Geography
Private privateArea As Area
Public Property Area() As Area
Get
Return privateArea
End Get
Private Set(ByVal value As Area)
privateArea = value
End Set
End Property
Public Sub New(ByVal area As Area)
Me.Area = area
End Sub
End Class
Public Class Area
Public ReadOnly Property Total() As Integer
Get
Return Water + Land
End Get
End Property
Private privateWater As Integer
Public Property Water() As Integer
Get
Return privateWater
End Get
Private Set(ByVal value As Integer)
privateWater = value
End Set
End Property
Private privateLand As Integer
Public Property Land() As Integer
Get
Return privateLand
End Get
Private Set(ByVal value As Integer)
privateLand = value
End Set
End Property
Public Sub New(ByVal water As Integer, ByVal land As Integer)
Me.Water = water
Me.Land = land
End Sub
End Class
Public Class People
Private privatePopulation As Integer
Public Property Population() As Integer
Get
Return privatePopulation
End Get
Private Set(ByVal value As Integer)
privatePopulation = value
End Set
End Property
Private privateLanguage As List(Of Language)
Public Property Language() As List(Of Language)
Get
Return privateLanguage
End Get
Private Set(ByVal value As List(Of Language))
privateLanguage = value
End Set
End Property
Public Sub New(ByVal population As Integer)
Me.Population = population
Language = New List(Of Language)()
End Sub
End Class
Public Class Language
Private privateName As LanguageName
Public Property Name() As LanguageName
Get
Return privateName
End Get
Private Set(ByVal value As LanguageName)
privateName = value
End Set
End Property
Private privateSpeakers As LanguageSpeakers
Public Property Speakers() As LanguageSpeakers
Get
Return privateSpeakers
End Get
Private Set(ByVal value As LanguageSpeakers)
privateSpeakers = value
End Set
End Property
Public Sub New(ByVal name As LanguageName, ByVal speakers As LanguageSpeakers)
Me.Name = name
Me.Speakers = speakers
End Sub
End Class
Public Class LanguageName
Private privateShortName As String
Public Property ShortName() As String
Get
Return privateShortName
End Get
Private Set(ByVal value As String)
privateShortName = value
End Set
End Property
Private privateLongName As String
Public Property LongName() As String
Get
Return privateLongName
End Get
Private Set(ByVal value As String)
privateLongName = value
End Set
End Property
Public Sub New(ByVal shortName As String, ByVal longName As String)
Me.ShortName = shortName
Me.LongName = longName
End Sub
End Class
Public Class LanguageSpeakers
Private privateAbsoluteNumber As Integer
Public Property AbsoluteNumber() As Integer
Get
Return privateAbsoluteNumber
End Get
Private Set(ByVal value As Integer)
privateAbsoluteNumber = value
End Set
End Property
Private privatePercent As Double
Public Property Percent() As Double
Get
Return privatePercent
End Get
Private Set(ByVal value As Double)
privatePercent = value
End Set
End Property
Public Sub New(ByVal absoluteNumber As Integer, ByVal percent As Double)
Me.AbsoluteNumber = absoluteNumber
Me.Percent = percent / 100.0
End Sub
End Class
End Namespace
Program.vb
Namespace AdvancedLinq
Friend NotInheritable Class Program
Private Sub New()
End Sub
''' <summary>
''' The main entry point for the application.
''' </summary>
<STAThread> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New mainForm())
End Sub
End Class
End Namespace