Example usage for org.apache.poi.ss.usermodel Cell setHyperlink

List of usage examples for org.apache.poi.ss.usermodel Cell setHyperlink

Introduction

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

Prototype

void setHyperlink(Hyperlink link);

Source Link

Document

Assign a hyperlink to this cell

Usage

From source file:com.sec.ose.osi.report.standard.IdentifiedFilesSheetTemplate.java

License:Open Source License

public void writeRow(ArrayList<IdentifiedFilesRow> identifiedFilesRowList) {

    for (IdentifiedFilesRow identifiedFilesRow : identifiedFilesRowList) {
        Row row = sheet.createRow(curRow++);

        Cell cell = row.createCell(ISheetTemplate.COL_A);
        cell.setCellValue(identifiedFilesRow.getProjectName());
        cell.setCellStyle(normalStyle);//  w w  w .  ja  v  a  2s.  c  om

        cell = row.createCell(ISheetTemplate.COL_B);
        cell.setCellValue(identifiedFilesRow.getFullPath());
        cell.setCellStyle(leftStyle);

        cell = row.createCell(ISheetTemplate.COL_C);
        cell.setCellValue(identifiedFilesRow.getComponent());
        cell.setCellStyle(normalStyle);

        cell = row.createCell(ISheetTemplate.COL_D);
        cell.setCellValue(identifiedFilesRow.getLicense());
        cell.setCellStyle(normalStyle);

        cell = row.createCell(ISheetTemplate.COL_E);
        cell.setCellValue(identifiedFilesRow.getDiscoveryType());
        cell.setCellStyle(normalStyle);

        cell = row.createCell(ISheetTemplate.COL_F);
        cell.setCellStyle(normalStyle);
        String value = "";
        String url = identifiedFilesRow.getUrl();
        if (url != null && (url.length() != 0) && !"null".equals(url)) {
            Hyperlink link = wb.getCreationHelper().createHyperlink(Hyperlink.LINK_URL);
            link.setAddress(url);
            link.setLabel(url);

            cell.setHyperlink(link);
            cell.setCellStyle(getCellStyle(WHITE, getFont(BLUE, (short) 10, false)));
            value = "Show Matched Code (" + (identifiedFilesRow.getCodeMatchCnt()) + ")";
        }
        cell.setCellValue(value);

        cell = row.createCell(ISheetTemplate.COL_G);
        cell.setCellValue(identifiedFilesRow.getComment());
        cell.setCellStyle(leftStyle);

    }
}

From source file:csv.impl.DefaultExcelFormatter.java

License:Open Source License

/**
 * Sets the cell style./*w w  w . j a va  2 s  . co m*/
 * This implementations calls various other methods to define
 * the style of the cell.
 * @param writer writer that requires the information
 * @param cell cell to be formatted
 * @param value value in cell
 * @see #getFormat(ExcelWriter, int, int, Object)
 * @see #getBackgroundColor(ExcelWriter, int, int, Object)
 * @see #getFillPattern(ExcelWriter, int, int, Object)
 * @see #getForegroundColor(ExcelWriter, int, int, Object)
 * @see #getFont(ExcelWriter, int, int, Object)
 * @see #getAlign(ExcelWriter, int, int, Object)
 * @see #getHyperlink(ExcelWriter, int, int, Object)
 */
