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

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

Introduction

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

Prototype

public String getAddress();

Source Link

Document

Hyperlink address.

Usage

From source file:com.vaadin.addon.spreadsheet.Spreadsheet.java

License:Open Source License

private void loadHyperLinks(int r1, int c1, int r2, int c2) {
    for (int r = r1 - 1; r < r2; r++) {
        final Row row = getActiveSheet().getRow(r);
        if (row != null) {
            for (int c = c1 - 1; c < c2; c++) {
                Cell cell = row.getCell(c);
                if (cell != null) {
                    try {
                        Hyperlink link = cell.getHyperlink();
                        if (link != null) {
                            if (link instanceof XSSFHyperlink) {
                                String tooltip = ((XSSFHyperlink) link).getTooltip();
                                // Show address if no defined tooltip (like
                                // in
                                // excel)
                                if (tooltip == null) {
                                    tooltip = link.getAddress();
                                }/*from   w w  w.ja  v a2  s . c om*/
                                getState().hyperlinksTooltips.put(SpreadsheetUtil.toKey(c + 1, r + 1), tooltip);
                            } else {
                                getState().hyperlinksTooltips.put(SpreadsheetUtil.toKey(c + 1, r + 1),
                                        link.getAddress());
                            }
                        } else {
                            // Check if the cell has HYPERLINK function
                            if (DefaultHyperlinkCellClickHandler.isHyperlinkFormulaCell(cell)) {
                                getState().hyperlinksTooltips.put(SpreadsheetUtil.toKey(c + 1, r + 1),
                                        DefaultHyperlinkCellClickHandler.getHyperlinkFunctionCellAddress(cell));
                            }
                        }
                    } catch (XmlValueDisconnectedException exc) {
                        LOGGER.log(Level.FINEST, exc.getMessage(), exc);
                    }
                }
            }
        }
    }
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.legacyExcel.exporter.ExportWorkbook.java

License:Open Source License

/**
 * Sets given value and style of the given cell and adds the given <code>link</code> as hyperlink
 * to the <code>cell</code>.
 * //from  ww  w. ja  va 2 s.c  o m
 * @param cell
 *          the cell in question
 * @param value
 *          the value to be set
 * @param link
 *          the link to be added
 * @param style
 *          if <code>null</code> the default style for hyperlinks is taken
 */
private void setHyperlink(Cell cell, Integer value, Hyperlink link, CellStyle style) {
    if (cell == null) {
        return;
    }
    if ((value != null) && (link != null)) {
        cell.setCellFormula("HYPERLINK(\"" + link.getAddress() + "\"," + value + ")");
    }

    CellStyle styleToSet = (style != null ? style : getHyperlinkStyle());
    cell.setCellStyle(styleToSet);
}

From source file:io.konig.spreadsheet.WorkbookProcessorImpl.java

License:Apache License

private String cellStringValue(Cell cell) {
    if (cell == null) {
        return null;
    }/*from   www . ja  v  a2 s  .co m*/
    String text = dataFormatter.formatCellValue(cell);
    if (text != null && !text.startsWith("HYPERLINK(")) {
        text = text.trim();
        if (text.length() == 0) {
            text = null;
        }
    } else {

        Hyperlink link = cell.getHyperlink();
        if (link != null) {
            text = link.getLabel();
            if (text == null) {
                text = link.getAddress();
            }
            if (text != null) {
                text = text.trim();

            }
        }
    }
    return text;
}

From source file:net.sf.ahtutils.report.util.DataUtil.java

public static Object getCellValue(Cell cell) {
    Object value = new Object();

    // Prevent a NullPointerException
    if (cell != null) {
        if (cell.getHyperlink() != null) {
            Workbook workbook = new XSSFWorkbook();
            FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
            Hyperlink link = cell.getHyperlink();
            String address = link.getAddress();
            if (logger.isTraceEnabled()) {
                logger.trace("Found a Hyperlink to " + cell.getHyperlink().getAddress() + " in cell "
                        + cell.getRowIndex() + "," + cell.getColumnIndex());
            }/*from  w ww .  j  a v a 2 s . com*/
            cell = evaluator.evaluateInCell(cell);
        }
        // Depending on the cell type, the value is read using Apache POI methods

        switch (cell.getCellType()) {

        // String are easy to handle
        case Cell.CELL_TYPE_STRING:
            logger.trace("Found string " + cell.getStringCellValue());
            value = cell.getStringCellValue();
            break;

        // Since date formatted cells are also of the numeric type, this needs to be processed
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                Date date = cell.getDateCellValue();
                DateFormat df = SimpleDateFormat.getDateInstance();
                logger.trace("Found date " + df.format(date));
                value = date;
            } else {
                logger.trace("Found general number " + cell.getNumericCellValue());
                value = cell.getNumericCellValue();
            }
            break;
        }
    } else {
        logger.trace("Found cell with NULL value");
    }
    return value;
}

From source file:org.hellojavaer.poi.excel.utils.ExcelUtils.java

License:Apache License

