org.apache.poi.common.usermodel.HyperlinkType Java Examples

The following examples show how to use org.apache.poi.common.usermodel.HyperlinkType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HSSFHyperlink.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a new hyperlink
 * 
 * This method is internal to be used only by
 * {@link HSSFCreationHelper#createHyperlink(HyperlinkType)}.
 *
 * @param type the type of hyperlink to create
 */
@Internal(since="3.15 beta 3")
protected HSSFHyperlink( HyperlinkType type )
{
    this.link_type = type;
    record = new HyperlinkRecord();
    switch(type){
        case URL:
        case EMAIL:
            record.newUrlLink();
            break;
        case FILE:
            record.newFileLink();
            break;
        case DOCUMENT:
            record.newDocumentLink();
            break;
        default:
            throw new IllegalArgumentException("Invalid type: " + type);
    }
}
 
Example #2
Source File: HSSFHyperlink.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static HyperlinkType getType(HyperlinkRecord record) {
    HyperlinkType link_type;
    // Figure out the type
    if (record.isFileLink()) {
        link_type = HyperlinkType.FILE;
    } else if(record.isDocumentLink()) {
        link_type = HyperlinkType.DOCUMENT;
    } else {
        if(record.getAddress() != null &&
                record.getAddress().startsWith("mailto:")) {
            link_type = HyperlinkType.EMAIL;
        } else {
            link_type = HyperlinkType.URL;
        }
    }
    return link_type;
}
 
Example #3
Source File: LinkCellConverterTest.java    From xlsmapper with Apache License 2.0 6 votes vote down vote up
public String getFormula2(Point point, Cell cell) {

            if(Utils.equals(comment, "空文字")) {
                return null;

            }

            // ダミーでリンクも設定する
            final CreationHelper helper = cell.getSheet().getWorkbook().getCreationHelper();
            final Hyperlink link = helper.createHyperlink(HyperlinkType.URL);
            link.setAddress(comment);
            cell.setHyperlink(link);

            final int rowNumber = point.y + 1;
            return String.format("HYPERLINK(D%s,\"リンク\"&A%s)", rowNumber, rowNumber);
        }
 
Example #4
Source File: CellLinkCellConverterFactory.java    From xlsmapper with Apache License 2.0 6 votes vote down vote up
@Override
protected void setupCell(final Cell cell, final Optional<CellLink> cellValue) throws TypeBindException {
    
    // 既存のハイパーリンクを削除
    // 削除しないと、Excelの見た目上はリンクは変わっているが、データ上は2重にリンクが設定されている。
    cell.removeHyperlink();
    
    if(cellValue.isPresent()) {
        final CreationHelper helper = cell.getSheet().getWorkbook().getCreationHelper();
        final HyperlinkType type = POIUtils.judgeLinkType(cellValue.get().getLink());
        final Hyperlink link = helper.createHyperlink(type);
        link.setAddress(cellValue.get().getLink());
        cell.setHyperlink(link);
        
        cell.setCellValue(cellValue.get().getLabel());
        
    } else {
        cell.setCellType(CellType.BLANK);
    }
    
}
 
Example #5
Source File: URICellConverterFactory.java    From xlsmapper with Apache License 2.0 6 votes vote down vote up
@Override
protected void setupCell(final Cell cell, final Optional<URI> cellValue) throws TypeBindException {
    
    // 既存のハイパーリンクを削除
    // 削除しないと、Excelの見た目上はリンクは変わっているが、データ上は2重にリンクが設定されている。
    cell.removeHyperlink();
    
    if(cellValue.isPresent()) {
        final CreationHelper helper = cell.getSheet().getWorkbook().getCreationHelper();
        final Hyperlink link = helper.createHyperlink(HyperlinkType.URL);
        link.setAddress(cellValue.get().toString());
        cell.setHyperlink(link);
        
        cell.setCellValue(cellValue.get().toString());
        
    } else {
        cell.setCellType(CellType.BLANK);
    }
    
}
 
Example #6
Source File: POIUtils.java    From xlsmapper with Apache License 2.0 5 votes vote down vote up
/**
 * リンクのアドレスを判定する。
 * @param linkAddress リンクのアドレス(URL)
 * @return 不明な場合は{@link HyperlinkType#NONE}を返す。
 * @throws IllegalArgumentException linkAddress が空文字の場合。
 */
public static HyperlinkType judgeLinkType(final String linkAddress) {

    ArgUtils.notEmpty(linkAddress, "linkAddress");

    if(linkAddress.matches(".*![\\p{Alnum}]+")) {
        // !A1のアドレスを含むかどうか
        return HyperlinkType.DOCUMENT;

    } else if(linkAddress.matches("[\\p{Alpha}]+[0-9]+")) {
        // A1の通常のアドレスの形式
        return HyperlinkType.DOCUMENT;

    } else if(linkAddress.matches(".+@.+")) {
        // @を含むかどうか
        return HyperlinkType.EMAIL;

    } else if(linkAddress.matches("[\\p{Alpha}]+://.+")) {
        // プロトコル付きかどうか
        return HyperlinkType.URL;

    } else if(linkAddress.matches(".+\\.[\\p{Alnum}]+")) {
        // 拡張子付きかどうか
        return HyperlinkType.FILE;

    } else {
        return HyperlinkType.NONE;
    }

}
 
Example #7
Source File: AbstractExcelFactory.java    From myexcel with Apache License 2.0 5 votes vote down vote up
private Cell setLink(Td td, Row currentRow, HyperlinkType hyperlinkType) {
    if (StringUtil.isBlank(td.getContent())) {
        return currentRow.createCell(td.getCol());
    }
    if (createHelper == null) {
        createHelper = workbook.getCreationHelper();
    }
    Cell cell = currentRow.createCell(td.getCol(), CellType.STRING);
    cell.setCellValue(td.getContent());
    Hyperlink link = createHelper.createHyperlink(hyperlinkType);
    link.setAddress(td.getLink());
    cell.setHyperlink(link);
    return cell;
}
 
Example #8
Source File: CustomCellWriteHandler.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
    List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    // 这里可以对cell进行任何操作
    LOGGER.info("第{}行,第{}列写入完成。", cell.getRowIndex(), cell.getColumnIndex());
    if (isHead && cell.getColumnIndex() == 0) {
        CreationHelper createHelper = writeSheetHolder.getSheet().getWorkbook().getCreationHelper();
        Hyperlink hyperlink = createHelper.createHyperlink(HyperlinkType.URL);
        hyperlink.setAddress("https://github.com/alibaba/easyexcel");
        cell.setHyperlink(hyperlink);
    }
}
 
