Search Results for

    Show / Hide Table of Contents

    Using hyperlinks (C# / netframework)

    Note

    This demo is available in your FlexCel installation at <FlexCel Install Folder>\samples\csharp\VS2022\netframework\10.API\70.HyperLinks and also at https:​//​github.​com/​tmssoftware/​TMS-​FlexCel.​NET-​demos/​tree/​master/​csharp/​VS2022/​netframework/​Modules/​10.​API/​70.​Hyper​Links

    Overview

    This is a small application to show how to use hyperlinks. Parameters in a hyperlink are not straightforward, so the idea is that you can go to Excel, create a Worksheet with your desired hyperlinks, and use this app to see what parameters they have.

    Columns in the Grid

    • Cell1, Cell2:

      These are the first and last cell on the range of the hyperlink. Normally they will be the same cell

    • Type: There are 4 types of Hyperlinks:

      1. URL: This can be http, https, ftp, or mailto://
      2. UNC: This is a path to a network site on universal naming convention, like \\server\folder\your_file.xls.

      3. Local File: This is a file stored relative to the path of the sheet. (for example, on the upper folder)

      4. Current Workbook: this is a link to a cell on the file. Note that this option always has Text=''

    • Text:

      Text of the HyperLink. This is empty when linking to a cell.

    • Description:

      Description of the HyperLink.

    • Target Frame:

      This parameter is not documented. You can leave it empty.

    • Text Mark:

      When entering an URL on Excel, you can enter additional text following the url with a "#" character (for example www.your_url.com#myurl") The text Mark is the text after the "#" char. When entering an address to a cell, the address goes here too.

    • Hint:

      This is the hint Excel will show when hovering the mouse over the hyperlink.

    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 - 2025 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.24.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 FlexCel.Core;
    using FlexCel.XlsAdapter;
    using System.IO;
    using System.Diagnostics;
    using System.Reflection;
    
    namespace HyperLinks
    {
        /// <summary>
        /// How to deal with Hyperlinks in FlexCel.
        /// </summary>
        public partial class mainForm: System.Windows.Forms.Form
        {
    
            public mainForm()
            {
                InitializeComponent();
                ResizeToolbar(mainToolbar);
            }
    
            private void ResizeToolbar(ToolStrip toolbar)
            {
    
                using (Graphics gr = CreateGraphics())
                {
                    double xFactor = gr.DpiX / 96.0;
                    double yFactor = gr.DpiY / 96.0;
                    toolbar.ImageScalingSize = new Size((int)(24 * xFactor), (int)(24 * yFactor));
                    toolbar.Width = 0; //force a recalc of the buttons.
                }
    
            }
    
            private void button2_Click(object sender, System.EventArgs e)
            {
                Close();
            }
    
            private ExcelFile Xls = null;
    
            private void ReadHyperLinks_Click(object sender, System.EventArgs e)
            {
                if (openFileDialog1.ShowDialog() != DialogResult.OK) return;
                Xls = new XlsFile();
    
                Xls.Open(openFileDialog1.FileName);
    
                dataGrid.CaptionText = "Hyperlinks on file: " + openFileDialog1.FileName;
                HlDataTable.Rows.Clear();
    
    
                for (int i = 1; i <= Xls.HyperLinkCount; i++)
                {
                    TXlsCellRange Range = Xls.GetHyperLinkCellRange(i);
                    THyperLink HLink = Xls.GetHyperLink(i);
    
                    string HLinkType = Enum.GetName(typeof(THyperLinkType), HLink.LinkType);
    
                    object[] values ={i, TCellAddress.EncodeColumn(Range.Left)+Range.Top.ToString(),
                                         TCellAddress.EncodeColumn(Range.Right)+Range.Bottom.ToString(),
                                         HLinkType,
                                         HLink.Text,
                                         HLink.Description,
                                         HLink.TextMark,
                                         HLink.TargetFrame,
                                         HLink.Hint
                                    };
                    HlDataTable.Rows.Add(values);
    
                }
    
            }
    
            private void writeHyperLinks_Click(object sender, System.EventArgs e)
            {
                if (Xls == null)
                {
                    MessageBox.Show("You need to open a file first.");
                    return;
                }
    
                ExcelFile XlsOut = new XlsFile(true);
                XlsOut.NewFile(1, TExcelFileFormat.v2019);
    
                for (int i = 1; i <= Xls.HyperLinkCount; i++)
                {
                    TXlsCellRange Range = Xls.GetHyperLinkCellRange(i);
                    THyperLink HLink = Xls.GetHyperLink(i);
    
                    int XF = -1;
                    object Value = Xls.GetCellValue(Range.Top, Range.Left, ref XF);
                    XlsOut.SetCellValue(i, 1, Value, XlsOut.AddFormat(Xls.GetFormat(XF)));
                    XlsOut.AddHyperLink(new TXlsCellRange(i, 1, i, 1), HLink);
                }
    
                if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;
                XlsOut.Save(saveFileDialog1.FileName);
                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();
                        }
                }
            }
    
        }
    }
    

    Form1.Designer.cs

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using FlexCel.Core;
    using FlexCel.XlsAdapter;
    using System.IO;
    using System.Diagnostics;
    using System.Reflection;
    namespace HyperLinks
    {
        public partial class mainForm: System.Windows.Forms.Form
        {
            private System.Windows.Forms.SaveFileDialog saveFileDialog1;
            private System.Windows.Forms.OpenFileDialog openFileDialog1;
            private System.Windows.Forms.DataGrid dataGrid;
            private System.Data.DataSet dataSet1;
            private System.Data.DataTable HlDataTable;
            private System.Data.DataColumn Index;
            private System.Data.DataColumn Cell1;
            private System.Data.DataColumn Cell2;
            private System.Data.DataColumn cType;
            private System.Data.DataColumn Description;
            private System.Data.DataColumn TextMark;
            private System.Data.DataColumn TargetFrame;
            private System.Data.DataColumn cText;
            private System.Data.DataColumn cHint;
            private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
            private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
            private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn2;
            private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn3;
            private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn4;
            private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn5;
            private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn6;
            private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn7;
            private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn8;
            private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn9;
            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.dataGrid = new System.Windows.Forms.DataGrid();
                this.HlDataTable = new System.Data.DataTable();
                this.Index = new System.Data.DataColumn();
                this.Cell1 = new System.Data.DataColumn();
                this.Cell2 = new System.Data.DataColumn();
                this.cType = new System.Data.DataColumn();
                this.cText = new System.Data.DataColumn();
                this.Description = new System.Data.DataColumn();
                this.TextMark = new System.Data.DataColumn();
                this.TargetFrame = new System.Data.DataColumn();
                this.cHint = new System.Data.DataColumn();
                this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
                this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
                this.dataGridTextBoxColumn2 = new System.Windows.Forms.DataGridTextBoxColumn();
                this.dataGridTextBoxColumn3 = new System.Windows.Forms.DataGridTextBoxColumn();
                this.dataGridTextBoxColumn4 = new System.Windows.Forms.DataGridTextBoxColumn();
                this.dataGridTextBoxColumn5 = new System.Windows.Forms.DataGridTextBoxColumn();
                this.dataGridTextBoxColumn6 = new System.Windows.Forms.DataGridTextBoxColumn();
                this.dataGridTextBoxColumn7 = new System.Windows.Forms.DataGridTextBoxColumn();
                this.dataGridTextBoxColumn8 = new System.Windows.Forms.DataGridTextBoxColumn();
                this.dataGridTextBoxColumn9 = new System.Windows.Forms.DataGridTextBoxColumn();
                this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
                this.dataSet1 = new System.Data.DataSet();
                this.mainToolbar = new System.Windows.Forms.ToolStrip();
                this.btnReadHyperlinks = new System.Windows.Forms.ToolStripButton();
                this.btnWriteHyperlinks = new System.Windows.Forms.ToolStripButton();
                this.btnExit = new System.Windows.Forms.ToolStripButton();
                ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(this.HlDataTable)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
                this.mainToolbar.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;
                this.saveFileDialog1.Title = "Save the file as...";
                // 
                // dataGrid
                // 
                this.dataGrid.CaptionText = "No file selected";
                this.dataGrid.DataMember = "";
                this.dataGrid.DataSource = this.HlDataTable;
                this.dataGrid.Dock = System.Windows.Forms.DockStyle.Fill;
                this.dataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
                this.dataGrid.Location = new System.Drawing.Point(0, 38);
                this.dataGrid.Name = "dataGrid";
                this.dataGrid.ReadOnly = true;
                this.dataGrid.Size = new System.Drawing.Size(768, 327);
                this.dataGrid.TabIndex = 3;
                this.dataGrid.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
                this.dataGridTableStyle1});
                // 
                // HlDataTable
                // 
                this.HlDataTable.Columns.AddRange(new System.Data.DataColumn[] {
                this.Index,
                this.Cell1,
                this.Cell2,
                this.cType,
                this.cText,
                this.Description,
                this.TextMark,
                this.TargetFrame,
                this.cHint});
                this.HlDataTable.TableName = "HlDataTable";
                // 
                // Index
                // 
                this.Index.ColumnName = "Index";
                // 
                // Cell1
                // 
                this.Cell1.ColumnName = "Cell1";
                // 
                // Cell2
                // 
                this.Cell2.ColumnName = "Cell2";
                // 
                // cType
                // 
                this.cType.ColumnName = "Type";
                // 
                // cText
                // 
                this.cText.ColumnName = "Text";
                // 
                // Description
                // 
                this.Description.ColumnName = "Description";
                // 
                // TextMark
                // 
                this.TextMark.ColumnName = "TextMark";
                // 
                // TargetFrame
                // 
                this.TargetFrame.ColumnName = "TargetFrame";
                // 
                // cHint
                // 
                this.cHint.ColumnName = "Hint";
                // 
                // dataGridTableStyle1
                // 
                this.dataGridTableStyle1.DataGrid = this.dataGrid;
                this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
                this.dataGridTextBoxColumn1,
                this.dataGridTextBoxColumn2,
                this.dataGridTextBoxColumn3,
                this.dataGridTextBoxColumn4,
                this.dataGridTextBoxColumn5,
                this.dataGridTextBoxColumn6,
                this.dataGridTextBoxColumn7,
                this.dataGridTextBoxColumn8,
                this.dataGridTextBoxColumn9});
                this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
                this.dataGridTableStyle1.MappingName = "HlDataTable";
                this.dataGridTableStyle1.PreferredColumnWidth = 15;
                // 
                // dataGridTextBoxColumn1
                // 
                this.dataGridTextBoxColumn1.Format = "";
                this.dataGridTextBoxColumn1.FormatInfo = null;
                this.dataGridTextBoxColumn1.HeaderText = "Index";
                this.dataGridTextBoxColumn1.MappingName = "Index";
                this.dataGridTextBoxColumn1.Width = 35;
                // 
                // dataGridTextBoxColumn2
                // 
                this.dataGridTextBoxColumn2.Format = "";
                this.dataGridTextBoxColumn2.FormatInfo = null;
                this.dataGridTextBoxColumn2.HeaderText = "Cell1";
                this.dataGridTextBoxColumn2.MappingName = "Cell1";
                this.dataGridTextBoxColumn2.Width = 40;
                // 
                // dataGridTextBoxColumn3
                // 
                this.dataGridTextBoxColumn3.Format = "";
                this.dataGridTextBoxColumn3.FormatInfo = null;
                this.dataGridTextBoxColumn3.HeaderText = "Cell2";
                this.dataGridTextBoxColumn3.MappingName = "Cell2";
                this.dataGridTextBoxColumn3.Width = 40;
                // 
                // dataGridTextBoxColumn4
                // 
                this.dataGridTextBoxColumn4.Format = "";
                this.dataGridTextBoxColumn4.FormatInfo = null;
                this.dataGridTextBoxColumn4.HeaderText = "Type";
                this.dataGridTextBoxColumn4.MappingName = "Type";
                this.dataGridTextBoxColumn4.Width = 75;
                // 
                // dataGridTextBoxColumn5
                // 
                this.dataGridTextBoxColumn5.Format = "";
                this.dataGridTextBoxColumn5.FormatInfo = null;
                this.dataGridTextBoxColumn5.HeaderText = "Text";
                this.dataGridTextBoxColumn5.MappingName = "Text";
                this.dataGridTextBoxColumn5.Width = 150;
                // 
                // dataGridTextBoxColumn6
                // 
                this.dataGridTextBoxColumn6.Format = "";
                this.dataGridTextBoxColumn6.FormatInfo = null;
                this.dataGridTextBoxColumn6.HeaderText = "Description";
                this.dataGridTextBoxColumn6.MappingName = "Description";
                this.dataGridTextBoxColumn6.Width = 150;
                // 
                // dataGridTextBoxColumn7
                // 
                this.dataGridTextBoxColumn7.Format = "";
                this.dataGridTextBoxColumn7.FormatInfo = null;
                this.dataGridTextBoxColumn7.HeaderText = "TextMark";
                this.dataGridTextBoxColumn7.MappingName = "TextMark";
                this.dataGridTextBoxColumn7.Width = 75;
                // 
                // dataGridTextBoxColumn8
                // 
                this.dataGridTextBoxColumn8.Format = "";
                this.dataGridTextBoxColumn8.FormatInfo = null;
                this.dataGridTextBoxColumn8.HeaderText = "TargetFrame";
                this.dataGridTextBoxColumn8.MappingName = "TargetFrame";
                this.dataGridTextBoxColumn8.Width = 75;
                // 
                // dataGridTextBoxColumn9
                // 
                this.dataGridTextBoxColumn9.Format = "";
                this.dataGridTextBoxColumn9.FormatInfo = null;
                this.dataGridTextBoxColumn9.HeaderText = "Hint";
                this.dataGridTextBoxColumn9.MappingName = "Hint";
                this.dataGridTextBoxColumn9.Width = 75;
                // 
                // openFileDialog1
                // 
                this.openFileDialog1.DefaultExt = "xls";
                this.openFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm|Excel 97/2003|*.xls|Excel 2007|*.xlsx;*.xlsm|All " +
        "files|*.*";
                this.openFileDialog1.Title = "Open an Excel File";
                // 
                // dataSet1
                // 
                this.dataSet1.DataSetName = "HlDataSet";
                this.dataSet1.Locale = new System.Globalization.CultureInfo("");
                this.dataSet1.Tables.AddRange(new System.Data.DataTable[] {
                this.HlDataTable});
                // 
                // mainToolbar
                // 
                this.mainToolbar.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.btnReadHyperlinks,
                this.btnWriteHyperlinks,
                this.btnExit});
                this.mainToolbar.Location = new System.Drawing.Point(0, 0);
                this.mainToolbar.Name = "mainToolbar";
                this.mainToolbar.Size = new System.Drawing.Size(768, 38);
                this.mainToolbar.TabIndex = 11;
                this.mainToolbar.Text = "toolStrip1";
                // 
                // btnReadHyperlinks
                // 
                this.btnReadHyperlinks.Image = ((System.Drawing.Image)(resources.GetObject("btnReadHyperlinks.Image")));
                this.btnReadHyperlinks.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.btnReadHyperlinks.Name = "btnReadHyperlinks";
                this.btnReadHyperlinks.Size = new System.Drawing.Size(96, 35);
                this.btnReadHyperlinks.Text = "Read Hyperlinks";
                this.btnReadHyperlinks.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
                this.btnReadHyperlinks.Click += new System.EventHandler(this.ReadHyperLinks_Click);
                // 
                // btnWriteHyperlinks
                // 
                this.btnWriteHyperlinks.Image = ((System.Drawing.Image)(resources.GetObject("btnWriteHyperlinks.Image")));
                this.btnWriteHyperlinks.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.btnWriteHyperlinks.Name = "btnWriteHyperlinks";
                this.btnWriteHyperlinks.Size = new System.Drawing.Size(98, 43);
                this.btnWriteHyperlinks.Text = "Write Hyperlinks";
                this.btnWriteHyperlinks.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
                this.btnWriteHyperlinks.Click += new System.EventHandler(this.writeHyperLinks_Click);
                // 
                // btnExit
                // 
                this.btnExit.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
                this.btnExit.Image = ((System.Drawing.Image)(resources.GetObject("btnExit.Image")));
                this.btnExit.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.btnExit.Name = "btnExit";
                this.btnExit.Size = new System.Drawing.Size(59, 35);
                this.btnExit.Text = "     E&xit     ";
                this.btnExit.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
                this.btnExit.Click += new System.EventHandler(this.button2_Click);
                // 
                // mainForm
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(768, 365);
                this.Controls.Add(this.dataGrid);
                this.Controls.Add(this.mainToolbar);
                this.Name = "mainForm";
                this.Text = "Form1";
                ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit();
                ((System.ComponentModel.ISupportInitialize)(this.HlDataTable)).EndInit();
                ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
                this.mainToolbar.ResumeLayout(false);
                this.mainToolbar.PerformLayout();
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
            #endregion
    
            private ToolStrip mainToolbar;
            private ToolStripButton btnReadHyperlinks;
            private ToolStripButton btnWriteHyperlinks;
            private ToolStripButton btnExit;
        }
    }
    

    Program.cs

    using System;
    using System.Windows.Forms;
    
    namespace HyperLinks
    {
        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());
            }
        }
    }
    
    In This Article
    Back to top FlexCel Studio for the .NET Framework v7.24.0.0
    © 2002 - 2025 tmssoftware.com