Search in sources :

Example 6 with Hyperlink

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();
}
Also used : CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) CellStyle(org.apache.poi.ss.usermodel.CellStyle) CellReference(org.apache.poi.ss.util.CellReference) Workbook(org.apache.poi.ss.usermodel.Workbook) Font(org.apache.poi.ss.usermodel.Font) CellCopyPolicy(org.apache.poi.ss.usermodel.CellCopyPolicy) Hyperlink(org.apache.poi.ss.usermodel.Hyperlink) Test(org.junit.Test)

Example 7 with Hyperlink

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
    }
}
Also used : Hyperlink(org.apache.poi.ss.usermodel.Hyperlink)

Aggregations

Hyperlink (org.apache.poi.ss.usermodel.Hyperlink)7 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)4 CellStyle (org.apache.poi.ss.usermodel.CellStyle)3 Font (org.apache.poi.ss.usermodel.Font)3 Workbook (org.apache.poi.ss.usermodel.Workbook)3 Test (org.junit.Test)3 Cell (org.apache.poi.ss.usermodel.Cell)2 CellCopyPolicy (org.apache.poi.ss.usermodel.CellCopyPolicy)2 CellReference (org.apache.poi.ss.util.CellReference)2 FileOutputStream (java.io.FileOutputStream)1 HSSFHyperlink (org.apache.poi.hssf.usermodel.HSSFHyperlink)1 BaseTestHyperlink (org.apache.poi.ss.usermodel.BaseTestHyperlink)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)1 XSSFHyperlink (org.apache.poi.xssf.usermodel.XSSFHyperlink)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1