Thursday, 13 January 2011 07:48

How to Change Word Font Color in C#, VB.NET

Word Font Color is one kind of font formats to change text color in documents. By default, the color is black. However, the font color will be changed in order to make the special or important contents (for example, notices in instruction document) more obviously so that readers will pay more attention on the contents.

Spire.Doc for .NET, the professional .NET Word component, enables users to set font color by using C#, VB.NET. And this guide presents the method how to easily change font color in a Word document via Spire.Doc for .NET. The following screenshot is the result after changing.

Word Text Font Color

  • Load the document which you want to change font color at first.
  • Secondly, get paragraph 1(the title) in section 1.
  • Thirdly, declare a new ParagraphStyle. Give it a name. Set CharacterFormat property of ParagraphStyle. Then, apply this ParagraphStyle for paragraph 1.
  • Fourthly, set another ParagraphStyle for paragraph 2 as step 3.

Code as following:

[C#]
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

namespace SetTextColor
{
    class TextColor
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\WordDocuments\A GOOD MAN IS HARD TO FIND.docx");

            //Set Text Color for Paragraph 1(the Title)
            Section section = document.Sections[0];
            Paragraph p1 = section.Paragraphs[0];
            ParagraphStyle s1 = new ParagraphStyle(document);
            s1.Name = "TitleTextColor";
            s1.CharacterFormat.TextColor = Color.RosyBrown;
            document.Styles.Add(s1);
            p1.ApplyStyle(s1.Name);

            //Set Text Color for Paragraph 2
            Paragraph p2 = section.Paragraphs[1];
            ParagraphStyle s2 = new ParagraphStyle(document);
            s2.Name = "BodyTextColor";
            s2.CharacterFormat.TextColor = Color.DarkBlue;
            document.Styles.Add(s2);
            p2.ApplyStyle(s2.Name);
            
            //Save and Launch
            document.SaveToFile("TextColor.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("TextColor.docx");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace SetTextColor
    Friend Class TextColor
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\WordDocuments\A GOOD MAN IS HARD TO FIND.docx")

            'Set Text Color for Paragraph 1(the Title)
            Dim section As Section = document.Sections(0)
            Dim p1 As Paragraph = section.Paragraphs(0)
            Dim s1 As New ParagraphStyle(document)
            s1.Name = "TitleTextColor"
            s1.CharacterFormat.TextColor = Color.RosyBrown
            document.Styles.Add(s1)
            p1.ApplyStyle(s1.Name)

            'Set Text Color for Paragraph 2
            Dim p2 As Paragraph = section.Paragraphs(1)
            Dim s2 As New ParagraphStyle(document)
            s2.Name = "BodyTextColor"
            s2.CharacterFormat.TextColor = Color.DarkBlue
            document.Styles.Add(s2)
            p2.ApplyStyle(s2.Name)

            'Save and Launch
            document.SaveToFile("TextColor.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("TextColor.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, a stand-alone Word component, enables developers/programmers to fast generate, load, write, modify and save Word documents on their .NET, WPF, Silverlight applications.

Word Replace function enables users to replace specified strings with new ones. All numbers of string will be detected and replaced at once time. Spire.Doc for .NET provides document.Replace method to realize replace function in C#, VB.NET. The following screenshot is the result after replacing.

Replace Word Text

By invoking document.Replace method, users can replace particular string with new value and set if the replacement is case-sensitive or the whole word will be affected. There are five possibilities document.Replace method provides.

  • Replace all entries of matchString(which will be replaced) regular expression with new value string.
  • Replace all entries of matchString regular expression with text range holder.
  • Replace specified matchString in matchDoc (specified document), taking into consideration of case-sensitive and whole word options.
  • Replace all entries of mathString with new value string, taking into consideration of case-sensitive and whole word options.
  • Replace all entries of matchString with text selection, taking into consideration of case-sensitive and whole word options.

This guide will introduce how to realize the fourth possibility above to replace the string "New Zealand" in the loaded document with "NZ". Download and install Spire.Doc for .NET. Follow the code:

[C#]
using Spire.Doc;

namespace ReplaceString
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\WordDocuments\New Zealand.docx");

            document.Replace("New Zealand", "NZ", false, true);

            document.SaveToFile("Replace.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Replace.docx");
        }
    }
}
[VB]
Imports Spire.Doc

Namespace ReplaceString
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\WordDocuments\New Zealand.docx")

            document.Replace("New Zealand", "NZ", False, True)

            document.SaveToFile("Replace.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Replace.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, a professional Word component, enables developers/programmers perform a wide range of processing tasks, such as generate, write, modify and save for their customize .NET, Silverlight and WPF applications.

Basic Knowledge about Office OpenXML

When talking about Office OpenXML, we may think of HTML. Actually, Office OpenXML is similar to HTML both are tag-based languages. The difference between Office OpenXML and HTML is that the tags which Office OpenXML uses are not predefined. If we want to create own tags within Office OpenXML, we need to follow a few rules.

Firstly, only one root element is contained in Office OpenXML document. The root element is often taken as document element and appears after the prolog section. Besides, all the Office OpenXML elements should contain end tags. Both start and end tag should be identical. Also, the elements can’t overlap. What’s more, all attribute values must use quotation marks and we can’t use some special characters within the text. After following the rules, the Office OpenXML document will be well formatted.

Use C# and VB.NET Convert Doc to Office OpenXML via Spire.Doc

Spire.Doc (Spire.Office) presents you an easy way to convert Doc to Office OpenXML. In this way, we can convert an exist Word doc file to Office OpenXML format with a few clicks. Now, just follow the simple steps.

Step 1: Create Project

Download Spire.Doc and install on system. Create a project through Visual Studio and add Spire.Doc DLL as reference.

Note: Please make sure Spire.Doc and Visual Studio are correctly installed on system

Step 2: Load Word Doc File

Load local Word doc file which we need to convert to Office OpenXML format. The following code can help us load it.

Document document = new Document();
document.LoadFromFile(@"D:\Sample.doc");

Step 3: Convert Doc to Office OpenXML

Spire.Doc supports convert Word Doc files to most of popular file formats such as PDF, HTML, Office OpenXML, EPub, RTF, Dot, Text, etc. Now, use the code below to convert Word to Office OpenXML.

document.SaveToFile("Sample.xml", FileFormat.Xml);

Step 4: Full Code

Now, write the full code into your project and press F5 to start the program.

[C#]
using System;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;

namespace to XML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Create word document
            Document document = new Document();
            document.LoadFromFile(@"D:\Sample.doc");

            //Save doc file.
            document.SaveToFile("Sample.xml", FileFormat.Xml);

            //Launching the MS Word file.
            WordDocViewer("Sample.xml");
        }

        private void WordDocViewer(string fileName)
        {
            try
            {
                System.Diagnostics.Process.Start(fileName);
            }
            catch { }
        }

    }
}
[VB.NET]
Imports System
Imports System.Windows.Forms
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace to XML
	Partial Public Class Form1
		Inherits Form
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
			'Create word document
			Dim document As New Document()
			document.LoadFromFile("D:\Sample.doc")

			'Save doc file.
			document.SaveToFile("Sample.xml", FileFormat.Xml);

			'Launching the MS Word file.
			WordDocViewer("Sample.xml")
		End Sub

		Private Sub WordDocViewer(ByVal fileName As String)
			Try
				System.Diagnostics.Process.Start(fileName)
			Catch
			End Try
		End Sub

	End Class
End Namespace

After running the demo, you may find an Office OpenXML document launched on your browser:

Spire.Doc is an MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight. Click to Learn more...

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.

Word encryption, one method to protect Word document, requires users to give the document a password. Without the password, encrypted document cannot be opened. Solution in this guide demonstrates how to encrypt Word document with custom password in C# and VB.NET via Spire.Doc for .NET.

Spire.Doc for .NET, specializing in performing Word processing tasks for .NET, provides a Document.Encrypt method which enables users to encrypt Word. The overload passed to this method is string password. Firstly, load the Word document which is needed to protect. Secondly, invoke Document.Encrypt method to encrypt with password. Thirdly, save the encrypt document and launch for viewing. After debugging, a dialog box pops up and requires the password. Enter the password to open the document and the document information will be shown as following to tell users that it is encrypted.

Encrypt Word Document

Download and install Spire.Doc for .NET and use the following code to encrypt Word.

[C#]
using Spire.Doc;

namespace Encryption
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx");

            //Encrypt
            document.Encrypt("eiceblue");

            //Save and Launch
            document.SaveToFile("Encryption.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Encryption.docx");
        }
    }
}
[VB.NET]
Imports Spire.Doc