@Override
public void setStyle(ExcelWriter writer, Cell cell, Object value) {
    int row = cell.getRowIndex();
    int column = cell.getColumnIndex();

    StyleDescription desc = new StyleDescription();

    // Collect cell style and check if we already had it before

    // data format
    desc.setFormat(getFormat(writer, row, column, value));
    desc.setFgColor(getForegroundColor(writer, row, column, value));
    desc.setFillPattern(getFillPattern(writer, row, column, value));
    desc.setBgColor(getBackgroundColor(writer, row, column, value));

    // Font
    desc.setFont(getFont(writer, row, column, value));

    // Borders
    desc.setTopBorderColor(getTopBorderColor(writer, row, column, value));
    desc.setLeftBorderColor(getLeftBorderColor(writer, row, column, value));
    desc.setRightBorderColor(getRightBorderColor(writer, row, column, value));
    desc.setBottomBorderColor(getBottomBorderColor(writer, row, column, value));
    desc.setTopBorderThickness(getTopBorderThickness(writer, row, column, value));
    desc.setLeftBorderThickness(getLeftBorderThickness(writer, row, column, value));
    desc.setRightBorderThickness(getRightBorderThickness(writer, row, column, value));
    desc.setBottomBorderThickness(getBottomBorderThickness(writer, row, column, value));
    desc.setTextWrap(isTextWrap(writer, row, column, value));

    // Alignment
    desc.setAlignment(getAlign(writer, row, column, value));

    if (!desc.isDefault()) {
        CellStyle style = styles.get(desc);
        if (style == null) {
            style = writer.getWorkbook().createCellStyle();
            styles.put(desc, style);
            desc.applyStyle(style);
        }

        desc.applyStyle(style);

        // set style
        cell.setCellStyle(style);
    }

    // Set a hyperlink
    Hyperlink link = getHyperlink(writer, row, column, value);
    if (link != null)
        cell.setHyperlink(link);

}

From source file:de.iteratec.iteraplan.businesslogic.exchange.elasticExcel.export.template.DataExportIntroSheetGenerator.java

License:Open Source License

/**
 * Creates a section of the summary, containing links to one of the types of sheets 
 * // www .j  a  v  a2 s . com
 * @param sheetContexts list of sheets to link to. Only contains one type of {@link NamedExpression}
 * @param startAt the row to start at in the introduction sheet
 * @param sectionHeader the header of the section
 * 
 * @return the row number that was reached while inserting the links
 */
