Example usage for org.apache.poi.ss.usermodel Hyperlink setAddress

List of usage examples for org.apache.poi.ss.usermodel Hyperlink setAddress

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Hyperlink setAddress.

Prototype

public void setAddress(String address);

Source Link

Document

Hyperlink address.

Usage

From source file:fr.ens.biologie.genomique.eoulsan.translators.io.XLSXTranslatorOutputFormat.java

License:LGPL

@Override
public void writeLink(final String text, final String link) throws IOException {

    final Cell cell = this.row.createCell(this.colCount++);

    if (text != null) {

        if (link != null) {
            Hyperlink hyperlink = createHelper.createHyperlink(Hyperlink.LINK_URL);
            hyperlink.setAddress(link);
            cell.setHyperlink(hyperlink);
        }/*from w  w  w.ja va2 s  .c om*/

        cell.setCellValue(new XSSFRichTextString(text));
    }

}

From source file:net.sf.jasperreports.engine.export.JRXlsExporter.java

License:Open Source License

protected void closeWorkbook(OutputStream os) throws JRException {
    try {/*www .jav  a  2 s.  co  m*/
        for (Object anchorName : anchorNames.keySet()) // the anchorNames map contains no entries for reports with ignore anchors == true;
        {
            HSSFName anchor = anchorNames.get(anchorName);
            List<Hyperlink> linkList = anchorLinks.get(anchorName);
            anchor.setRefersToFormula(
                    "'" + workbook.getSheetName(anchor.getSheetIndex()) + "'!" + anchor.getRefersToFormula());

            if (linkList != null && !linkList.isEmpty()) {
                for (Hyperlink link : linkList) {
                    link.setAddress(anchor.getRefersToFormula());
                }
            }
        }

        int index = 0;
        for (Integer linkPage : pageLinks.keySet()) { // the pageLinks map contains no entries for reports with ignore hyperlinks == true 
            List<Hyperlink> linkList = pageLinks.get(linkPage);
            if (linkList != null && !linkList.isEmpty()) {
                for (Hyperlink link : linkList) {
                    index = onePagePerSheetMap.get(linkPage - 1) != null
                            ? (onePagePerSheetMap.get(linkPage - 1) ? Math.max(0, linkPage - 1)
                                    : Math.max(0, sheetsBeforeCurrentReportMap.get(linkPage)))
                            : 0;
                    link.setAddress("'" + workbook.getSheetName(index) + "'!$A$1");
                }
            }
        }

        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
            workbook.getSheetAt(i).setForceFormulaRecalculation(true);
        }

        workbook.write(os);
    } catch (IOException e) {
        throw new JRException("Error generating XLS report : " + jasperPrint.getName(), e);
    }
}

From source file:net.sf.jasperreports.engine.export.JRXlsExporter.java

License:Open Source License

