【问题标题】:How to add hyperlink to Excel simple shape using apache poi?如何使用 apache poi 将超链接添加到 Excel 简单形状?
【发布时间】:2013-06-03 07:14:33
【问题描述】:

我使用 Microsoft Excel 2007。我在第一个 Excel 表上有一个简单的形状。我想在这个简单的形状上添加一个超链接,它引用特定行和列上的另一张表。我对此进行了研究,但我只找到了演示如何向给定单元格添加超链接的示例。如何在 Apache POI 的帮助下将超链接添加到给定的简单形状?

【问题讨论】:

    标签: excel-2007 apache-poi openxml


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-02
      • 2015-09-22
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      相关资源
      最近更新 更多