Namespace Encryption
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx")

            'Encrypt
            document.Encrypt("eiceblue")

            'Save and Launch
            document.SaveToFile("Encryption.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Encryption.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, an easy-to-use component to operate Word document, allows developers to fast generate, write, edit and save Word (Word 97-2003, Word 2007, Word 2010) in C# and VB.NET for .NET, Silverlight and WPF.

Wednesday, 05 January 2011 08:37

Add Excel Borders in C#, VB.NET

In Excel, you can add borders to individual cells to emphasize data, mark summarized values, or separate data in cell ranges. Borders can also be formatted with different line styles and colors. This article presents how we can apply border styles to Excel cells using Spire.XLS.

Step 1: Create a Workbook instance and get the first worksheet.

Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];

Step 2: Get the cell range where you want to apply border style.

CellRange range = sheet.Range["B2:D4"];

Step 3: Call BorderInside() and BorderAround() methods to apply styles to borders inside and around the range. To add a border to the top, bottom, left and right, use the Borders[BordersLineType.EdgeTop].LineStyle property, Borders[BordersLineType.EdgeBottom].LineStyle property and alike.

range.BorderInside(LineStyleType.Thin, Color.Blue);
range.BorderAround(LineStyleType.Medium, Color.Blue);

Step 4: Save to file.

wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);

Output:

Add Excel Borders in C#, VB.NET

Full Code:

[C#]
Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];

//apply border style to the range B2:D4
CellRange range = sheet.Range["B2:D4"];
range.BorderInside(LineStyleType.Thin, Color.Blue);
range.BorderAround(LineStyleType.Medium, Color.Blue);

//apply border style to the range D7:F9
range = sheet.Range["D7:F9"];
range.Borders.LineStyle = LineStyleType.Double;
range.Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
range.Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;

//apply border style to the range F12:H14
range = sheet.Range["F12:H14"];
range.Merge();
range.BorderAround(LineStyleType.Thin);
range.Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.Thin;
range.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thick;  
range.Borders.Color = Color.Red;

wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);
[VB.NET]
Dim wb As Workbook = New Workbook()
Dim sheet As Worksheet = wb.Worksheets(0)
 
'apply border style to the range B2:D4
Dim range As CellRange = sheet.Range("B2:D4")
range.BorderInside(LineStyleType.Thin, Color.Blue)
range.BorderAround(LineStyleType.Medium, Color.Blue)
 
'apply border style to the range D7:F9
range = sheet.Range("D7:F9")
range.Borders.LineStyle = LineStyleType.Double
range.Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.None
range.Borders(BordersLineType.DiagonalUp).LineStyle = LineStyleType.None
 
'apply border style to the range F12:H14
range = sheet.Range("F12:H14")
range.Merge()
range.BorderAround(LineStyleType.Thin)
range.Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.Thin
range.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Thick
range.Borders.Color = Color.Red

wb.SaveToFile("result.xlsx", ExcelVersion.Version2013)
Wednesday, 05 January 2011 08:36

Set Excel Font in C#, VB.NET

Excel font makes the whole Excel colorful. Whatever you emphasize some special cell data or beautify the whole document, you can start from setting Excel font, such as font style, size, color, bold, underline, italic etc. This section will introduce a solution to set Excel font via a .NET Excel component in C#, VB.NET.

Spire.XLS for .NET, applied both in ASP.NET websites and Windows Forms application, enables users to generate, edit and covert Excel files to other files. Using this Excel component, you can set your Excel font by the class Spire.Xls.CellStyle.Font very quickly. This class enables you to set Excel font style, size, color, bold, underline and italic which are the most commonly used formats, also it can set data alignment, strikethrough, subscript, superscript etc. Please view the Excel font effect in below picture:

Set Excel Cell Font

Here you can download Spire,XLS for .NET. After installing it on system and adding Spire.Xls dll, you can begin to set Excel font by below code.

Whole Code:

