1
我使用Microsoft Excel 2007.我在第一張Excel表格上有一個簡單的形狀。我想在這個簡單的形狀上添加一個超鏈接,它指向特定行和列中的另一個表單。我對此進行了研究,但是我只找到了演示如何將超鏈接添加到給定單元格的示例。在Apache POI的幫助下,如何將超鏈接添加到給定的簡單形狀?如何使用apache poi將超鏈接添加到Excel簡單形狀?
我使用Microsoft Excel 2007.我在第一張Excel表格上有一個簡單的形狀。我想在這個簡單的形狀上添加一個超鏈接,它指向特定行和列中的另一個表單。我對此進行了研究,但是我只找到了演示如何將超鏈接添加到給定單元格的示例。在Apache POI的幫助下,如何將超鏈接添加到給定的簡單形狀?如何使用apache poi將超鏈接添加到Excel簡單形狀?
我找到了我的問題的解決方案。我希望這會對其他人有所幫助。
// Example for hyperlink address
// String hyperlinkAddress = sheet.getPackagePart().getPartName() + "/#'Sheet name'!Cell Address"
// My hyperlink address
String hyperlinkAddress = sheet.getPackagePart().getPartName()
+ "/#'List_of_Items'!A12";
// Create URI object which will containing our hyperlink address.
URI uri = new URI(null, null, hyperlinkAddress, null, null);
// Add relationship to XSSFDrawing object.
aPatriarch.getPackagePart().addRelationship(uri, TargetMode.INTERNAL,
XSSFRelation.SHEET_HYPERLINKS.getRelation());
// We need to extract the ID of the Relationship.
// We'll set the ID of the hyperlink to be the same as ID of the Relationship.
// To find appropriate Relationship we will traverse through all Relationships in the 'aPatriarch' object.
String relationshipId = null;
PackageRelationshipCollection prc = aPatriarch.getPackagePart().getRelationships();
for (PackageRelationship pr : prc) {
URI targetURI = pr.getTargetURI();
String target = targetURI.getPath();
if (target.equals(hyperlinkAddress)) {
relationshipId = pr.getId();
break;
}
}
// Create the hyperlink object
CTHyperlink hyperlink = CTHyperlink.Factory.newInstance();
// Set ID of hyperlink to be the same as Relationship ID
hyperlink.setId(relationshipId);
// Add hyperlink to the given shape
shape.getCTShape().getNvSpPr().getCNvPr().setHlinkClick(hyperlink);