protected void setHyperlinkCell(JRPrintHyperlink hyperlink) {
    Hyperlink link = null;

    Boolean ignoreHyperlink = HyperlinkUtil.getIgnoreHyperlink(XlsReportConfiguration.PROPERTY_IGNORE_HYPERLINK,
            hyperlink);//from  w ww  .  j  a  va  2 s  .  c o  m
    if (ignoreHyperlink == null) {
        ignoreHyperlink = getCurrentItemConfiguration().isIgnoreHyperlink();
    }

    //test for ignore hyperlinks done here
    if (!ignoreHyperlink) {
        JRHyperlinkProducer customHandler = getHyperlinkProducer(hyperlink);
        if (customHandler == null) {
            switch (hyperlink.getHyperlinkTypeValue()) {
            case REFERENCE: {
                String href = hyperlink.getHyperlinkReference();
                if (href != null) {
                    link = createHelper.createHyperlink(Hyperlink.LINK_URL);
                    link.setAddress(href);
                }
                break;
            }
            case LOCAL_ANCHOR: {
                //test for ignore anchors done here
                if (!getCurrentItemConfiguration().isIgnoreAnchors()) {
                    String href = hyperlink.getHyperlinkAnchor();
                    if (href != null) {
                        link = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
                        if (anchorLinks.containsKey(href)) {
                            (anchorLinks.get(href)).add(link);
                        } else {
                            List<Hyperlink> hrefList = new ArrayList<Hyperlink>();
                            hrefList.add(link);
                            anchorLinks.put(href, hrefList);
                        }

                    }
                }
                break;
            }
            case LOCAL_PAGE: {
                Integer hrefPage = (getCurrentItemConfiguration().isOnePagePerSheet()
                        ? hyperlink.getHyperlinkPage()
                        : 0);
                if (hrefPage != null) {
                    link = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
                    if (pageLinks.containsKey(sheetsBeforeCurrentReport + hrefPage)) {
                        pageLinks.get(sheetsBeforeCurrentReport + hrefPage).add(link);
                    } else {
                        List<Hyperlink> hrefList = new ArrayList<Hyperlink>();
                        hrefList.add(link);
                        pageLinks.put(sheetsBeforeCurrentReport + hrefPage, hrefList);
                    }
                }
                break;
            }
            case REMOTE_ANCHOR: {
                String href = hyperlink.getHyperlinkReference();
                if (href != null && hyperlink.getHyperlinkAnchor() != null) {
                    href = href + "#" + hyperlink.getHyperlinkAnchor();
                    link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
                    link.setAddress(href);

                }
                break;

            }
            case REMOTE_PAGE: {
                String href = hyperlink.getHyperlinkReference();
                if (href != null && hyperlink.getHyperlinkPage() != null) {
                    href = href + "#JR_PAGE_ANCHOR_0_" + hyperlink.getHyperlinkPage().toString();
                    link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
                    link.setAddress(href);

                }
                break;

            }
            case NONE:
            default: {
            }
            }
        } else {
            String href = customHandler.getHyperlink(hyperlink);
            if (href != null) {
                link = createHelper.createHyperlink(Hyperlink.LINK_URL);
                link.setAddress(href);
            }
        }
        if (link != null) {
            //TODO: make tooltips functional
            //            if(hyperlink.getHyperlinkTooltip() != null)
            //            {
            //               link.setLabel(hyperlink.getHyperlinkTooltip());
            //            }
            cell.setHyperlink(link);
        }
    }
}

From source file:net.sf.jasperreports.engine.export.JRXlsMetadataExporter.java

License:Open Source License

protected void closeWorkbook(OutputStream os) throws JRException {
    try {//from w  w  w.  j  a  v a 2  s .com
        for (Object anchorName : anchorNames.keySet()) {
            HSSFName anchor = anchorNames.get(anchorName);
            List<Hyperlink> linkList = anchorLinks.get(anchorName);
            anchor.setRefersToFormula(
                    "'" + workbook.getSheetName(anchor.getSheetIndex()) + "'!" + anchor.getRefersToFormula());

            if (linkList != null && !linkList.isEmpty()) {
                for (Hyperlink link : linkList) {
                    link.setAddress(anchor.getRefersToFormula());
                }
            }
        }

        int index = 0;
        for (Integer linkPage : pageLinks.keySet()) {
            List<Hyperlink> linkList = pageLinks.get(linkPage);
            if (linkList != null && !linkList.isEmpty()) {
                for (Hyperlink link : linkList) {
                    index = onePagePerSheetMap.get(linkPage - 1) != null
                            ? (onePagePerSheetMap.get(linkPage - 1) ? Math.max(0, linkPage - 1)
                                    : Math.max(0, sheetsBeforeCurrentReportMap.get(linkPage)))
                            : 0;
                    link.setAddress("'" + workbook.getSheetName(index) + "'!$A$1");
                }
            }
        }

        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
            HSSFSheet currentSheet = workbook.getSheetAt(i);
            currentSheet.setForceFormulaRecalculation(true);
            for (String columnName : columnNames) {
                Integer columnWidth = columnWidths.get(columnName);
                if (columnWidth != null && columnWidth < Integer.MAX_VALUE) {
                    currentSheet.setColumnWidth(columnNamesMap.get(columnName),
                            Math.min(43 * columnWidth, 256 * 255));
                } else {
                    currentSheet.autoSizeColumn(columnNamesMap.get(columnName), false);
                }
            }
        }
        workbook.write(os);
    } catch (IOException e) {
        throw new JRException("Error generating XLS metadata report : " + jasperPrint.getName(), e);
    }
}

