Canalblog
Editer l'article Suivre ce blog Administration + Créer mon blog
Publicité
SpireXLS
28 mai 2012

With Silverlight - Insert Hyperlink in Excel

Sometimes, hyperlink will be taken as data information and saved in an Excel file. The hyperlink can be a website address, E-mail address or other file. It leads readers to get more information after clicking hyperlink. Therefore, as data, hyperlink provides a convenient way to help readers get more additional related information.

In this post, I want to share my method to insert hyperlink in Excel file with Silverlight. I will create a new Excel file and add two hyperlinks into worksheet. One of the hyperlinks is one website address, and the other one is e-mail address.

Also, Spire.XLS for Silverlight, is used in this example. And I have added its dll file as reference in project.

STEPS:

Step 1. Design User Control

Rename MainPage.xaml as InsertHyperlink.xaml and then open it. UserControl appears. Change its size as I want and add background color for UserControl. Then, add a label to edit content to topic I want to talk about and set format for content. Next, add a RUN button.

InsertHyperlink.xaml

<UserControlx:Class="ExcelImage.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    d:DesignHeight="202"d:DesignWidth="424"xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <UserControl.Foreground>

        <LinearGradientBrushEndPoint="1,0.5"StartPoint="0,0.5">

            <GradientStopColor="Black"Offset="0" />

            <GradientStopColor="#FF0E0808"Offset="1" />

            <GradientStopColor="Black"Offset="0" />

        LinearGradientBrush>

    UserControl.Foreground>

    <Gridx:Name="LayoutRoot"Height="200"Width="423">

        <sdk:LabelHeight="83"HorizontalAlignment="Center"Margin="33,37,46,80"Name="label1"VerticalAlignment="Center"Width="344"Content="Insert Hyperlink in Excel File"FontSize="24"FontFamily="Times New Roman"FontWeight="Bold"Foreground="White" />

        <ButtonContent="RUN"Height="36"HorizontalAlignment="Left"Margin="338,134,0,0"Name="button1"VerticalAlignment="Top"Width="61"Click="button1_Click" />

        <Grid.Background>

            <LinearGradientBrushEndPoint="1,0.5"StartPoint="0,0.5">

                <GradientStopColor="Black"Offset="0" />

                <GradientStopColor="#FF0CB9EE"Offset="1" />

                <GradientStopColor="#FF054D63"Offset="0" />

                <GradientStopColor="#FF0992BD"Offset="0.336" />

                <GradientStopColor="#FF0999C6"Offset="0.156" />

                <GradientStopColor="#FF0994BF"Offset="0.27" />

                <GradientStopColor="#FF0992BD"Offset="0.467" />

                <GradientStopColor="#FF0993BF"Offset="0.762" />

            LinearGradientBrush>

        Grid.Background>

    Grid>

UserControl>

Step 2. Declare saveFileDialog

Declare a new Excel workbook and saveFileDialog. When declaring saveFileDialog, I also need to set filter for it to enable I to choose the appropriate format. I set filter as Excel Document(.xls).

        private SaveFileDialog saveFiledialog = null;

        private Workbook workbook = new Workbook();

 

        public MainPage()

        {

            InitializeComponent();

            this.saveFiledialog = new SaveFileDialog();

            this.saveFiledialog.Filter = "Excel Document(*.xls)|*.xls";

        }

 

Step 3. Insert Hyperlink

Firstly, create an empty worksheet for workbook and initialize this worksheet.

            workbook.CreateEmptySheets(1);

            Worksheet sheet = workbook.Worksheets[0];

 

Secondly, insert the first hyperlink in Excel. Declare a hyperlink and use sheet.Hyperlinks.Add() method to insert. Parameter is passed to this method is cell range. Then, set hyperlink type and address. Then, merge cells to add hyperlink text and set format for text.

            HyperLink hyperlink1 = sheet.HyperLinks.Add(sheet.Range["A1"]);

            hyperlink1.Type = HyperLinkType.Url;

            hyperlink1.Address = @"http://www.google.com";

            sheet.Range["A1:C1"].Merge();

            sheet.Range["A1"].Text = "Google";

            sheet.Range["A1"].Style.Font.FontName = "Arial Black";

            sheet.Range["A1"].Style.Font.Size = 20;

            sheet.Range["A1"].Style.Font.Color = Colors.Blue;

            sheet.Range["A1"].Style.VerticalAlignment = VerticalAlignType.Center;

 

