use of org.apache.poi.ss.usermodel.Hyperlink in project poi by apache.
the class TestXSSFCell method testCopyCellFrom_CellCopyPolicy_copyHyperlink.
@Test
public final void testCopyCellFrom_CellCopyPolicy_copyHyperlink() throws IOException {
setUp_testCopyCellFrom_CellCopyPolicy();
final Workbook wb = srcCell.getSheet().getWorkbook();
final CreationHelper createHelper = wb.getCreationHelper();
srcCell.setCellValue("URL LINK");
Hyperlink link = createHelper.createHyperlink(HyperlinkType.URL);
link.setAddress("http://poi.apache.org/");
srcCell.setHyperlink(link);
// Set link cell style (optional)
CellStyle hlinkStyle = wb.createCellStyle();
Font hlinkFont = wb.createFont();
hlinkFont.setUnderline(Font.U_SINGLE);
hlinkFont.setColor(IndexedColors.BLUE.getIndex());
hlinkStyle.setFont(hlinkFont);
srcCell.setCellStyle(hlinkStyle);
// Copy hyperlink
final CellCopyPolicy policy = new CellCopyPolicy.Builder().copyHyperlink(true).mergeHyperlink(false).build();
destCell.copyCellFrom(srcCell, policy);
assertNotNull(destCell.getHyperlink());
assertSame("unit test assumes srcCell and destCell are on the same sheet", srcCell.getSheet(), destCell.getSheet());
final List<XSSFHyperlink> links = srcCell.getSheet().getHyperlinkList();
assertEquals("number of hyperlinks on sheet", 2, links.size());
assertEquals("source hyperlink", new CellReference(srcCell).formatAsString(), links.get(0).getCellRef());
assertEquals("destination hyperlink", new CellReference(destCell).formatAsString(), links.get(1).getCellRef());
wb.close();
}
use of org.apache.poi.ss.usermodel.Hyperlink in project tika by apache.
the class XLSXHREFFormatter method applyStyleAndValue.
@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);
cell.setCellStyle(style);
String fName = Paths.get(path).getFileName().toString();
cell.setCellValue(fName);
links++;
} else {
//silently stop adding hyperlinks
}
}
Aggregations