本文介绍了如何以 POI Word 格式创建电子邮件链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 XWPFDocument?有对 Excel (HSSF XSSF) 的描述,但我没有t 在 Word (HWPF XWPF) 中找到了类似的东西.

How can I create an external link or an email link in a XWPFDocument? There is a description for Excel (HSSF XSSF), but i haven't found anything similar for 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});
    }

这篇关于如何以 POI Word 格式创建电子邮件链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:53