Thirdly, insert the second hyperlink in Excel as the way to insert the first one. The different is that the url address is changed as mailto:XXX@gmail.com because the hyperlink is an E-mail address. Finally, set row height for cells.

            HyperLink hyperlink2 = sheet.HyperLinks.Add(sheet.Range["A2"]);

            hyperlink2.Type = HyperLinkType.Url;

            hyperlink2.Address = @"mailto:XXX@gmail.com";

            sheet.Range["A2:C2"].Merge();

            sheet.Range["A2"].Text = "Contact with Me";

            sheet.Range["A2"].Style.Font.FontName = "Calibri";

            sheet.Range["A2"].Style.Font.Size = 20;

            sheet.Range["A2"].Style.Font.Color = Colors.Green;

            sheet.Range["A2"].Style.VerticalAlignment = VerticalAlignType.Center;

 

            sheet.AllocatedRange.RowHeight = 25;

 

Step 4. Save File

Judge if the saveFiledialog which has been declared can pop up. If yes, save the Excel file by using save file dialog box.

            bool? result = this.saveFiledialog.ShowDialog();

            if (result.HasValue && result.Value)

            {

                using (Stream stream = this.saveFiledialog.OpenFile())

                {

                    this.workbook.SaveToStream(stream);

                }

            }

 

Full InsertHyperlink.xaml.cs

usingSystem.Windows;

usingSystem.Windows.Controls;

usingSystem.IO;

usingSystem.Windows.Media;

usingSpire.Xls;

 

namespaceExcelHyperlink

{

    public partial class MainPage : UserControl

    {

        private SaveFileDialog saveFiledialog = null;

        private Workbook workbook = new Workbook();

 

        public MainPage()

        {

            InitializeComponent();

            this.saveFiledialog = new SaveFileDialog();

            this.saveFiledialog.Filter = "Excel Document(*.xls)|*.xls";

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            workbook.CreateEmptySheets(1);

            Worksheet sheet = workbook.Worksheets[0];

 

            HyperLink hyperlink1 = sheet.HyperLinks.Add(sheet.Range["A1"]);

            hyperlink1.Type = HyperLinkType.Url;

            hyperlink1.Address = @"http://www.google.com";

            sheet.Range["A1:C1"].Merge();

            sheet.Range["A1"].Text = "Google";

            sheet.Range["A1"].Style.Font.FontName = "Arial Black";

            sheet.Range["A1"].Style.Font.Size = 20;

            sheet.Range["A1"].Style.Font.Color = Colors.Blue;

            sheet.Range["A1"].Style.VerticalAlignment = VerticalAlignType.Center;

 

            HyperLink hyperlink2 = sheet.HyperLinks.Add(sheet.Range["A2"]);

            hyperlink2.Type = HyperLinkType.Url;

            hyperlink2.Address = @"mailto:XXX@gmail.com";

            sheet.Range["A2:C2"].Merge();

            sheet.Range["A2"].Text = "Contact with Me";

            sheet.Range["A2"].Style.Font.FontName = "Calibri";

            sheet.Range["A2"].Style.Font.Size = 20;

            sheet.Range["A2"].Style.Font.Color = Colors.Green;

            sheet.Range["A2"].Style.VerticalAlignment = VerticalAlignType.Center;

 

            sheet.AllocatedRange.RowHeight = 25;

 

            bool? result = this.saveFiledialog.ShowDialog();

            if (result.HasValue && result.Value)

            {

                using (Stream stream = this.saveFiledialog.OpenFile())

                {

                    this.workbook.SaveToStream(stream);

                }

            }

        }

    }

}

RESULT

__________________________________________________________________

Click Here to LEARN MORE about Spire.XLS

Click Here to DOWNLOAD Spire.XLS

Spire.Office also can be used to realize this function.

Publicité
Publicité
Commentaires
SpireXLS
Publicité
Publicité