From source file:net.sf.jasperreports.engine.export.JRXlsMetadataExporter.java

License:Open Source License

protected void setHyperlinkCell(JRPrintHyperlink hyperlink, CellSettings cellSettings) {
    Hyperlink link = null;

    Boolean ignoreHyperlink = HyperlinkUtil.getIgnoreHyperlink(XlsReportConfiguration.PROPERTY_IGNORE_HYPERLINK,
            hyperlink);//from   ww w  .j a v  a  2s  .c  o  m
    if (ignoreHyperlink == null) {
        ignoreHyperlink = getCurrentItemConfiguration().isIgnoreHyperlink();
    }

    if (!ignoreHyperlink) {
        JRHyperlinkProducer customHandler = getHyperlinkProducer(hyperlink);
        if (customHandler == null) {
            switch (hyperlink.getHyperlinkTypeValue()) {
            case REFERENCE: {
                String href = hyperlink.getHyperlinkReference();
                if (href != null) {
                    link = createHelper.createHyperlink(Hyperlink.LINK_URL);
                    link.setAddress(href);
                }
                break;
            }
            case LOCAL_ANCHOR: {
                if (!getCurrentItemConfiguration().isIgnoreAnchors()) {
                    String href = hyperlink.getHyperlinkAnchor();
                    if (href != null) {
                        link = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
                        if (anchorLinks.containsKey(href)) {
                            (anchorLinks.get(href)).add(link);
                        } else {
                            List<Hyperlink> hrefList = new ArrayList<Hyperlink>();
                            hrefList.add(link);
                            anchorLinks.put(href, hrefList);
                        }
                    }
                }
                break;
            }
            case LOCAL_PAGE: {
                Integer hrefPage = (getCurrentItemConfiguration().isOnePagePerSheet()
                        ? hyperlink.getHyperlinkPage()
                        : 0);
                if (hrefPage != null) {
                    link = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
                    if (pageLinks.containsKey(sheetsBeforeCurrentReport + hrefPage)) {
                        pageLinks.get(sheetsBeforeCurrentReport + hrefPage).add(link);
                    } else {
                        List<Hyperlink> hrefList = new ArrayList<Hyperlink>();
                        hrefList.add(link);
                        pageLinks.put(sheetsBeforeCurrentReport + hrefPage, hrefList);
                    }
                }
                break;
            }
            case REMOTE_ANCHOR: {
                String href = hyperlink.getHyperlinkReference();
                if (href != null && hyperlink.getHyperlinkAnchor() != null) {
                    href = href + "#" + hyperlink.getHyperlinkAnchor();
                    link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
                    link.setAddress(href);

                }
                break;
            }
            case REMOTE_PAGE: {
                String href = hyperlink.getHyperlinkReference();
                if (href != null && hyperlink.getHyperlinkPage() != null) {
                    href = href + "#JR_PAGE_ANCHOR_0_" + hyperlink.getHyperlinkPage().toString();
                    link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
                    link.setAddress(href);

                }
                break;
            }
            case NONE:
            default: {
            }
            }
        } else {
            String href = customHandler.getHyperlink(hyperlink);
            if (href != null) {
                link = createHelper.createHyperlink(Hyperlink.LINK_URL);
                link.setAddress(href);
            }
        }

        if (link != null) {
            //TODO: make tooltips functional
            //            if(hyperlink.getHyperlinkTooltip() != null)
            //            {
            //               link.setLabel(hyperlink.getHyperlinkTooltip());
            //            }
            cellSettings.setLink(link);
        }
    }
}

From source file:opn.greenwebs.HyperlinkExample.java

