java - 如何创建 POI Word 格式的电子邮件链接

标签 java apache-poi

如何在 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});
    }

关于java - 如何创建 POI Word 格式的电子邮件链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7007810/

相关文章:

java - 将查询中的可变列数导出到 Excel

java - 如何使用 jexcelapi 或 Apache HSSF 跟踪更改

Android - 创建word文档

java - "hasNextInt()"扫描仪问题

java - 如何删除 URL 的子域部分

java - 绝对值java

java - Java 中的不变性

java - 我怎样才能绕过这个无效的类加载器层次结构?

java - 使用 POI 将 HTML 数据表导出到 Excel

apache-poi - 使用 apache poi 在合并的单元格中水平居中图像