Search code examples
javajxl

Specifying Cell format for WritableHyperlink using jxl for java


I'm using jxl(a Java based API for MS excel file manipulation) to create excel reports. I'm inserting the hyperlink like this

//sheet is WritableSheet
//adding hyperlink to cell 0,0 of the sheet
WritableHyperlink hl = new WritableHyperlink(0, 0, "http://www.google.com", "home page");
sheet.addHyperlink(hl);

This works fine, but it displays the data in default cell format, which is white colored cell and blue font. Is there any way by which I can specify cell format for this hyperlink like it is done for a label or number. This is important because this hyperlink opens the error screenshot, so as per the specification the cell should be in Red color.

Thanks


Solution

  • Set a label formatted how you'd like for the same cell as the hyperlink, e.g.:

        WritableHyperlink hl = new WritableHyperlink(0, 0, 
                new URL("http://www.google.com"));
        sheet.addHyperlink(hl);
    
        WritableFont redFont = new WritableFont(WritableFont.ARIAL);
        redFont.setColour(Colour.RED);
        WritableCellFormat cellFormat = new WritableCellFormat(redFont);
        Label label = new Label(0, 0, "home page", cellFormat);
        sheet.addCell(label);