Syncfusion AI Assistant

How can I help you?

How to find and replace text in hyperlinks?

24 Jun 20264 minutes to read

The following code illustrates how to find and replace text in hyperlinks.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;

    //Loads an existing file.
    IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
    IWorksheet sheet = workbook.Worksheets[0];

    //Find and Replace text in hyperlinks
    for (int i = 0; i < sheet.HyperLinks.Count; i++)
    {
        IHyperLink hyperLink = sheet.HyperLinks[i];
        string address = hyperLink.Address;
        string displayText = hyperLink.TextToDisplay;
        hyperLink.Address = address.Replace("http://", "https://");
        if (!string.IsNullOrEmpty(displayText))
            hyperLink.TextToDisplay = displayText.Replace("http://", "https://");
    }
    
    // Saving the workbook
    workbook.SaveAs("Output.xlsx");
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;

    //Loads an existing file.
    IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
    IWorksheet sheet = workbook.Worksheets[0];

    //Find and Replace text in hyperlinks
    for (int i = 0; i < sheet.HyperLinks.Count; i++)
    {
        IHyperLink hyperLink = sheet.HyperLinks[i];
        string address = hyperLink.Address;
        string displayText = hyperLink.TextToDisplay;
        hyperLink.Address = address.Replace("http://", "https://");
        if (!string.IsNullOrEmpty(displayText))
            hyperLink.TextToDisplay = displayText.Replace("http://", "https://");
    }

    // Saving the workbook
    workbook.SaveAs("Output1.xlsx");
}
Using excelEngine As ExcelEngine = New ExcelEngine()
    Dim application As IApplication = excelEngine.Excel
    application.DefaultVersion = ExcelVersion.Xlsx

    'Loads an existing file.
    Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")
    Dim sheet As IWorksheet = workbook.Worksheets(0)

    'Find and Replace text in hyperlinks
    For i As Integer = 0 To sheet.HyperLinks.Count - 1
        Dim hyperLink As IHyperLink = sheet.HyperLinks(i)
        Dim address As String = hyperLink.Address
        Dim displayText As String = hyperLink.TextToDisplay
        hyperLink.Address = address.Replace("http://", "https://")
        If Not String.IsNullOrEmpty(displayText) Then
            hyperLink.TextToDisplay = displayText.Replace("http://", "https://")
        End If
    Next

    'Saving the workbook
    workbook.SaveAs("Output.xlsx")
End Using