Search Results for

    Show / Hide Table of Contents

    Manual formulas (VB.Net / netframework)

    Note

    This demo is available in your FlexCel installation at <FlexCel Install Folder>\samples\vb\VS2022\netframework\20.Reports\48.Manual Formulas and also at https:​//​github.​com/​tmssoftware/​TMS-​FlexCel.​NET-​demos/​tree/​master/​vb/​VS2022/​netframework/​Modules/​20.​Reports/​48.​Manual Formulas

    Overview

    Here we will create a small fully functional Minesweeper in Excel, and without any macros.

    Most of the time, you will just want to write the formulas directly on the report, and let FlexCel change them when inserting rows. But sometimes, you might want to create a formula using flexcel tags. You can do it by using <#formula> and <#ref> tags, but don't do it unless you really need to.

    Concepts

    • How to replace <#tags> inside a formula.

    • When you use the <#Formula> tag, formulas are just strings on the template. So we could define inside a cell: "<#formula>=b3", but then if we want to change the template, and insert a row below row 1, the string will still be "<#formula>=b3". That is why we need to use a <#Ref> tag. For example <#formula>=<#ref(0;-1)> on cell c3 will point to b3, but if you insert a row below row 1, it will point to b4 as it should be.

    • There is never a real need to use the <#formula> tag, you can always workaround it. On this particular case we used it so we could change the font, (since it is not possible to change the font on a conditional format), but we could have also used the approach we used on the Expression Parameters demo. In fact, it might have been better that way (by adding the <#format range> tag in the data). But this demo is to show how to use this feature, it is up to you to decide if it is worth or not.

    • How to use conditional formatting, locked cells and data validation to make a nice application.

    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 ManualFormulas
        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|*.xls"
                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(288, 24)
                Me.label1.TabIndex = 2
                Me.label1.Text = "A report with formulas created with tags."
                ' 
                ' 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 = "Manual Formulas Report"
                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 ManualFormulas
        ''' <summary>
        ''' Shows the formula tag.
        ''' </summary>
        Partial Public Class mainForm
            Inherits System.Windows.Forms.Form
    
            Public Sub New()
                InitializeComponent()
            End Sub
    
            Private Sub SetupMines(ByVal MinesReport As FlexCelReport)
                Dim ds As New DataSet()
                Dim dtrows As DataTable = ds.Tables.Add("datarow")
                dtrows.Columns.Add("position", GetType(Integer))
    
                Dim dtcols As DataTable = ds.Tables.Add("datacol")
                dtcols.Columns.Add("position", GetType(Integer))
                dtcols.Columns.Add("value", GetType(Integer))
    
                ds.Relations.Add(dtrows.Columns("position"), dtcols.Columns("position"))
    
                'let's create 10 mines.
                Dim mines As New ArrayList()
                Dim rnd As New Random()
                Do While mines.Count < 10
                    Dim nextMine As Integer = rnd.Next(9 * 9 - 1)
                    Dim minepos As Integer = mines.BinarySearch(nextMine)
                    If minepos >= 0 Then 'the value already exists
                        Continue Do
                    End If
                    mines.Insert((Not minepos), nextMine)
                Loop
    
                'Fill the tables on master detail
                For r As Integer = 0 To 8
                    dtrows.Rows.Add(New Object() { r })
                    For c As Integer = 0 To 8
                        Dim values(1) As Object
                        values(0) = r
                        If mines.BinarySearch(r * 9 + c) >= 0 Then
                            values(1) = 1
                        Else
                            values(1) = DBNull.Value
                        End If
                        dtcols.Rows.Add(values)
                    Next c
                Next r
    
                'finally, add the tables to the report.
                MinesReport.ClearTables()
                MinesReport.AddTable(ds, TDisposeMode.DisposeAfterRun) 'leave to Flexcel to delete the dataset.
            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 MinesReport As New FlexCelReport(True)
                    AddHandler MinesReport.AfterGenerateWorkbook, AddressOf MinesReport_AfterGenerateWorkbook
                    SetupMines(MinesReport)
                    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
                        MinesReport.Run(DataPath & "Manual Formulas.template.xls", 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 btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
                Close()
            End Sub
    
            Private Sub MinesReport_AfterGenerateWorkbook(ByVal sender As Object, ByVal e As FlexCel.Report.GenerateEventArgs)
                'do some "pretty" up for the final user.
                'we could do this directly on the template, but doing it here allows us to keep the template unprotected and easier to modify.
    
                e.File.ActiveSheet = 2
                e.File.SheetVisible = TXlsSheetVisible.Hidden
                e.File.ActiveSheet = 1
                e.File.Protection.SetSheetProtection(Nothing, New TSheetProtectionOptions(True))
                For r As Integer = 20 To FlxConsts.Max_Rows97_2003 + 1
                    e.File.SetRowHidden(r, True)
                Next r
                For c As Integer = 12 To FlxConsts.Max_Columns97_2003 + 1
                    e.File.SetColHidden(c, True)
                Next c
            End Sub
        End Class
    
    End Namespace
    

    Program.vb

    Namespace ManualFormulas
        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
    
    In This Article
    Back to top FlexCel Studio for the .NET Framework v7.24.0.0
    © 2002 - 2025 tmssoftware.com