将超链接插入XWPFTableRow

时间:2017-03-16 07:54:50

标签: java hyperlink apache-poi tablerow settext

我无法使用XWPFTableRow在单元格中插入超链接到其他文档。

    XWPFTableRow tableRow = table.createRow();
    //Nombre GUI / Botón
    tableRow.getCell(0).setColor(cellColorImPar);
    tableRow.getCell(0).setText("Añadir");
    //Estado
    tableRow.getCell(1).setColor(cellColorImPar);
    tableRow.getCell(1).setText("Service: [entidad].rest.be#add");

我想在超链接中转换文本

由于

1 个答案:

答案 0 :(得分:0)

对不起。我只想转换文字"服务:[entidad] .rest.be #add"在超链接

我的最终解决方案是:

    //Fila 1 - Añadir
    XWPFTableRow tableRow = table.createRow();
    //Nombre GUI / Botón
    tableRow.getCell(0).setColor(cellColorImPar);
    tableRow.getCell(0).setText("Añadir");

    tableRow.getCell(1).setColor(cellColorImPar);
    XWPFParagraph para1 = tableRow.getCell(1).getParagraphs().get(0);
    para1.setAlignment(ParagraphAlignment.LEFT);
    XWPFRun run1 = para1.createRun();
    run1.setText("Habilitar el botón si tiene ROLE de MODIFICACION");
    run1.addBreak();
    run1 = para1.createRun();
    appendExternalHyperlink("../Servicios/"+name+"_Service.docx","1. Servicio del botón guardar: "+prepareName+".rest.be#add",run1);
    run1.addBreak();


public static void appendExternalHyperlink(String url, String text, XWPFRun run){

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

    //Append the link and bind it to the relationship
    CTHyperlink cLink=run.getParagraph().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});

     //Create the formatting
     CTRPr rpr = ctr.addNewRPr();
     CTColor colour = CTColor.Factory.newInstance();
     colour.setVal("0000FF");
     rpr.setColor(colour);
     CTRPr rpr1 = ctr.addNewRPr();
     rpr1.addNewU().setVal(STUnderline.SINGLE);

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