Example #9
Source File: EventWorksheet.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
private Hyperlink setAttachmentURLLinks(SignupAttachment attach) {
	Hyperlink hsHyperlink = wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
	String link = this.sakaiFacade.getServerConfigurationService().getServerUrl()
			+ attach.getLocation();
	hsHyperlink.setAddress(link);
	hsHyperlink.setLabel(attach.getFilename());
	return hsHyperlink;
}
 
Example #10
Source File: ExcelCell.java    From objectlabkit with Apache License 2.0 5 votes vote down vote up
public ExcelCell link(String url, String label) {
    final CreationHelper creationHelper = row().sheet().workbook().poiWorkbook().getCreationHelper();
    final Hyperlink hl = creationHelper.createHyperlink(HyperlinkType.URL);
    hl.setAddress(url);
    hl.setLabel(label);
    currentCell.setCellValue(label);
    currentCell.setHyperlink(hl);
    style(LINK);
    return this;
}
 
Example #11
Source File: EventWorksheet.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
private Hyperlink setAttachmentURLLinks(SignupAttachment attach) {
	Hyperlink hsHyperlink = wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
	String link = this.sakaiFacade.getServerConfigurationService().getServerUrl()
			+ attach.getLocation();
	hsHyperlink.setAddress(link);
	hsHyperlink.setLabel(attach.getFilename());
	return hsHyperlink;
}
 
Example #12
Source File: PptTemplates.java    From PPT-Templates with Apache License 2.0 4 votes vote down vote up
private static Optional<PptVariable> parseHyperlinkVariale(Hyperlink<?, ?> link) {
	if(link != null && link.getTypeEnum() == HyperlinkType.URL) {
		return PptParser.parse(link.getAddress());
	}
	return Optional.empty();
}
 
Example #13
Source File: POIUtilsTest.java    From xlsmapper with Apache License 2.0 4 votes vote down vote up
@Test
public void testJudgeLinkType() {
    
    assertThat(POIUtils.judgeLinkType("!A1"), is(HyperlinkType.DOCUMENT));
    assertThat(POIUtils.judgeLinkType("Sheet(a)!A1"), is(HyperlinkType.DOCUMENT));
    
    assertThat(POIUtils.judgeLinkType("[email protected]"), is(HyperlinkType.EMAIL));
    assertThat(POIUtils.judgeLinkType("mailto:[email protected]"), is(HyperlinkType.EMAIL));
    
    assertThat(POIUtils.judgeLinkType("http://sample.co.jp/"), is(HyperlinkType.URL));
    assertThat(POIUtils.judgeLinkType("http://sample.co.jp/?name1=1&name2=2"), is(HyperlinkType.URL));
    
    assertThat(POIUtils.judgeLinkType("sample.xls"), is(HyperlinkType.FILE));
    assertThat(POIUtils.judgeLinkType("../sample.xls"), is(HyperlinkType.FILE));
    
}
 
