检测超链接类型
Contents
[
Hide
]
检测超链接类型
Excel文件可以具有不同类型的超链接,如外部链接、单元引用、文件路径等。Aspose.Cells支持检测超链接的类型功能。超链接的类型由TargetModeType枚举表示。TargetModeType枚举具有以下成员。
- EXTERNAL:外部链接
- FILE_PATH:本地和完整的文件/文件夹路径。
- EMAIL:电子邮件
- CELL_REFERENCE:链接到单元格或命名范围。
要检查超链接的类型,Hyperlink类提供一个LinkType属性,返回类型为TargetModeType。以下代码片段演示了如何使用这个LinkType属性使用此源Excel文件。
源代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"; | |
} | |
} |
以下是上述代码片段生成的输出。
控制台输出
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