@@ -34,6 +34,101 @@ public Cell(DocumentFormat.OpenXml.Spreadsheet.Cell cell, SheetData sheetData, W
3434 _workbookPart = workbookPart ?? throw new ArgumentNullException ( nameof ( workbookPart ) ) ;
3535 }
3636
37+ /// <summary>
38+ /// Adds a hyperlink to the specified cell with an optional tooltip.
39+ /// </summary>
40+ /// <param name="hyperlinkUrl">The URL of the hyperlink to be added.</param>
41+ /// <param name="tooltip">The optional tooltip to display when hovering over the hyperlink.</param>
42+ /// <exception cref="ArgumentException">
43+ /// Thrown when <paramref name="hyperlinkUrl"/> is null or empty.
44+ /// </exception>
45+ /// <exception cref="InvalidOperationException">
46+ /// Thrown when the parent WorksheetPart or the OpenXML Worksheet cannot be found.
47+ /// </exception>
48+ /// <remarks>
49+ /// This method creates a hyperlink relationship in the parent worksheet and adds a Hyperlink element
50+ /// to the cell's reference in the worksheet. The method does not modify the cell's text; use <see cref="PutValue"/>
51+ /// to set display text if needed.
52+ /// </remarks>
53+ /// <example>
54+ /// The following example demonstrates how to use the <c>SetHyperlink</c> method:
55+ /// <code>
56+ /// using (Workbook wb = new Workbook("path/to/your/file.xlsx"))
57+ /// {
58+ /// Worksheet sheet = wb.Worksheets[0];
59+ /// Cell cell = sheet.Cells["A1"];
60+ /// cell.PutValue("Click Me");
61+ /// cell.SetHyperlink("https://example.com", "Visit Example");
62+ /// wb.Save("path/to/your/file.xlsx");
63+ /// }
64+ /// </code>
65+ /// </example>
66+ public void SetHyperlink ( string hyperlinkUrl , string tooltip = null )
67+ {
68+ if ( string . IsNullOrEmpty ( hyperlinkUrl ) )
69+ throw new ArgumentException ( "Hyperlink URL cannot be null or empty." , nameof ( hyperlinkUrl ) ) ;
70+
71+ // Get the WorksheetPart for this cell
72+ WorksheetPart worksheetPart = GetWorksheetPart ( ) ;
73+ if ( worksheetPart == null )
74+ throw new InvalidOperationException ( "WorksheetPart is not available for this cell." ) ;
75+
76+ // Get the underlying OpenXML Worksheet
77+ var openXmlWorksheet = worksheetPart . Worksheet ;
78+ if ( openXmlWorksheet == null )
79+ throw new InvalidOperationException ( "The OpenXML Worksheet is not available." ) ;
80+
81+ // Ensure the Hyperlinks collection exists
82+ Hyperlinks hyperlinks = openXmlWorksheet . GetFirstChild < Hyperlinks > ( ) ;
83+ if ( hyperlinks == null )
84+ {
85+ hyperlinks = new Hyperlinks ( ) ;
86+ openXmlWorksheet . InsertAfter ( hyperlinks , openXmlWorksheet . GetFirstChild < SheetData > ( ) ) ;
87+ }
88+
89+ // Create a unique relationship ID
90+ string relationshipId = "rId" + Guid . NewGuid ( ) . ToString ( ) ;
91+
92+ // Add the hyperlink relationship to the worksheet part
93+ worksheetPart . AddHyperlinkRelationship ( new Uri ( hyperlinkUrl , UriKind . Absolute ) , true , relationshipId ) ;
94+
95+ // Create and add the Hyperlink element
96+ Hyperlink hyperlink = new Hyperlink
97+ {
98+ Reference = CellReference , // The reference of this cell in A1 notation
99+ Tooltip = tooltip ,
100+ Id = relationshipId
101+ } ;
102+ hyperlinks . Append ( hyperlink ) ;
103+
104+ // Save changes to the worksheet
105+ openXmlWorksheet . Save ( ) ;
106+ }
107+
108+ /// <summary>
109+ /// Retrieves the WorksheetPart containing this cell.
110+ /// </summary>
111+ /// <returns>The <see cref="WorksheetPart"/> that contains the current cell.</returns>
112+ /// <exception cref="InvalidOperationException">
113+ /// Thrown if the WorksheetPart cannot be located.
114+ /// </exception>
115+ /// <remarks>
116+ /// This method searches all WorksheetParts in the parent WorkbookPart and identifies
117+ /// the one containing the current SheetData.
118+ /// </remarks>
119+ private WorksheetPart GetWorksheetPart ( )
120+ {
121+ foreach ( var worksheetPart in _workbookPart . GetPartsOfType < WorksheetPart > ( ) )
122+ {
123+ if ( worksheetPart . Worksheet . Descendants < SheetData > ( ) . Contains ( _sheetData ) )
124+ {
125+ return worksheetPart ;
126+ }
127+ }
128+
129+ throw new InvalidOperationException ( "Unable to find the WorksheetPart containing this cell." ) ;
130+ }
131+
37132 /// <summary>
38133 /// Sets the value of the cell as a string.
39134 /// </summary>
0 commit comments