如何使用Apache POI添加超链接到Excel简单的形状?(How to add hyperli

2019-10-18 04:42发布

我使用Microsoft Excel 2007年我第一个excel表一个简单的形状。 我想在这个简单的形状,其是指在特定行和列中的另一片添加的超链接。 我做这个研究,但我只找到其中​​演示了如何将超链接添加到给定单元的例子。 我如何将超链接添加到一个给定的简单的形状与Apache POI的帮助?

Answer 1:

我发现我的问题的解决方案。 我希望这会帮助别人。

    // 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);


文章来源: How to add hyperlink to Excel simple shape using apache poi?