Search in sources :

Example 11 with CellCopyPolicy

use of org.apache.poi.ss.usermodel.CellCopyPolicy in project poi by apache.

the class TestXSSFCell method testCopyCellFrom_CellCopyPolicy_default.

@Test
public final void testCopyCellFrom_CellCopyPolicy_default() {
    setUp_testCopyCellFrom_CellCopyPolicy();
    // default copy policy
    final CellCopyPolicy policy = new CellCopyPolicy();
    destCell.copyCellFrom(srcCell, policy);
    assertEquals(CellType.FORMULA, destCell.getCellTypeEnum());
    assertEquals("2+3", destCell.getCellFormula());
    assertEquals(srcCell.getCellStyle(), destCell.getCellStyle());
}
Also used : CellCopyPolicy(org.apache.poi.ss.usermodel.CellCopyPolicy) Test(org.junit.Test)

Example 12 with CellCopyPolicy

use of org.apache.poi.ss.usermodel.CellCopyPolicy 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)

Aggregations

CellCopyPolicy (org.apache.poi.ss.usermodel.CellCopyPolicy)12 Test (org.junit.Test)9 Row (org.apache.poi.ss.usermodel.Row)5 Cell (org.apache.poi.ss.usermodel.Cell)3 CellReference (org.apache.poi.ss.util.CellReference)3 Date (java.util.Date)2 BaseTestXRow (org.apache.poi.ss.usermodel.BaseTestXRow)2 CellStyle (org.apache.poi.ss.usermodel.CellStyle)2 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)2 Font (org.apache.poi.ss.usermodel.Font)2 FormulaError (org.apache.poi.ss.usermodel.FormulaError)2 Hyperlink (org.apache.poi.ss.usermodel.Hyperlink)2 Sheet (org.apache.poi.ss.usermodel.Sheet)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)2 CTCell (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell)2 CTRow (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow)2 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)1 Beta (org.apache.poi.util.Beta)1