Friday, 07 January 2011 07:10

Insert Hyperlink to Word in C#, VB.NET

Word Hyperlink can be inserted automatically when users press ENTER or SPACEBAR after typing address of an existing Web page, such as www.e-iceblue.com. Also, users can insert customized hyperlink in Word for text to picture that they want to display as hyperlink. The hyperlink for text or picture can be Web page, E-mail address, other files or document.

Spire.Doc for .NET, a professional .NET Word component to manipulate Word documents, enables users to insert hyperlink in Word with C#/VB.NET. You can invoke the method paragraph.AppendHyperlink(string link, string text, HyperlinkType) to insert hyperlink in Word directly via Spire.Doc for .NET. And the HyperlinkType in method can be set as WebLink, EmailLink etc. Below is an effective screenshot after inserting hyperlink to Word in C#, VB.NET

Insert Word Hyperlink

Download and install Spire.Doc for .NET and then use the following code to insert hyperlink in Word.

[C#]
            //Create Word
            Document document = new Document();
            Section section = document.AddSection();
            Paragraph paragraph = section.AddParagraph();

            //Set Paragraph Styles
            ParagraphStyle txtStyle = new ParagraphStyle(document);
            txtStyle.Name = "Style";
            txtStyle.CharacterFormat.FontName = "Impact";
            txtStyle.CharacterFormat.FontSize = 16;
            txtStyle.CharacterFormat.TextColor = Color.RosyBrown;
            document.Styles.Add(txtStyle);

            ParagraphStyle hyperlinkstyle = new ParagraphStyle(document);
            hyperlinkstyle.Name = "linkStyle";
            hyperlinkstyle.CharacterFormat.FontName = "Calibri";
            hyperlinkstyle.CharacterFormat.FontSize = 15;
            document.Styles.Add(hyperlinkstyle);

            //Append Text and Set Text Format
            paragraph = section.AddParagraph();
            paragraph.AppendText("Home page");
            paragraph.ApplyStyle(txtStyle.Name);

            //Insert Hyperlink(Web Page)
            paragraph = section.AddParagraph();
            paragraph.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink);
            paragraph.ApplyStyle(hyperlinkstyle.Name);

            //Append text
            paragraph = section.AddParagraph();
            paragraph.AppendText("Contact US");
            paragraph.ApplyStyle(txtStyle.Name);

            //Insert Hyperlink(E-mail Address);
            paragraph = section.AddParagraph();
            paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com", HyperlinkType.EMailLink);
            paragraph.ApplyStyle(hyperlinkstyle.Name);

            //Save and Launch
            document.SaveToFile("Hyperlink.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Hyperlink.docx");
[VB.NET]
           'Create Word
            Dim document As New Document()
            Dim section As Section = document.AddSection()
            Dim paragraph As Paragraph = section.AddParagraph()

            'Set Paragraph Styles
            Dim txtStyle As New ParagraphStyle(document)
            txtStyle.Name = "Style"
            txtStyle.CharacterFormat.FontName = "Impact"
            txtStyle.CharacterFormat.FontSize = 16
            txtStyle.CharacterFormat.TextColor = Color.RosyBrown
            document.Styles.Add(txtStyle)

            Dim hyperlinkstyle As New ParagraphStyle(document)
            hyperlinkstyle.Name = "linkStyle"
            hyperlinkstyle.CharacterFormat.FontName = "Calibri"
            hyperlinkstyle.CharacterFormat.FontSize = 15
            document.Styles.Add(hyperlinkstyle)

            'Append Text and Set Text Format
            paragraph = section.AddParagraph()
            paragraph.AppendText("Home page")
            paragraph.ApplyStyle(txtStyle.Name)

            'Insert Hyperlink(Web Page)
            paragraph = section.AddParagraph()
            paragraph.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink)
            paragraph.ApplyStyle(hyperlinkstyle.Name)

            'Append text
            paragraph = section.AddParagraph()
            paragraph.AppendText("Contact US")
            paragraph.ApplyStyle(txtStyle.Name)

            'Insert Hyperlink(E-mail Address);
            paragraph = section.AddParagraph()
            paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com", HyperlinkType.EMailLink)
            paragraph.ApplyStyle(hyperlinkstyle.Name)

            'Save and Launch
            document.SaveToFile("Hyperlink.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Hyperlink.docx")

