Detect Hyperlink Type

An Excel file can have different types of hyperlinks like external, cell reference, file path, etc. Aspose.Cells supports the feature to detect the type of hyperlink. The types of hyperlinks are represented by the TargetModeType Enumeration. The TargetModeType Enumeration has the following members.

To check the type of hyperlink, the Hyperlink class provides a LinkType property with a return type of TargetModeType. The following code snippet demonstrates the use of the LinkType property by using this source excel file.

Source Code

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
public static void main(String[] args) throws Exception {
// The path to the directories.
String sourceDir = Utils.Get_SourceDirectory();
Workbook workbook = new Workbook(sourceDir + "LinkTypes.xlsx");
// Get the first (default) worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Create a range A2:B3
Range range = worksheet.getCells().createRange("A1", "A7");
// Get Hyperlinks in range
Hyperlink[] hyperlinks = range.getHyperlinks();
for (Hyperlink link : hyperlinks)
{
System.out.println(link.getTextToDisplay() + ": " + getLinkTypeName(link.getLinkType()));
}
System.out.println("DetectLinkTypes executed successfully.");
}
private static String getLinkTypeName(int linkType){
if(linkType == TargetModeType.EXTERNAL){
return "EXTERNAL";
} else if(linkType == TargetModeType.FILE_PATH){
return "FILE_PATH";
} else if(linkType == TargetModeType.EMAIL){
return "EMAIL";
} else {
return "CELL_REFERENCE";
}
}

The following is the output generated by the code snippet given above.

Console Output

LinkTypes.xlsx: FILE_PATH </br>
C:\Windows\System32\cmd.exe: FILE_PATH </br>
C:\Program Files\Common Files: FILE_PATH </br>
'Test Sheet'!B2: CELL_REFERENCE </br>
FullPathExample: CELL_REFERENCE </br>
https://products.aspose.com/cells/ : EXTERNAL </br>
mailto:test@test.com?subject=TestLink: EMAIL