Meta templates (VB.Net / netframework)
Note
This demo is available in your FlexCel installation at <FlexCel Install Folder>\samples\vb\VS2022\netframework\20.Reports\90.Meta Templates and also at https://github.com/tmssoftware/TMS-FlexCel.NET-demos/tree/master/vb/VS2022/netframework/Modules/20.Reports/90.Meta Templates
Overview
FlexCel allows self-modifying templates, that can adapt themselves to the data that is going to be reported. This provides other way to create generic reports, besides what is shown in the Generic Reports example. But the utility of meta templates can go much further.
In this example, we have a template that will show rss feeds. If the feed contains a pubDate field, it will be added. If it doesn't, the row will be deleted from the template before running the report.
Concepts
You can use the <#Preprocess> tag to evaluate expressions when the template is being read. You can delete columns and rows, clear fields, etc. Once the preprocessing is over, the report will be run on the modified template.
Database fields and expressions can have default values, that will be applied when the field or expression does not exist. For example: <#DataTable.field;20> will evaluate to the value in DataTable.Field if Field exists, or 20 otherwise.
The <#Defined> tag can be used to know wheter a field or table is defined or not, and to for example delete the column if the field does not exist.
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 System.Data.OleDb
Imports System.Threading
Imports FlexCel.Core
Imports FlexCel.XlsAdapter
Imports FlexCel.Report
Imports System.Xml
Namespace MetaTemplates
Partial Public Class mainForm
Inherits System.Windows.Forms.Form
Private saveFileDialog1 As System.Windows.Forms.SaveFileDialog
Private panel2 As System.Windows.Forms.Panel
Private WithEvents button2 As System.Windows.Forms.Button
Private WithEvents btnExportExcel As System.Windows.Forms.Button
Private cbFeeds As System.Windows.Forms.ComboBox
Private cbOffline As System.Windows.Forms.CheckBox
Private cbShowFeedCount As System.Windows.Forms.CheckBox
Private components As System.ComponentModel.IContainer = 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()
Dim resources As New System.ComponentModel.ComponentResourceManager(GetType(mainForm))
Me.saveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
Me.panel2 = New System.Windows.Forms.Panel()
Me.button2 = New System.Windows.Forms.Button()
Me.btnExportExcel = New System.Windows.Forms.Button()
Me.cbFeeds = New System.Windows.Forms.ComboBox()
Me.cbOffline = New System.Windows.Forms.CheckBox()
Me.cbShowFeedCount = New System.Windows.Forms.CheckBox()
Me.panel2.SuspendLayout()
Me.SuspendLayout()
'
' saveFileDialog1
'
Me.saveFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm|Excel 97/2003|*.xls|Excel 2007|*.xlsx;*.xlsm|All " & "files|*.*"
Me.saveFileDialog1.RestoreDirectory = True
'
' panel2
'
Me.panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.panel2.Controls.Add(Me.button2)
Me.panel2.Controls.Add(Me.btnExportExcel)
Me.panel2.Controls.Add(Me.cbFeeds)
Me.panel2.Dock = System.Windows.Forms.DockStyle.Top
Me.panel2.Location = New System.Drawing.Point(0, 0)
Me.panel2.Name = "panel2"
Me.panel2.Size = New System.Drawing.Size(528, 40)
Me.panel2.TabIndex = 3
'
' button2
'
Me.button2.Anchor = (CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles))
Me.button2.BackColor = System.Drawing.SystemColors.Control
Me.button2.Image = (CType(resources.GetObject("button2.Image"), System.Drawing.Image))
Me.button2.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
Me.button2.Location = New System.Drawing.Point(464, 2)
Me.button2.Name = "button2"
Me.button2.Size = New System.Drawing.Size(56, 26)
Me.button2.TabIndex = 2
Me.button2.Text = "Exit"
Me.button2.TextAlign = System.Drawing.ContentAlignment.MiddleRight
Me.button2.UseVisualStyleBackColor = False
' Me.button2.Click += New System.EventHandler(Me.button2_Click)
'
' btnExportExcel
'
Me.btnExportExcel.BackColor = System.Drawing.SystemColors.Control
Me.btnExportExcel.Image = (CType(resources.GetObject("btnExportExcel.Image"), System.Drawing.Image))
Me.btnExportExcel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
Me.btnExportExcel.Location = New System.Drawing.Point(16, 2)
Me.btnExportExcel.Name = "btnExportExcel"
Me.btnExportExcel.Size = New System.Drawing.Size(120, 30)
Me.btnExportExcel.TabIndex = 1
Me.btnExportExcel.Text = "Export to Excel"
Me.btnExportExcel.TextAlign = System.Drawing.ContentAlignment.MiddleRight
Me.btnExportExcel.UseVisualStyleBackColor = False
' Me.btnExportExcel.Click += New System.EventHandler(Me.btnExportExcel_Click)
'
' cbFeeds
'
Me.cbFeeds.DisplayMember = "1"
Me.cbFeeds.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.cbFeeds.Location = New System.Drawing.Point(200, 8)
Me.cbFeeds.Name = "cbFeeds"
Me.cbFeeds.Size = New System.Drawing.Size(216, 21)
Me.cbFeeds.TabIndex = 4
Me.cbFeeds.ValueMember = "1"
'
' cbOffline
'
Me.cbOffline.Checked = True
Me.cbOffline.CheckState = System.Windows.Forms.CheckState.Checked
Me.cbOffline.Location = New System.Drawing.Point(40, 56)
Me.cbOffline.Name = "cbOffline"
Me.cbOffline.Size = New System.Drawing.Size(368, 24)
Me.cbOffline.TabIndex = 5
Me.cbOffline.Text = "Use offline data (do not connect to internet)"
'
' cbShowFeedCount
'
Me.cbShowFeedCount.Location = New System.Drawing.Point(40, 80)
Me.cbShowFeedCount.Name = "cbShowFeedCount"
Me.cbShowFeedCount.Size = New System.Drawing.Size(360, 24)
Me.cbShowFeedCount.TabIndex = 6
Me.cbShowFeedCount.Text = "Show feed number column in the generated report."
'
' mainForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(528, 126)
Me.Controls.Add(Me.cbShowFeedCount)
Me.Controls.Add(Me.cbOffline)
Me.Controls.Add(Me.panel2)
Me.Name = "mainForm"
Me.Text = "Meta Templates"
' Me.Load += New System.EventHandler(Me.mainForm_Load)
Me.panel2.ResumeLayout(False)
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 System.Data.OleDb
Imports System.Threading
Imports FlexCel.Core
Imports FlexCel.XlsAdapter
Imports FlexCel.Report
Imports System.Xml
Namespace MetaTemplates
''' <summary>
''' Templates that self-modify themselves before running.
''' </summary>
Partial Public Class mainForm
Inherits System.Windows.Forms.Form
Public Sub New()
InitializeComponent()
End Sub
Public Structure FeedData
Public Name As String
Public Url As String
Public Logo As String
Public Sub New(ByVal aName As String, ByVal aUrl As String, ByVal aLogo As String)
Name = aName
Url = aUrl
Logo = aLogo
End Sub
Public Overrides Function ToString() As String
Return Name
End Function
End Structure
Private Feeds() As FeedData = { _
New FeedData("TMS", "https://www.tmssoftware.com/rss/tms.xml", "tms.gif"), _
New FeedData("MSDN","https://sxpdata.microsoft.com/feeds/3.0/msdntn/MSDNMagazine_enus", "msdn.jpg"), _
New FeedData("SLASHDOT", "http://rss.slashdot.org/Slashdot/slashdot", "slashdot.gif") _
}
Private Sub button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button2.Click
Close()
End Sub
Private ReadOnly Property DataPath() As String
Get
Return Path.Combine(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), ".."), "..") & Path.DirectorySeparatorChar
End Get
End Property
Private Sub Export(ByVal data As DataSet)
Using Report As New FlexCelReport(True)
Report.AddTable(data)
Report.SetValue("FeedName", CType(cbFeeds.SelectedValue, FeedData).Name)
Report.SetValue("FeedUrl", CType(cbFeeds.SelectedValue, FeedData).Url)
Report.SetValue("ShowCount", cbShowFeedCount.Checked)
Using fs As New FileStream(Path.Combine(Path.Combine(DataPath, "logos"), CType(cbFeeds.SelectedValue, FeedData).Logo), FileMode.Open)
Dim b(CInt(fs.Length) - 1) As Byte
fs.Read(b, 0, b.Length)
Report.SetValue("Logo", b)
End Using
Report.Run(DataPath & "Meta Templates.template.xls", saveFileDialog1.FileName)
End Using
End Sub
Private Sub btnExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportExcel.Click
Using data As New DataSet()
Dim LocalData As String = Path.Combine(Path.Combine(DataPath, "data"), CType(cbFeeds.SelectedValue, FeedData).Name & ".xml")
If cbOffline.Checked Then
data.ReadXml(LocalData)
Else
'In a real world example, this should be done on a thread, as it is done in the HTML example.
'To keep things simple here ,we will just "freeze" the gui while downloading the data, without
'providing feedback to the user.
Dim FeedReader As New XmlTextReader(CType(cbFeeds.SelectedValue, FeedData).Url)
data.ReadXml(FeedReader)
End If
#If (SaveForOffline) Then
data.WriteXml(LocalData)
#End If
If saveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Export(data)
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 mainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
cbFeeds.DataSource = Feeds
cbFeeds.SelectedIndex = 0
End Sub
End Class
End Namespace
Program.vb
Namespace MetaTemplates
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