Spire.Doc is a Microsoft Word component, which enables users to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document in WPF, .NET and Silverlight.

Published in Hyperlink
Wednesday, 05 January 2011 06:15

Insert Hyperlink in Excel

Excel Hyperlink guides users to get more information about data in one cell. For example, the hyperlink links text (city name) in cell to a city history webpage and can help readers learn more about a city. Besides website address, the hyperlink can be an Email address or a file. Generally speaking, if data in on cell is website address like www.e-iceblue.com or Email address, the hyperlink will be generated automatically.

Spire.XLS for .NET, a professional .NET Excel component to generate, open and write Excel files, enables users to insert hyperlink in Excel by using C#, VB.NET. This guide illustrates how to realize this function quickly via Spire.XLS for .NET.

Now, I will insert hyperlink for Text “Chicago” and the Email address besides “Chicago” cell. Invoke sheet.Hyperlinks.Add(cellRange) method to insert hyperlink for specified cell. Then, set TextToDisplay, Type, Address properties for this hyperlink.Download and install Spire.XLS for .NET. Use the following code to insert hyperlink in Excel with C#, VB.NET.

Insert Excel Hyperlink

[C#]
using Spire.Xls;

namespace ExcelHyperlink
{
    class XLSLink
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"E:\Work\Documents\ExcelFiles\Student Info.xlsx");
            Worksheet sheet = workbook.Worksheets[0];

            HyperLink UrlLink= sheet.HyperLinks.Add(sheet.Range["E8"]);
            UrlLink.TextToDisplay = sheet.Range["E8"].Text;
            UrlLink.Type=HyperLinkType.Url; 
            UrlLink.Address="http://en.wikipedia.org/wiki/Chicago";

            HyperLink MailLink = sheet.HyperLinks.Add(sheet.Range["F8"]);       
            MailLink.TextToDisplay = sheet.Range["F8"].Text;
            MailLink.Type = HyperLinkType.Url;
            MailLink.Address = "mailto:ka.Lee220@hotmail.com";

            workbook.SaveToFile("ExcelHyperlink.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("ExcelHyperlink.xlsx");
        }
    }
}

[VB.NET]
Imports Spire.Xls

Namespace ExcelHyperlink
    Friend Class XLSLink
        Shared Sub Main(ByVal args() As String)
            Dim workbook As New Workbook()
            workbook.LoadFromFile("E:\Work\Documents\ExcelFiles\Student Info.xlsx")
            Dim sheet As Worksheet = workbook.Worksheets(0)

            Dim UrlLink As HyperLink = sheet.HyperLinks.Add(sheet.Range("E8"))
            UrlLink.TextToDisplay = sheet.Range("E8").Text
            UrlLink.Type = HyperLinkType.Url
            UrlLink.Address = "http://en.wikipedia.org/wiki/Chicago"

            Dim MailLink As HyperLink = sheet.HyperLinks.Add(sheet.Range("F8"))
            MailLink.TextToDisplay = sheet.Range("F8").Text
            MailLink.Type = HyperLinkType.Url
            MailLink.Address = "mailto:ka.Lee220@hotmail.com"

            workbook.SaveToFile("ExcelHyperlink.xlsx", ExcelVersion.Version2010)
            System.Diagnostics.Process.Start("ExcelHyperlink.xlsx")
        End Sub
    End Class
End Namespace
Published in Link
Wednesday, 21 July 2010 09:59

Word Hyperlink in C#, VB.NET

The sample demonstrates how to insert hyperlinks into a Word document.

Published in Context
Wednesday, 21 July 2010 09:53

Word Header and Footer in C#, VB.NET

The sample demonstrates how to insert header and footer into a Word document.

Published in Context