Java Code Examples for org.apache.poi.ss.usermodel.Cell#setHyperlink()

The following examples show how to use org.apache.poi.ss.usermodel.Cell#setHyperlink() . 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: ExcelUtils.java    From onetwo with Apache License 2.0 6 votes vote down vote up
public static void copyCellStyle(Cell source, Cell target){
	CellStyle style = source.getCellStyle();
	if(style!=null){
		//TODO:会影响性能, 可缓存。。。
		CellStyle newCellStyle = source.getRow().getSheet().getWorkbook().createCellStyle();
		newCellStyle.cloneStyleFrom(style);
		target.setCellStyle(style);
	}
	if(source.getCellComment()!=null){
		target.setCellComment(source.getCellComment());
	}
	if(source.getHyperlink()!=null){
		target.setHyperlink(source.getHyperlink());
	}
	target.setCellType(source.getCellType());
}
 
Example 2
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 3
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 4
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 5
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 6
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 7
Source File: ExcelUtils.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public static void copyRow(Sheet worksheet, Row newRow, Row sourceRow) {
Workbook workbook = worksheet.getWorkbook();
      for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
          Cell oldCell = sourceRow.getCell(i);
          Cell newCell = newRow.createCell(i);

          if (oldCell == null) {
              newCell = null;
              continue;
          }

          CellStyle newCellStyle = workbook.createCellStyle();
          newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
          newCell.setCellStyle(newCellStyle);

          if (oldCell.getCellComment() != null) {
              newCell.setCellComment(oldCell.getCellComment());
          }

          if (oldCell.getHyperlink() != null) {
              newCell.setHyperlink(oldCell.getHyperlink());
          }

          newCell.setCellType(oldCell.getCellType());

          switch (oldCell.getCellType()) {
              case Cell.CELL_TYPE_BLANK:
                  newCell.setCellValue(oldCell.getStringCellValue());
                  break;
              case Cell.CELL_TYPE_BOOLEAN:
                  newCell.setCellValue(oldCell.getBooleanCellValue());
                  break;
              case Cell.CELL_TYPE_ERROR:
                  newCell.setCellErrorValue(oldCell.getErrorCellValue());
                  break;
              case Cell.CELL_TYPE_FORMULA:
                  newCell.setCellFormula(oldCell.getCellFormula());
                  break;
              case Cell.CELL_TYPE_NUMERIC:
                  newCell.setCellValue(oldCell.getNumericCellValue());
                  break;
              case Cell.CELL_TYPE_STRING:
                  newCell.setCellValue(oldCell.getRichStringCellValue());
                  break;
          }
      }

      for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
          CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
          if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
              CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(),
                      (newRow.getRowNum() +
                              (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow()
                                      )),
                      cellRangeAddress.getFirstColumn(),
                      cellRangeAddress.getLastColumn());
              worksheet.addMergedRegion(newCellRangeAddress);
          }
      }
  }