public static void main(String[] args) throws Exception {
    Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
    CreationHelper createHelper = wb.getCreationHelper();

    //cell style for hyperlinks
    //by default hyperlinks are blue and underlined
    CellStyle hlink_style = wb.createCellStyle();
    Font hlink_font = wb.createFont();
    hlink_font.setUnderline(Font.U_SINGLE);
    hlink_font.setColor(IndexedColors.BLUE.getIndex());
    hlink_style.setFont(hlink_font);//  ww  w .java 2  s.c  o m

    Cell cell;
    Sheet sheet = wb.createSheet("Hyperlinks");
    //URL
    cell = sheet.createRow(0).createCell((short) 0);
    cell.setCellValue("URL Link");

    Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);
    link.setAddress("http://poi.apache.org/");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);

    //link to a file in the current directory
    cell = sheet.createRow(1).createCell((short) 0);
    cell.setCellValue("File Link");
    link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
    link.setAddress("link1.xls");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);

    //e-mail link
    cell = sheet.createRow(2).createCell((short) 0);
    cell.setCellValue("Email Link");
    link = createHelper.createHyperlink(Hyperlink.LINK_EMAIL);
    //note, if subject contains white spaces, make sure they are url-encoded
    link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);

    //link to a place in this workbook

    //create a target sheet and cell
    Sheet sheet2 = wb.createSheet("Target Sheet");
    sheet2.createRow(0).createCell((short) 0).setCellValue("Target Cell");

    cell = sheet.createRow(3).createCell((short) 0);
    cell.setCellValue("Worksheet Link");
    Hyperlink link2 = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
    link2.setAddress("'Target Sheet'!A1");
    cell.setHyperlink(link2);
    cell.setCellStyle(hlink_style);

    FileOutputStream out = new FileOutputStream("hyperinks.xlsx");
    wb.write(out);
    out.close();

}

From source file:org.apache.tika.eval.reports.XLSXHREFFormatter.java

License:Apache License

@Override
public void applyStyleAndValue(int dbColNum, ResultSet resultSet, Cell cell) throws SQLException {
    if (links < MAX_HYPERLINKS) {
        Hyperlink hyperlink = workbook.getCreationHelper().createHyperlink(linkType);
        String path = resultSet.getString(dbColNum);
        String address = urlBase + path;
        hyperlink.setAddress(address);
        cell.setHyperlink(hyperlink);//ww  w .  jav  a2s .  c  om
        cell.setCellStyle(style);
        String fName = Paths.get(path).getFileName().toString();
        cell.setCellValue(fName);
        links++;
    } else {
        //silently stop adding hyperlinks
    }
}

From source file:org.azkfw.document.database.xlsx.XLSXWriter.java

License:Apache License

private Hyperlink createTableLink(final String name) {
    CreationHelper ch = workbook.getCreationHelper();
    Hyperlink link = ch.createHyperlink(Hyperlink.LINK_DOCUMENT);
    link.setAddress(String.format("%s!A1", name));
    return link;/*w  ww.j  av  a2s.  co m*/
}

From source file:org.cerberus.service.export.ExportServiceFactory.java

License:Open Source License

