2017-03-16 100 views
0

我不能使用XWPFTableRow將超鏈接插入單元中的其他文檔。將超鏈接插入到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"); 

我要轉換的文本中的超鏈接

感謝

+0

我看不到任何代碼處理中的超鏈接片段。你試過哪些代碼不起作用? – Gagravarr

回答

0

對不起。我只是想轉換的文本「服務:entidad] .rest.be#添加」,在一個超鏈接

我最後的解決辦法是:

//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}); 
}