2013-06-03 3 views
1

Microsoft Excel 2007을 사용합니다. 첫 번째 Excel 시트에는 간단한 모양이 있습니다. 특정 행과 열의 다른 시트를 참조하는이 간단한 모양의 하이퍼 링크를 추가하고 싶습니다. 나는 이것에 대한 연구를하지만 특정 셀에 하이퍼 링크를 추가하는 방법을 보여주는 예제 만 발견했습니다. 아파치 POI의 도움으로 주어진 간단한 모양에 하이퍼 링크를 어떻게 추가 할 수 있습니까?apache poi를 사용하여 Excel에 간단한 하이퍼 링크를 추가하는 방법은 무엇입니까?

답변

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); 
관련 문제