private int createSummarySection(List<SheetContext> sheetContexts, int startAt, String sectionHeader) {
    int rowNr = startAt;
    Map<IteraExcelStyle, CellStyle> styles = wbContext.getStyles();
    Workbook workbook = wbContext.getWb();
    CreationHelper createHelper = workbook.getCreationHelper();

    // header
    Sheet introSheet = getIntroductionSheet();
    Cell headerCell = introSheet.createRow(rowNr++).createCell(SUMMARY_COL);
    headerCell.setCellValue(sectionHeader);
    headerCell.setCellStyle(styles.get(IteraExcelStyle.HEADER));
    headerCell.getRow().createCell(SUMMARY_COL + 1).setCellStyle(styles.get(IteraExcelStyle.HEADER));

    for (SheetContext sheetContext : sheetContexts) {
        String sheetName = sheetContext.getSheetName();
        String extraInfo = sheetContext.getExpression().getName();
        // name is empty, we assume it's a relationship, we need to get the name of the relationship ends
        if (StringUtils.isEmpty(extraInfo)) {
            NamedExpression expression = sheetContext.getExpression();
            if (expression instanceof RelationshipExpression) {
                extraInfo = createRelationshipExtrainfo((RelationshipExpression) expression);
            }
        }

        Row entryRow = introSheet.createRow(rowNr++);
        Cell hyperlinkCell = entryRow.createCell(SUMMARY_COL);
        hyperlinkCell.setCellValue(sheetName);
        entryRow.createCell(SUMMARY_COL + 1).setCellValue(extraInfo);

        Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
        link.setAddress("'" + sheetName + "'!A1");
        hyperlinkCell.setHyperlink(link);
        hyperlinkCell.setCellStyle(styles.get(IteraExcelStyle.HYPERLINK));
    }

    //spacing between sections
    introSheet.createRow(rowNr++);

    return rowNr;
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.elasticExcel.export.template.RelationshipSheetGenerator.java

License:Open Source License

/**{@inheritDoc}**/
protected void addHeaderHyperlink(FeatureExpression<?> featureExpression, Cell headerCell) {
    Hyperlink link = wbContext.getWb().getCreationHelper().createHyperlink(Hyperlink.LINK_DOCUMENT);
    String sheetName = wbContext/*from  w  ww  . java2s. c om*/
            .getSheetContextByExpressionName(featureExpression.getType().getPersistentName()).getSheetName();
    link.setAddress("'" + sheetName + "'!A1");
    headerCell.setHyperlink(link);
}

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>.
 * // w ww.  j  a va  2s . 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 used
 */
private void setHyperlink(Cell cell, String value, Hyperlink link, CellStyle style) {
    if (cell == null) {
        return;
    }
    CreationHelper createHelper = getWb().getCreationHelper();
    if (value != null) {
        cell.setCellValue(createHelper.createRichTextString(value));
    }
    if (link != null) {
        cell.setHyperlink(link);
    }

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

From source file:de.iteratec.iteraplan.businesslogic.exchange.timeseriesExcel.exporter.TimeseriesIntroSheetGenerator.java

License:Open Source License

@Override
protected void createSummary() {
    Sheet introSheet = getIntroductionSheet();

    int rowNum = SUMMARY_ROW;
    Cell headerCell = introSheet.createRow(rowNum++).createCell(SUMMARY_COL);
    headerCell.setCellValue(MessageAccess.getStringOrNull("excel.export.timeseries.intro.description"));
    headerCell.setCellStyle(wbContext.getCellStyles().get(IteraExcelStyle.HEADER));
    introSheet.addMergedRegion(new CellRangeAddress(headerCell.getRowIndex(), headerCell.getRowIndex(),
            headerCell.getColumnIndex(), headerCell.getColumnIndex() + 1));

    CreationHelper createHelper = getWorkbook().getCreationHelper();

    // TODO group the summary by building block type with subheaders
    for (int i = 0; i < getWorkbook().getNumberOfSheets(); i++) {
        Sheet sheet = getWorkbook().getSheetAt(i);
        String sheetName = sheet.getSheetName();
        if (!introSheet.equals(sheet)) {
            Row summaryRow = introSheet.createRow(rowNum++);
            Cell hyperlinkCell = summaryRow.createCell(SUMMARY_COL);
            hyperlinkCell.setCellValue(sheetName);
            summaryRow.createCell(SUMMARY_COL + 1)
                    .setCellValue(ExcelUtils.getStringCellValue(sheet.getRow(0).getCell(0)));

            Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
            link.setAddress("'" + sheetName + "'!A1");
            hyperlinkCell.setHyperlink(link);
            hyperlinkCell.setCellStyle(wbContext.getCellStyles().get(IteraExcelStyle.HYPERLINK));
        }/* ww w.j  a va2  s  .  co  m*/
    }

    adjustSheetColumnWidths();
}

From source file:de.jlo.talendcomp.excel.SpreadsheetOutput.java

License:Apache License

private void setCellHyperLink(Cell cell, String url) {
    if (url.contains("://")) {
        Hyperlink link = creationHelper.createHyperlink(HyperlinkType.URL);
        link.setAddress(url);//ww  w . java  2s.  c o  m
        cell.setHyperlink(link);
    } else if (url.startsWith("mailto:")) {
        Hyperlink link = creationHelper.createHyperlink(HyperlinkType.EMAIL);
        link.setAddress(url);
        cell.setHyperlink(link);
    } else {
        Hyperlink link = creationHelper.createHyperlink(HyperlinkType.FILE);
        link.setAddress(url);
        cell.setHyperlink(link);
    }
    if (cell.getCellTypeEnum() == CellType.BLANK) {
        cell.setCellValue(url);
    }
}

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);/*from w w  w . ja v a 2s .c o  m*/
            cell.setHyperlink(hyperlink);
        }

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

}

From source file:fr.openwide.core.export.excel.AbstractExcelTableExport.java

License:Apache License

/**
 * Ajoute un lien hypertexte sur la cellule.
 * //from   w  w w  . j  a v a  2 s  .  c o  m
 * @param cell cellule
 * @param hyperlink lien  ajouter
 * @return cellule
 */
protected Cell addLinkToCell(Cell cell, Hyperlink hyperlink) {
    cell.setHyperlink(hyperlink);
    cell.setCellStyle(getRowStyle(STYLE_LINK_NAME, cell.getRowIndex()));

    return cell;
}

From source file:gov.nih.nci.evs.app.neopl.CSVtoExcel.java

