Wednesday, 16 September 2020 03:20

Modify and delete Hyperlinks in Excel in Java

Written by  support iceblue
Rate this item
(0 votes)

This article will show you how to modify and delete hyperlinks in Excel using Spire.XLS for Java. For how to insert and read hyperlinks, you can check this tutorial.

Modify hyperlinks

import com.spire.xls.*;
import com.spire.xls.collections.HyperLinksCollection;

public class ModifyHyperlink {
    public static void main(String[] args) {
        //Create a workbook and load an Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");

        //Get the collection of all hyperlinks in the worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Change the values of TextToDisplay and Address property
        HyperLinksCollection links = sheet.getHyperLinks();
        links.get(0).setTextToDisplay("Republic of Indonesia");
   links.get(0).setAddress("https://baike.baidu.com/item/%E5%8D%B0%E5%BA%A6%E5%B0%BC%E8%A5%BF%E4%BA%9A/129972?fr=aladdin");

        //Save the document
        workbook.saveToFile("output/ModifyHyperlink.xlsx", ExcelVersion.Version2013);
   

Modify and delete Hyperlinks in Excel in Java

Delete Hyperlinks

import com.spire.xls.*;
import com.spire.xls.collections.HyperLinksCollection;
public class RemoveHyperlink {
    public static void main(String[] args) {
        //Create a workbook and load an Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Get the collection of all hyperlinks in the worksheet
        HyperLinksCollection links = sheet.getHyperLinks();

        //Remove hyperlink and keep link text
        sheet.getHyperLinks().removeAt(0);
        sheet.getHyperLinks().removeAt(0);

        //Save the document
        String output = "output/RemoveHyperlink.xlsx";
        workbook.saveToFile(output, ExcelVersion.Version2013);
    }
}

Modify and delete Hyperlinks in Excel in Java

Additional Info

  • tutorial_title: