En savoir plus sur Xeed Workbooks pour .NET
Aujourd'hui, nous allons nous pencher sur l'ajout d'un nouvel élément à notre document xlsx : Les hyperliens.
Qu'est-ce qu'un lien hypertexte ?
Un lien hypertexte est un élément tel qu'un mot, une phrase ou un objet qui pointe vers un autre emplacement. Lorsque vous cliquez sur un lien, vous accédez à la cible de ce lien.
Dans le contexte des classeurs pour .NET, un lien hypertexte est un mot ou une phrase dans une cellule ou une plage de cellules donnée.
Le Lien hypertexte encapsule l'objet qui représente un lien hypertexte et possède les propriétés suivantes :
CellRange: the range of cells where the hyperlink is applied.
DestinationAddress: the destination address of the hyperlink.
TextToDisplay: the text that should be displayed if the existing text is to be replaced. The default value will be the DestinationAddress if no value is provided when the item is created.
Tooltip: the text that will be displayed when the user mouses over the hyperlink’s cells range.
Type: the hyperlink's type, supported values are:
Email: the hyperlink refers to an email address.
ExternalFile: The hyperlink refers to an external document.
Internal: The hyperlink refers to a Cell in the current Worksheet or from another Worksheet of the Workbook.
WebSite: The hyperlink refers to a website.
Accès aux hyperliens
Les liens hypertextes d'un classeur sont accessibles par le biais de la fonction Collection de liens hypertextes sur un Feuille de travail.
Le Collection de liens hypertextes contient tous les hyperliens de la feuille de calcul. Elle permet également à l'utilisateur d'ajouter de nouveaux hyperliens et de les manipuler.
Le Collection de liens hypertextes possède les propriétés suivantes :
Count: returns the number of Hyperlinks in this collection.
Item: returns the designated Hyperlink.
Ajout d'hyperliens à une feuille de calcul
Pour ajouter des hyperliens à une feuille de calcul, nous utilisons la fonction Ajouter disponible sur le site Collection d'hyperliens classe.
Le Ajouter offre 3 surcharges, la différence entre elles étant la manière dont l'emplacement du lien hypertexte dans la feuille de calcul est spécifié.
Option de surcharge 1
destinationLocation: the destination location of the hyperlink (a CellAddress from the current worksheet or from another worksheet, an external document, a website or an email address).
cellAdress: the address for the hyperlink's location in the worksheet.
hyperlinkRowLength: specifies on how many vertical cells the hyperlink be active (1 by default).
hyperlinkColumnLength: specifies on how many horizontal cells the hyperlink be active. (1 by default).
textToDisplay: the cell's text, if it is different from the existing one (null by default).
tooltip: the tooltip text that will be displayed on mouse over.
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
var worksheet = document.Worksheets[ 0 ];
// Format
// worksheet.Hyperlinks.Add( destinationLocation, cellAddress, hperlinkRowLength, hyperlinkColumnLength, textToDisplay, tooltip );
// Hyperlink Type: Internal (same worksheet)
worksheet.Cells[ "B2" ].Value = "Add an hyperlink to a cell reference in same worksheet:";
worksheet.Hyperlinks.Add( "Z1", "B3", 1, 1, "Link to another cell.", "A cell reference." );
// Hyperlink Type: Internal (other worksheet)
worksheet.Cells[ "B5" ].Value = "Add an hyperlink to a cell reference in another worksheet:";
worksheet.Hyperlinks.Add( "Sheet2!B1", "B6", 1, 2, "Link to another worksheet's cell.", "Another worksheet cell reference." );
// Hyperlink Type: ExternalFile
worksheet.Cells[ "B8" ].Value = "Add an hyperlink to an external document:";
worksheet.Hyperlinks.Add( "../abc.xlsx", "B9", 2, 3, "Link to another document.", "An external document link." );
// Hyperlink Type: Email
worksheet.Cells[ "B12" ].Value = "Add an hyperlink to an email address:";
worksheet.Hyperlinks.Add( "sales@xceed.com", "B13", 3, 2, null, "An email link." );
// Hyperlink Type: Website
worksheet.Cells[ "B17" ].Value = "Add an hyperlink to a web site:";
worksheet.Hyperlinks.Add( "www.xceed.com", "B18", 1, 2, "Xceed", "A web site link." );
}
Option de surcharge 2
destinationLocation: the destination location of the hyperlink (a CellAddress from the current worksheet or from another worksheet, an external document, a website or an email address).
topLeftRowId: the Id of the row where the hyperlink is located in the worksheet.
topLeffColumnId: the Id of the column where the hyperlink is located in the worksheet.
hyperlinkRowLength: specifies on how many vertical cells the hyperlink be active (1 by default).
hyperlinkColumnLength: specifies on how many horizontal cells the hyperlink be active. (1 by default).
textToDisplay: the cell's text, if it is different from the existing one (null by default).
tooltip: the tooltip text that will be displayed on mouse over.
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
var worksheet = document.Worksheets[ 0 ];
// Format
// worksheet.Hyperlinks.Add( destinationLocation, topLeftRowId, topLeftColumnId, hperlinkRowLength, hyperlinkColumnLength, textToDisplay, tooltip );
// Hyperlink Type: Internal (same worksheet)
worksheet.Cells[ "B2" ].Value = "Add an hyperlink to a cell reference in same worksheet:";
worksheet.Hyperlinks.Add( "Z1", 2, 1, 1, 1, "Link to another cell.", "A cell reference." );
// Hyperlink Type: Internal (other worksheet)
worksheet.Cells[ "B5" ].Value = "Add an hyperlink to a cell reference in another worksheet:";
worksheet.Hyperlinks.Add( "Sheet2!B1", 5, 1, 1, 2, "Link to another worksheet's cell.", "Another worksheet cell reference." );
// Hyperlink Type: ExternalFile
worksheet.Cells[ "B8" ].Value = "Add an hyperlink to an external document:";
worksheet.Hyperlinks.Add( "../abc.xlsx", 8, 1, 2, 3, "Link to another document.", "An external document link." );
// Hyperlink Type: Email
worksheet.Cells[ "B12" ].Value = "Add an hyperlink to an email address:";
worksheet.Hyperlinks.Add( "sales@xceed.com", 12, 1, 3, 2, null, "An email link." );
// Hyperlink Type: Website
worksheet.Cells[ "B12" ].Value = "Add an hyperlink to an email address:";
worksheet.Hyperlinks.Add( "www.xceed.com", 17, 1, 1, 2, "Xceed", "A web site link." );
}
Option de surcharge 3
destinationLocation: the destination location of the hyperlink (a CellAddress from the current worksheet or from another worksheet, an external document, a website or an email address).
topLeftCellAddress: the top left cell address for the location of the hyperlink in the worksheet.
bottomRightCellAddress: the bottom right cell address for the location of the hyperlink in the worksheet.
textToDisplay: the cell's text, if it is different from the existing one (null by default).
tooltip: the tooltip text that will be displayed on mouse over.
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
var worksheet = document.Worksheets[ 0 ];
// Format
// worksheet.Hyperlinks.Add( destinationLocation, topLeftCellAddress, bottomRightCellAddress, textToDisplay, tooltip );
// Hyperlink Type: Internal (same worksheet)
worksheet.Cells[ "B2" ].Value = "Add an hyperlink to a cell reference in same worksheet:";
worksheet.Hyperlinks.Add( "Z1", "B3", "B3", "Link to another cell.", "A cell reference." );
// Hyperlink Type: Internal (other worksheet)
worksheet.Cells[ "B5" ].Value = "Add an hyperlink to a cell reference in another worksheet:";
worksheet.Hyperlinks.Add( "Sheet2!B1", "B6", "C6", "Link to another worksheet's cell.", "Another worksheet cell reference." );
// Hyperlink Type: ExternalFile
worksheet.Cells[ "B8" ].Value = "Add an hyperlink to an external document:";
worksheet.Hyperlinks.Add( "../abc.xlsx", "B9", "D10", "Link to another document.", "An external document link." );
// Hyperlink Type: Email
worksheet.Cells[ "B12" ].Value = "Add an hyperlink to an email address:";
worksheet.Hyperlinks.Add( "sales@xceed.com", "B13", "C15", null, "An email link." );
// Hyperlink Type: Website
worksheet.Cells[ "B12" ].Value = "Add an hyperlink to an email address:";
worksheet.Hyperlinks.Add( "www.xceed.com", "B18", "C18", "Xceed", "A web site link." );
}
Pour plus d'informations, veuillez vous référer à la la documentation.