@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T> T readRow(ExcelReadContext<T> context, Row row,
        Map<Integer, Map<String, ExcelReadFieldMappingAttribute>> fieldMapping, Class<T> targetClass,
        ExcelReadRowProcessor<T> processor, boolean isTrimSpace) {
    try {/*from w ww .java2 s  .  co m*/
        context.setCurRowData(targetClass.newInstance());
    } catch (Exception e1) {
        throw new RuntimeException(e1);
    }
    int curRowIndex = context.getCurRowIndex();
    for (Entry<Integer, Map<String, ExcelReadFieldMappingAttribute>> fieldMappingEntry : fieldMapping
            .entrySet()) {
        int curColIndex = fieldMappingEntry.getKey();// excel index;
        // proc cell
        context.setCurColIndex(curColIndex);

        Cell cell = null;
        if (row != null) {
            cell = row.getCell(curColIndex);
        }
        context.setCurCell(cell);

        Map<String, ExcelReadFieldMappingAttribute> fields = fieldMappingEntry.getValue();
        for (Map.Entry<String, ExcelReadFieldMappingAttribute> fieldEntry : fields.entrySet()) {
            String fieldName = fieldEntry.getKey();
            ExcelReadFieldMappingAttribute attribute = fieldEntry.getValue();
            // proccess link
            String linkField = attribute.getLinkField();
            if (linkField != null) {
                String address = null;
                if (cell != null) {
                    Hyperlink hyperlink = cell.getHyperlink();
                    if (hyperlink != null) {
                        address = hyperlink.getAddress();
                    }
                }
                if (isTrimSpace && address != null) {
                    address = address.trim();
                    if (address.length() == 0) {
                        address = null;
                    }
                }
                if (Map.class.isAssignableFrom(targetClass)) {// map
                    ((Map) context.getCurRowData()).put(linkField, address);
                } else {// java bean
                    try {
                        setProperty(context.getCurRowData(), linkField, address);
                    } catch (Exception e1) {
                        ExcelReadException e = new ExcelReadException(e1);
                        e.setRowIndex(curRowIndex);
                        e.setColIndex(curColIndex);
                        e.setCode(ExcelReadException.CODE_OF_PROCESS_EXCEPTION);
                        throw e;
                    }
                }
            }

            Object value = _readCell(cell);
            if (value != null && value instanceof String && isTrimSpace) {
                value = ((String) value).trim();
                if (((String) value).length() == 0) {
                    value = null;
                }
            }
            if (value == null && attribute.isRequired()) {
                ExcelReadException e = new ExcelReadException("Cell value is null");
                e.setRowIndex(curRowIndex);
                e.setColIndex(curColIndex);
                e.setCode(ExcelReadException.CODE_OF_CELL_VALUE_REQUIRED);
                throw e;
            }
            //
            try {
                if (Map.class.isAssignableFrom(targetClass)) {// map
                    value = procValueConvert(context, row, cell, attribute, fieldName, value);
                    ((Map) context.getCurRowData()).put(fieldName, value);
                } else {// java bean
                    value = procValueConvert(context, row, cell, attribute, fieldName, value);
                    setProperty(context.getCurRowData(), fieldName, value);
                }
            } catch (Exception e1) {
                ExcelReadException e = new ExcelReadException(e1);
                e.setRowIndex(curRowIndex);
                e.setColIndex(curColIndex);
                e.setCode(ExcelReadException.CODE_OF_PROCESS_EXCEPTION);
                throw e;
            }
        }
    }
    return context.getCurRowData();
}

From source file:org.spdx.spdxspreadsheet.LicenseSheet.java

License:Apache License

/**
 * Retrieve the text from a license text cell either through the hyperlink
 * to a text file in a directory local to the spreadsheet or from the cell 
 * itself if there is no hyperlink present
 * @param textCell//from   w  w  w  .  j  av a  2 s.  c o m
 * @return
 */
public static String getLicenseTemplateText(Cell textCell, String textFilePath) {
    String localFileName = null;
    File licenseTemplateTextFile = null;
    Hyperlink cellHyperlink = textCell.getHyperlink();
    if (cellHyperlink != null && cellHyperlink.getAddress() != null) {
        localFileName = cellHyperlink.getAddress();
        licenseTemplateTextFile = new File(textFilePath + File.separator + localFileName);
        if (!licenseTemplateTextFile.exists()) {
            // try without the workbook path
            licenseTemplateTextFile = new File(localFileName);
        }
        if (!licenseTemplateTextFile.exists()) {
            licenseTemplateTextFile = null;
        }
    }
    if (licenseTemplateTextFile == null && textCell.getStringCellValue() != null
            && textCell.getStringCellValue().toUpperCase().endsWith(".TXT")) {
        localFileName = textCell.getStringCellValue();
        licenseTemplateTextFile = new File(textFilePath + File.separator + localFileName);
    }
    if (localFileName != null) {
        if (!licenseTemplateTextFile.exists()) {
            logger.warn("Can not find linked license text file " + licenseTemplateTextFile.getName());
            return ("WARNING: Could not find license text file " + licenseTemplateTextFile.getName());
        }
        if (!licenseTemplateTextFile.canRead()) {
            logger.warn("Can not read linked license text file " + licenseTemplateTextFile.getName());
            return ("WARNING: Could not read license text file " + licenseTemplateTextFile.getName());
        }
        try {
            InputStream in = new FileInputStream(licenseTemplateTextFile);
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, ENCODING));
            try {
                StringBuilder sb = new StringBuilder();
                String line = null;
                String newLine = System.getProperty("line.separator");
                line = reader.readLine();
                if (line != null) {
                    sb.append(line);
                }
                while ((line = reader.readLine()) != null) {
                    sb.append(newLine);
                    sb.append(line);
                }
                return sb.toString();
            } finally {
                reader.close();
            }
        } catch (IOException e) {
            logger.warn("Error reading linked license template text file " + licenseTemplateTextFile.getName()
                    + ": " + e.getMessage());
            return ("WARNING: Error reading license template text file " + licenseTemplateTextFile.getName());
        }
    } else { // no file name
        return textCell.getStringCellValue();
    }
}