加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何在POI Word格式中创建电子邮件链接

发布时间:2020-12-15 05:19:46 所属栏目:Java 来源:网络整理
导读:如何在 XWPFDocument中创建外部链接或电子邮件链接?有一个关于 Excel (HSSF XSSF)的描述,但我没有找到任何类似的Word(HWPF XWPF). 解决方法 public void example() throws Exception{ XWPFDocument document = new XWPFDocument(); //Append a link to appe
如何在 XWPFDocument中创建外部链接或电子邮件链接?有一个关于 Excel (HSSF XSSF)的描述,但我没有找到任何类似的Word(HWPF XWPF).

解决方法

public void example() throws Exception{

        XWPFDocument document = new XWPFDocument(); 
        //Append a link to 
        appendExternalHyperlink("https://poi.apache.org"," Link to POI",document.createParagraph());

        document.write(new FileOutputStream("resultat.docx"));
    }

    /**
     * Appends an external hyperlink to the paragraph.
     * 
     * @param url The URL to the external target
     * @param text The linked text
     * @param paragraph the paragraph the link will be appended to.
     */
    public static void appendExternalHyperlink(String url,String text,XWPFParagraph paragraph){

        //Add the link as External relationship
        String id=paragraph.getDocument().getPackagePart().addExternalRelationship(url,XWPFRelation.HYPERLINK.getRelation()).getId();

        //Append the link and bind it to the relationship
        CTHyperlink cLink=paragraph.getCTP().addNewHyperlink();
        cLink.setId(id);

        //Create the linked text
        CTText ctText=CTText.Factory.newInstance();
        ctText.setStringValue(text);
        CTR ctr=CTR.Factory.newInstance();
        ctr.setTArray(new CTText[]{ctText});

        //Insert the linked text into the link
        cLink.setRArray(new CTR[]{ctr});
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读