Example #14
Source File: AbstractExcelFactory.java    From myexcel with Apache License 2.0 4 votes vote down vote up
/**
 * 创建单元格
 *
 * @param td         td
 * @param sheet      sheet
 * @param currentRow 当前行
 */
protected void createCell(Td td, Sheet sheet, Row currentRow) {
    Cell cell;
    if (td.isFormula()) {
        cell = currentRow.createCell(td.getCol(), CellType.FORMULA);
        cell.setCellFormula(td.getContent());
    } else {
        String content = td.getContent();
        switch (td.getTdContentType()) {
            case STRING:
                cell = currentRow.createCell(td.getCol(), CellType.STRING);
                cell.setCellValue(content);
                break;
            case DOUBLE:
                cell = currentRow.createCell(td.getCol(), CellType.NUMERIC);
                if (null != content) {
                    cell.setCellValue(Double.parseDouble(content));
                }
                break;
            case DATE:
                cell = currentRow.createCell(td.getCol());
                if (td.getDate() != null) {
                    cell.setCellValue(td.getDate());
                } else if (td.getLocalDateTime() != null) {
                    cell.setCellValue(td.getLocalDateTime());
                } else if (td.getLocalDate() != null) {
                    cell.setCellValue(td.getLocalDate());
                }
                break;
            case BOOLEAN:
                cell = currentRow.createCell(td.getCol(), CellType.BOOLEAN);
                if (null != content) {
                    cell.setCellValue(Boolean.parseBoolean(content));
                }
                break;
            case NUMBER_DROP_DOWN_LIST:
                cell = currentRow.createCell(td.getCol(), CellType.NUMERIC);
                String firstEle = setDropDownList(td, sheet, content);
                if (firstEle != null) {
                    cell.setCellValue(Double.parseDouble(firstEle));
                }
                break;
            case BOOLEAN_DROP_DOWN_LIST:
                cell = currentRow.createCell(td.getCol(), CellType.BOOLEAN);
                firstEle = setDropDownList(td, sheet, content);
                if (firstEle != null) {
                    cell.setCellValue(Boolean.parseBoolean(firstEle));
                }
                break;
            case DROP_DOWN_LIST:
                cell = currentRow.createCell(td.getCol(), CellType.STRING);
                firstEle = setDropDownList(td, sheet, content);
                if (firstEle != null) {
                    cell.setCellValue(firstEle);
                }
                break;
            case LINK_URL:
                cell = setLink(td, currentRow, HyperlinkType.URL);
                break;
            case LINK_EMAIL:
                cell = setLink(td, currentRow, HyperlinkType.EMAIL);
                break;
            case IMAGE:
                cell = currentRow.createCell(td.getCol());
                setImage(td, sheet);
                break;
            default:
                cell = currentRow.createCell(td.getCol(), CellType.STRING);
                cell.setCellValue(content);
                break;
        }
    }

    // 设置单元格样式
    this.setCellStyle(currentRow, cell, td);
    if (td.getCol() != td.getColBound()) {
        for (int j = td.getCol() + 1, colBound = td.getColBound(); j <= colBound; j++) {
            cell = currentRow.createCell(j);
            this.setCellStyle(currentRow, cell, td);
        }
    }
    if (td.getColSpan() > 0 || td.getRowSpan() > 0) {
        sheet.addMergedRegion(new CellRangeAddress(td.getRow(), td.getRowBound(), td.getCol(), td.getColBound()));
    }
}
 
Example #15
Source File: HSSFCreationHelper.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public HSSFHyperlink createHyperlink(HyperlinkType type) {
    return new HSSFHyperlink(type);
}
 
Example #16
Source File: HSSFHyperlink.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Return the type of this hyperlink
 *
 * @return the type of this hyperlink
 */
@Override
public HyperlinkType getTypeEnum() {
    return link_type;
}
 
Example #17
Source File: CreationHelper.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Creates a new Hyperlink, of the given type
 */
Hyperlink createHyperlink(HyperlinkType type);