License:Open Source License

public void runHSSF(String inputfile) {
    int size = checkSpecialCharacters(inputfile);

    int n = inputfile.lastIndexOf(".");
    String outputfile = getOutputFile(inputfile, "xls");

    try {/*ww  w .ja  va  2s  .c o  m*/
        HSSFWorkbook wb = new HSSFWorkbook();

        HSSFCellStyle cellStyle = wb.createCellStyle();
        //cellStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
        //cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //font.setColor(HSSFColor.WHITE.index);
        cellStyle.setFont(font);

        HSSFCellStyle linkCellStyle = wb.createCellStyle();
        //cellStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
        //cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont linkfont = wb.createFont();
        linkfont.setColor(HSSFColor.LIGHT_BLUE.index);
        linkCellStyle.setFont(linkfont);

        //CreationHelper helper = wb.getCreationHelper();
        CSVReader reader = new CSVReader(new FileReader(inputfile));//CSV file
        String[] line;
        int r = 0;
        Cell cell = null;

        //"C7419","Acanthoma","C0846967","Acanthoma","MDR","Acanthoma","10059394","LT"
        // skip heading
        HSSFHyperlink url_link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
        HSSFSheet sheet = null;
        int page_num = 1;
        Row row = null;
        line = reader.readNext();
        int lcv = 0;
        int row_count = 0;
        Vector w = new Vector();
        while ((line = reader.readNext()) != null) {
            if (lcv % PAGE_SIZE == 0) {
                r = 0;
                String sheetLabel = SHEET_LABEL;
                if (size > PAGE_SIZE) {
                    sheetLabel = sheetLabel + " (Page " + page_num + ")";
                }
                sheet = wb.createSheet(sheetLabel);
                for (int j = 0; j < HEADINGS.length; j++) {
                    sheet.autoSizeColumn(j);
                }
                row = sheet.createRow((short) r);
                for (int i = 0; i < HEADINGS.length; i++) {
                    String heading = HEADINGS[i];
                    cell = row.createCell(i);
                    cell.setCellValue(heading);
                    cell.setCellStyle(cellStyle);
                }
                sheet.createFreezePane(0, 1); // this will freeze the header row
                page_num++;
            }

            String s4 = (String) line[4];
            if (s4.compareTo("NCI") == 0) {
                w.add("NCI Line number: " + r);
            } else if (s4.compareTo("NCI") != 0) {
                r++;
                row = sheet.createRow((short) r);
                row_count++;

                row = sheet.createRow((short) r);
                row_count++;

                for (int i = 0; i < HEADINGS.length; i++) {
                    cell = row.createCell(i);
                    int codeCol = codeColumn[i];
                    cell.setCellValue(line[i]);
                    if (NCIT_LINK && codeCol == LINK_NCIT) {
                        url_link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
                        String code = line[i];
                        url_link.setAddress(getNCItHyperlink(code));
                        cell.setHyperlink(url_link);
                        cell.setCellStyle(linkCellStyle);
                    } else if (NCIM_LINK && codeCol == LINK_NCIM) {
                        url_link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
                        String code = line[i];
                        url_link.setAddress(getNCImHyperlink(code));
                        cell.setHyperlink(url_link);
                        cell.setCellStyle(linkCellStyle);
                    } else if (SOURCE_LINK && codeCol == LINK_OTHER) {
                        url_link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
                        String code = line[i];
                        String source = line[SOURCE_INDEX];
                        if (localNameMap.containsKey(source)) {
                            url_link.setAddress(getSourceHyperlink(source, code));
                            cell.setHyperlink(url_link);
                            cell.setCellStyle(linkCellStyle);
                        }
                    }
                }
            }
            lcv++;
        }

        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream(outputfile);
        wb.write(fileOut);
        fileOut.close();
        System.out.println("Output file " + outputfile + " generated.");
        System.out.println("row_count: " + row_count);
        System.out.println("NCI: " + w.size());
        for (int i = 0; i < w.size(); i++) {
            String t = (String) w.elementAt(i);
            System.out.println(t);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}