private int createRow(String test, HashMap<String, List<TestCaseExecution>> executionsPerTestCase, Sheet sheet,
        int currentIndex, List<String> mapCountries) {

    int lastRow = currentIndex + executionsPerTestCase.size();

    int current = currentIndex;

    TreeMap<String, List<TestCaseExecution>> sortedKeys = new TreeMap<String, List<TestCaseExecution>>(
            executionsPerTestCase);/*from www.jav  a  2 s.  c  o  m*/
    CellStyle wrapStyle = sheet.getColumnStyle(0); //Create new style
    wrapStyle.setWrapText(true); //Set wordwrap

    for (String testCaseKey : sortedKeys.keySet()) {
        List<String> browserEnvironment = new LinkedList<String>();
        String application;
        String description;
        Row r = sheet.createRow(current);
        List<TestCaseExecution> executionList = executionsPerTestCase.get(testCaseKey);
        Cell testCell = r.createCell(0);
        testCell.setCellValue(test);
        testCell.setCellStyle(wrapStyle);
        r.createCell(1).setCellValue(testCaseKey);

        //gets the first object to retrieve the application - at least exists one test case execution
        if (executionList.isEmpty()) {
            application = "N/D";
            description = "N/D";
        } else {
            application = executionList.get(0).getApplication();
            description = executionList.get(0).getTestCaseObj().getBehaviorOrValueExpected();
        }
        //Sets the application and description
        r.createCell(2).setCellValue(application);
        r.createCell(3).setCellValue(description);

        int rowStartedTestCaseInfo = current;

        for (TestCaseExecution exec : executionList) {
            if (browserEnvironment.isEmpty()) {
                browserEnvironment.add(exec.getEnvironment() + "_" + exec.getBrowser());
                r.createCell(4).setCellValue(exec.getEnvironment());
                r.createCell(5).setCellValue(exec.getBrowser());
            } else {
                int index = browserEnvironment.indexOf(exec.getEnvironment() + "_" + exec.getBrowser());

                //Does not exist any information about browser and environment
                if (browserEnvironment.indexOf(exec.getEnvironment() + "_" + exec.getBrowser()) == -1) {
                    //need to add another row with the same characteristics
                    r = sheet.createRow(++current);
                    r.createCell(0).setCellValue(test);
                    r.createCell(1).setCellValue(testCaseKey);
                    r.createCell(2).setCellValue(application);
                    r.createCell(3).setCellValue(description);
                    r.createCell(4).setCellValue(exec.getEnvironment());
                    r.createCell(5).setCellValue(exec.getBrowser());

                    browserEnvironment.add(exec.getEnvironment() + "_" + exec.getBrowser());
                } else {
                    //there is information about the browser and environment
                    Row rowExisting = sheet.getRow(rowStartedTestCaseInfo + index);
                    r = rowExisting;
                }

            }

            //TODO:FN tirar daqui estes valores
            int indexOfCountry = mapCountries.indexOf(exec.getCountry()) + 6;
            Cell executionResult = r.createCell(indexOfCountry);
            executionResult.setCellValue(exec.getControlStatus());
            //Create hyperling
            CreationHelper createHelper = sheet.getWorkbook().getCreationHelper();
            CellStyle hlinkstyle = sheet.getWorkbook().createCellStyle();
            Font hlinkfont = sheet.getWorkbook().createFont();
            hlinkfont.setUnderline(XSSFFont.U_SINGLE);
            hlinkfont.setColor(HSSFColor.BLUE.index);
            hlinkstyle.setFont(hlinkfont);

            Hyperlink link = (Hyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL);
            link.setAddress("http://www.tutorialspoint.com/");
            executionResult.setHyperlink((Hyperlink) link);
            executionResult.setCellStyle(hlinkstyle);

        }
        current++;

    }

    /*r.createCell(1).setCellValue("");
     r.createCell(2).setCellValue("");
     r.createCell(3).setCellValue("");
     r.createCell(4).setCellValue("");
     r.createCell(5).setCellValue("");
     */
    //        for(TestCaseWithExecution exec : execution){
    //            
    //            //r.createCell(2).setCellValue(exec.getDescription());
    //            //r.createCell(3).setCellValue(exec.getApplication());
    //            //r.createCell(4).setCellValue(exec.getEnvironment());
    //            //r.createCell(5).setCellValue(exec.getBrowser());
    //            int indexOfCountry = mapCountries.indexOf(exec.getCountry()) + 6;
    //            r.createCell(indexOfCountry).setCellValue(exec.getControlStatus());
    //            //current++;
    //        }
    //puts the test name in the first column
    /*r = sheet.getRow(currentIndex);
     r.getCell(0).setCellValue(test);
     */
    /*CellRangeAddress range = new CellRangeAddress(currentIndex, lastRow, 0, 0);
     sheet.addMergedRegion(range);*/
    return lastRow;
}

From source file:org.gedantic.web.servlet.WorkbookCreator.java

License:Open Source License

/**
 * Adds the footer.//w ww  .  j  av a 2  s.c o  m
 */
private void addFooter() {
    nextRow();
    nextRow();
    cell.setCellValue("Produced by gedantic");

    Hyperlink link = wb.getCreationHelper().createHyperlink(org.apache.poi.common.usermodel.Hyperlink.LINK_URL);
    link.setAddress("http://gedantic.org");
    cell.setHyperlink(link);

    cell.setCellStyle(styleHyperlink);

}