Meta templates (C# / netframework)
Note
This demo is available in your FlexCel installation at <FlexCel Install Folder>\samples\csharp\VS2022\netframework\20.Reports\90.Meta Templates and also at https://github.com/tmssoftware/TMS-FlexCel.NET-demos/tree/master/csharp/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.cs
using System.Reflection;
using 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 - 2024 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("7.21.0.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.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Data.OleDb;
using System.Threading;
using FlexCel.Core;
using FlexCel.XlsAdapter;
using FlexCel.Report;
using System.Xml;
namespace MetaTemplates
{
/// <summary>
/// Templates that self-modify themselves before running.
/// </summary>
public partial class mainForm: System.Windows.Forms.Form
{
public mainForm()
{
InitializeComponent();
}
public struct FeedData
{
public string Name;
public string Url;
public string Logo;
public FeedData(string aName, string aUrl, string aLogo)
{
Name = aName;
Url = aUrl;
Logo = aLogo;
}
public override string ToString()
{
return Name;
}
}
private FeedData[] Feeds =
{
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 void button2_Click(object sender, System.EventArgs e)
{
Close();
}
private string DataPath
{
get
{
return Path.Combine(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), ".."), "..") + Path.DirectorySeparatorChar;
}
}
private void Export(DataSet data)
{
using (FlexCelReport Report = new FlexCelReport(true))
{
Report.AddTable(data);
Report.SetValue("FeedName", ((FeedData)cbFeeds.SelectedValue).Name);
Report.SetValue("FeedUrl", ((FeedData)cbFeeds.SelectedValue).Url);
Report.SetValue("ShowCount", cbShowFeedCount.Checked);
using (FileStream fs = new FileStream(Path.Combine(Path.Combine(DataPath, "logos"), ((FeedData)cbFeeds.SelectedValue).Logo), FileMode.Open))
{
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
Report.SetValue("Logo", b);
}
Report.Run(DataPath + "Meta Templates.template.xls", saveFileDialog1.FileName);
}
}
private void btnExportExcel_Click(object sender, System.EventArgs e)
{
using (DataSet data = new DataSet())
{
string LocalData = Path.Combine(Path.Combine(DataPath, "data"), ((FeedData)cbFeeds.SelectedValue).Name + ".xml");
if (cbOffline.Checked)
{
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.
XmlTextReader FeedReader = new XmlTextReader(((FeedData)cbFeeds.SelectedValue).Url);
data.ReadXml(FeedReader);
}
#if (SaveForOffline)
data.WriteXml(LocalData);
#endif
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Export(data);
if (MessageBox.Show("Do you want to open the generated file?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
using (Process p = new Process())
{
p.StartInfo.FileName = saveFileDialog1.FileName;
p.StartInfo.UseShellExecute = true;
p.Start();
}
}
}
}
}
private void mainForm_Load(object sender, System.EventArgs e)
{
cbFeeds.DataSource = Feeds;
cbFeeds.SelectedIndex = 0;
}
}
}
Form1.Designer.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Data.OleDb;
using System.Threading;
using FlexCel.Core;
using FlexCel.XlsAdapter;
using FlexCel.Report;
using System.Xml;
namespace MetaTemplates
{
public partial class mainForm: System.Windows.Forms.Form
{
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button btnExportExcel;
private System.Windows.Forms.ComboBox cbFeeds;
private System.Windows.Forms.CheckBox cbOffline;
private System.Windows.Forms.CheckBox cbShowFeedCount;
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#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 void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(mainForm));
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.panel2 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.btnExportExcel = new System.Windows.Forms.Button();
this.cbFeeds = new System.Windows.Forms.ComboBox();
this.cbOffline = new System.Windows.Forms.CheckBox();
this.cbShowFeedCount = new System.Windows.Forms.CheckBox();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// saveFileDialog1
//
this.saveFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm|Excel 97/2003|*.xls|Excel 2007|*.xlsx;*.xlsm|All " +
"files|*.*";
this.saveFileDialog1.RestoreDirectory = true;
//
// panel2
//
this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel2.Controls.Add(this.button2);
this.panel2.Controls.Add(this.btnExportExcel);
this.panel2.Controls.Add(this.cbFeeds);
this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
this.panel2.Location = new System.Drawing.Point(0, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(528, 40);
this.panel2.TabIndex = 3;
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button2.BackColor = System.Drawing.SystemColors.Control;
this.button2.Image = ((System.Drawing.Image)(resources.GetObject("button2.Image")));
this.button2.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button2.Location = new System.Drawing.Point(464, 2);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 26);
this.button2.TabIndex = 2;
this.button2.Text = "Exit";
this.button2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// btnExportExcel
//
this.btnExportExcel.BackColor = System.Drawing.SystemColors.Control;
this.btnExportExcel.Image = ((System.Drawing.Image)(resources.GetObject("btnExportExcel.Image")));
this.btnExportExcel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btnExportExcel.Location = new System.Drawing.Point(16, 2);
this.btnExportExcel.Name = "btnExportExcel";
this.btnExportExcel.Size = new System.Drawing.Size(120, 30);
this.btnExportExcel.TabIndex = 1;
this.btnExportExcel.Text = "Export to Excel";
this.btnExportExcel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.btnExportExcel.UseVisualStyleBackColor = false;
this.btnExportExcel.Click += new System.EventHandler(this.btnExportExcel_Click);
//
// cbFeeds
//
this.cbFeeds.DisplayMember = "1";
this.cbFeeds.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbFeeds.Location = new System.Drawing.Point(200, 8);
this.cbFeeds.Name = "cbFeeds";
this.cbFeeds.Size = new System.Drawing.Size(216, 21);
this.cbFeeds.TabIndex = 4;
this.cbFeeds.ValueMember = "1";
//
// cbOffline
//
this.cbOffline.Checked = true;
this.cbOffline.CheckState = System.Windows.Forms.CheckState.Checked;
this.cbOffline.Location = new System.Drawing.Point(40, 56);
this.cbOffline.Name = "cbOffline";
this.cbOffline.Size = new System.Drawing.Size(368, 24);
this.cbOffline.TabIndex = 5;
this.cbOffline.Text = "Use offline data (do not connect to internet)";
//
// cbShowFeedCount
//
this.cbShowFeedCount.Location = new System.Drawing.Point(40, 80);
this.cbShowFeedCount.Name = "cbShowFeedCount";
this.cbShowFeedCount.Size = new System.Drawing.Size(360, 24);
this.cbShowFeedCount.TabIndex = 6;
this.cbShowFeedCount.Text = "Show feed number column in the generated report.";
//
// mainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(528, 126);
this.Controls.Add(this.cbShowFeedCount);
this.Controls.Add(this.cbOffline);
this.Controls.Add(this.panel2);
this.Name = "mainForm";
this.Text = "Meta Templates";
this.Load += new System.EventHandler(this.mainForm_Load);
this.panel2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
}
}
Program.cs
using System;
using System.Windows.Forms;
namespace MetaTemplates
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new mainForm());
}
}
}