javajxls

JXLS - how to create hyperlink to Excel worksheets in workbook


I am trying to create an Excel workbook with with JXLS. I want a text hyperlink for navigating through worksheets in a workbook. I couldn't find any helpful information online. Please give any idea or hyperlink for that can help to to solve the problem. Thanks


Solution

  • jXLS is a small and easy-to-use Java library for writing Excel files using XLS templates and reading data from Excel into Java objects using XML configuration. If you are trying to create hyerlink, jXLS doen't have low lever excel manupulation capability. But you can to use Apache POI a free library. This code create hyperlink to a Cell for that task as shown below.

            //creating the cell
            Row row = my_sheet.createRow(0);                
            Cell cell = row.createCell(0);
    
            //creating helper class
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFCreationHelper helper= workbook.getCreationHelper();
    
            //creating the hyperlink
            link = helper.createHyperlink(HSSFHyperlink.LINK_DOCUMENT);
            link.setAddress("'target_worksheet_name'!A1");
    
            //optional hyperlink style
            XSSFCellStyle hlinkstyle = workbook.createCellStyle();
            XSSFFont hlinkfont = workbook.createFont();
            hlinkfont.setUnderline(XSSFFont.U_SINGLE);
            hlinkfont.setColor(HSSFColor.BLUE.index);
            hlinkstyle.setFont(hlinkfont);
    
            //applying the hyperlink to the cell
            cell.setHyperlink(link);