Introduction
Hyperlinks can quickly link the current text or picture to a given destination address, providing us with great convenience in everyday work. This article describes how to add hyperlinks to Excel tables in the C # language through the Free edition component, which will include the following points:
1. Add links to Web pages (text, pictures)
1.1 Linking to text
1.2 Link to Picture
2. Add a link to the specified document
3. Add a link to the specified cell
using Tools
- Free Spire.xls for. NET 8.3 (Community edition)
- Visual Studio
PS : Download Install the component and Add reference to the component Spire.Xls.dll to the project program (e.g.), DLL files can be obtained in the Bin folder under the installation path.
Sample Code Action1. Add links to Web pages
(There are two cases where you add a text link and a picture link.) )
1.1 Adding hyperlinks to text strings
Step 1: Create Excel to get worksheets
New= wb. worksheets[0];
Step 2: Get the cell, add the text, and set the alignment
Sheet. range["A1 " " Green transpotation"; sheet. range["A1"]. Style.verticalalignment = Verticalaligntype.bottom;
Step 3: Set the hyperlink to the specified cell
HyperLink Urllink = sheet. Hyperlinks.add (sheet. range["A1"= sheet. range["A1"="https://baike.baidu.com/item ";
1.2 Link to Picture
Step 1: Initialize a string class to load the picture
string @" C:\Users\Administrator\Desktop\images\th.jpg ";
Step 2: Add a picture to the A1 cell and set the hyperlink
Excelpicture picture = sheet. Pictures.add (11, picpath); Picture. Sethyperlink ("https://en.wikipedia.org/wiki/Sustainable_transport"True );
Step 3: Adjust the position of the picture in the cell
Sheet. columns[0; sheet. rows[0;
Finally, save the document
Wb. SaveToFile ("hyperlink.xlsx", excelversion.version2013); System.Diagnostics.Process.Start ("hyperlink.xlsx");
After debugging runs the project program, generate the document as shown in:
All code:
usingSpire.xls;namespacetexthyperlink_xls{classProgram {Static voidMain (string[] args) { //Add a text hyperlink//Create a workbook class object to get the first worksheetWorkbook WB =NewWorkbook (); Worksheet sheet= WB. worksheets[0]; //get the first cell to add text and set the text alignmentSheet. range["A1"]. Text ="Green Transport (transpotation)"; Sheet. range["A1"]. Style.verticalalignment =Verticalaligntype.bottom; //Create a Hyperlink class object, set the text hyperlink in cell A1HyperLink Urllink = sheet. Hyperlinks.add (sheet. range["A1"]); Urllink.texttodisplay= Sheet. range["A1"]. Text; Urllink.type=Hyperlinktype.url; Urllink.address="Https://baike.baidu.com/item"; //add a picture hyperlink. //Initializes a string class, loads the picture stringPicpath =@"C:\Users\Administrator\Desktop\images\th.jpg"; //add a picture to a A1 cell and set a hyperlinkExcelpicture picture = sheet. Pictures.add (1,1, Picpath); Picture. Sethyperlink ("Https://en.wikipedia.org/wiki/Sustainable_transport",true); //set the position of a picture in a cellSheet. columns[0]. ColumnWidth = -; Sheet. rows[0]. RowHeight = -; Picture. Toprowoffset= -; //Save and open a fileWb. SaveToFile ("hyperlink.xlsx", excelversion.version2013); System.Diagnostics.Process.Start ("hyperlink.xlsx"); } }}
View Code
2. Add link to document
"C #"
//instantiate a workbook class and load an Excel documentWorkbook Workbook =NewWorkbook (); Workbook. LoadFromFile (@"C:\Users\Administrator\Desktop\Sample.xlsx");//Get first worksheetWorksheet sheet = workbook. worksheets[0];//set hyperlink to specified cellCellRange range = Sheet. range["E2"]; HyperLink Filelink=sheet. Hyperlinks.add (range); Filelink.type=Hyperlinktype.file; Filelink.texttodisplay= Sheet. range["E2"]. Text; Filelink.address=@"C:\Users\Administrator\Desktop\test.docx";//Save and open a documentWorkbook. SaveToFile ("filelink.xlsx"); System.Diagnostics.Process.Start ("filelink.xlsx");
Examples of effects:
3. Add a link to the specified cell
"C #"
//Create a new Excel class object, load the Excel document, get the first worksheetWorkbook Workbook =NewWorkbook (); Workbook. LoadFromFile (@"C:\Users\Administrator\Desktop\Sample.xlsx"); Worksheet sheet= Workbook. worksheets[0];//gets the specified cell, linked to a specific cell in the specified documentCellRange range = Sheet. range["E2"]; HyperLink Wblink=sheet. Hyperlinks.add (range); Wblink.type=Hyperlinktype.workbook; Wblink.texttodisplay="has been liquidated"; Wblink.address="a Account Details! A1";//Save and open a documentWorkbook. SaveToFile ("linktocell.xlsx", excelversion.version2013); System.Diagnostics.Process.Start ("linktocell.xlsx");
Effect Show:
4. Add a linked UNC path
"C #"
//Create a Workbook class object, load an Excel document, get a second worksheetWorkbook Workbook =NewWorkbook (); Workbook. LoadFromFile (@"C:\Users\Administrator\Desktop\Sample.xlsx"); Worksheet sheet= Workbook. worksheets[1];//Add UNC link to cell A1, set connection type to UNC, add display text and link pathCellRange range = Sheet. range["A1"]; HyperLink Unclink=sheet. Hyperlinks.add (range); Unclink.type=Hyperlinktype.unc; Unclink.texttodisplay="Address"; Unclink.address="\\192.168.1.118";//Save DocumentWorkbook. SaveToFile ("Linktounc.xls");
Examples of effects:
The above is all about adding an Excel hyperlink.
< end of this article >
If you want to reprint, please indicate the source.
C # Set Excel Hyperlinks (ii)