[C#]
using Spire.Xls;

namespace Excel_font
{
    class Program
    {
        static void Main(string[] args)
        {
            //create a new workbook and load Excel data
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\Excel Font.xlsx");
            Worksheet sheet = workbook.Worksheets[0];

            //Set excel font style
            sheet.Range["A3:E3"].Style.Font.FontName = "Comic Sans MS";
            sheet.Range["A4:E14"].Style.Font.FontName = "Corbel";
            //set excel font size
            sheet.Range["A2:E2"].Style.Font.Size = 50;
            sheet.Range["A3:E3"].Style.Font.Size = 25;
            sheet.Range["A4:E14"].Style.Font.Size = 12;
            //set excel cell data to be bold
            sheet.Range["A3:E3"].Style.Font.IsBold = true;
            //set excel cell data to be underline
            sheet.Range["A3:E3"].Style.Font.Underline = FontUnderlineType.Single;
            //set excel cell data color
            sheet.Range["A3:E3"].Style.Font.Color = Color.DarkOliveGreen;
            sheet.Range["A4:E14"].Style.Font.Color = Color.Firebrick;
            //set excel cell data to be italic
            sheet.Range["A4:E14"].Style.Font.IsItalic = true;

            //save and launch the file
            workbook.SaveToFile("sample.xlsx",ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("sample.xlsx");
        }
    }
}
[VB.NET]
Imports Spire.Xls

Namespace Excel_font
    Class Program
        Private Shared Sub Main(ByVal args() As String)
            'create a new workbook and load Excel data
            Dim workbook As Workbook = New Workbook
            workbook.LoadFromFile("..\Excel Font.xlsx")
            Dim sheet As Worksheet = workbook.Worksheets(0)
            'Set excel font style
            sheet.Range("A3:E3").Style.Font.FontName = "Comic Sans MS"
            sheet.Range("A4:E14").Style.Font.FontName = "Corbel"
            'set excel font size
            sheet.Range("A2:E2").Style.Font.Size = 50
            sheet.Range("A3:E3").Style.Font.Size = 25
            sheet.Range("A4:E14").Style.Font.Size = 12
            'set excel cell data to be bold
            sheet.Range("A3:E3").Style.Font.IsBold = true
            'set excel cell data to be underline
            sheet.Range("A3:E3").Style.Font.Underline = FontUnderlineType.Single
            'set excel cell data color
            sheet.Range("A3:E3").Style.Font.Color = Color.DarkOliveGreen
            sheet.Range("A4:E14").Style.Font.Color = Color.Firebrick
            'set excel cell data to be italic
            sheet.Range("A4:E14").Style.Font.IsItalic = true
            'save and launch the file
            workbook.SaveToFile("sample.xlsx",ExcelVersion.Version2010)
            System.Diagnostics.Process.Start("sample.xlsx")
        End Sub
    End Class
End Namespace
Wednesday, 05 January 2011 07:49

Set Cell Fill in Excel in C#, VB.NET

Cell Fill is one kind of cell format settings in Excel, which includes cell background color and pattern style. With cell fill, the appearance of Excel file can be beautified. Also, data in cell which is set background color or pattern style can be more obvious.

Spire.XLS for .NET, a professional .NET Excel component on manipulating Excel files, enables users to set in Excel. And this guide will focuses on how to realize Excel cell fill in C#, VB.NET, including cell background color and pattern style settings. User can set style property of CellRange class which Spire.XLS for .NET provides to get wanted background color and pattern style of one specified cell or a cell range. The following screenshot shows the result- workbook without cell fill setting.

Add Excel Cell Fill

Download and install Spire.XLS for .NET and use the following code to set cell fill.

Sample Code:

[C#]
   worksheet.Range["A7:F7"].Style.Color = Color.Yellow;
   worksheet.Range["A8:F8"].Style.FillPattern = ExcelPatternType.Percent125Gray;
[VB.NET]
  worksheet.Range("A7:F7").Style.Color = Color.Yellow
  worksheet.Range("A8:F8").Style.FillPattern = ExcelPatternType.Percent125Gray
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
Tuesday, 04 January 2011 08:26

Spire.XLS Program Guide Content for .NET

Spire.XLS is a versatile Excel library that is designed for software developers to perform a wide range of Excel processing tasks on .NET, Silverlight and WPF Platform. As a combination of APIs and GUI controls, Spire.XLS does not need to install MS Excel or any third party libraries and supports to apply Excel on the formats of either Excel .xls 97-2003 or Excel .xlsx 2007, 2010 and 2013.

This API gives developers powerful tools for performing simple tasks as well as more complex tasks. Basic operation tasks such as create, save, protect and merge Excel files, add/delete/hide worksheets, edit spreadsheet cell data, generate charts can be all realized with high efficiency.


Chart

Smart Marker