Search in sources :

Example 6 with CTCell

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell in project poi by apache.

the class XSSFCell method setBlank.

/**
     * Blanks this cell. Blank cells have no formula or value but may have styling.
     * This method erases all the data previously associated with this cell.
     */
private void setBlank() {
    CTCell blank = CTCell.Factory.newInstance();
    blank.setR(_cell.getR());
    if (_cell.isSetS()) {
        blank.setS(_cell.getS());
    }
    _cell.set(blank);
}
Also used : CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell)

Example 7 with CTCell

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell in project poi by apache.

the class XSSFRow method onDocumentWrite.

/**
     * Fired when the document is written to an output stream.
     *
     * @see org.apache.poi.xssf.usermodel.XSSFSheet#write(java.io.OutputStream) ()
     */
protected void onDocumentWrite() {
    // check if cells in the CTRow are ordered
    boolean isOrdered = true;
    CTCell[] cArray = _row.getCArray();
    if (cArray.length != _cells.size()) {
        isOrdered = false;
    } else {
        int i = 0;
        for (XSSFCell cell : _cells.values()) {
            CTCell c1 = cell.getCTCell();
            CTCell c2 = cArray[i++];
            String r1 = c1.getR();
            String r2 = c2.getR();
            if (!(r1 == null ? r2 == null : r1.equals(r2))) {
                isOrdered = false;
                break;
            }
        }
    }
    if (!isOrdered) {
        cArray = new CTCell[_cells.size()];
        int i = 0;
        for (XSSFCell xssfCell : _cells.values()) {
            cArray[i] = (CTCell) xssfCell.getCTCell().copy();
            // we have to copy and re-create the XSSFCell here because the 
            // elements as otherwise setCArray below invalidates all the columns!
            // see Bug 56170, XMLBeans seems to always release previous objects
            // in the CArray, so we need to provide completely new ones here!
            //_cells.put(entry.getKey(), new XSSFCell(this, cArray[i]));
            xssfCell.setCTCell(cArray[i]);
            i++;
        }
        _row.setCArray(cArray);
    }
}
Also used : CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell)

Example 8 with CTCell

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell in project poi by apache.

the class TestXSSFCell method testFormulaString.

@Test
public void testFormulaString() throws IOException {
    XSSFWorkbook wb = (XSSFWorkbook) _testDataProvider.createWorkbook();
    try {
        XSSFCell cell = wb.createSheet().createRow(0).createCell(0);
        //low-level bean holding cell's xml
        CTCell ctCell = cell.getCTCell();
        cell.setCellFormula("A2");
        assertEquals(CellType.FORMULA, cell.getCellTypeEnum());
        assertEquals("A2", cell.getCellFormula());
        //the value is not set and cell's type='N' which means blank
        assertEquals(STCellType.N, ctCell.getT());
        //set cached formula value
        cell.setCellValue("t='str'");
        //we are still of 'formula' type
        assertEquals(CellType.FORMULA, cell.getCellTypeEnum());
        assertEquals("A2", cell.getCellFormula());
        //cached formula value is set and cell's type='STR'
        assertEquals(STCellType.STR, ctCell.getT());
        assertEquals("t='str'", cell.getStringCellValue());
        //now remove the formula, the cached formula result remains
        cell.setCellFormula(null);
        assertEquals(CellType.STRING, cell.getCellTypeEnum());
        assertEquals(STCellType.STR, ctCell.getT());
        //the line below failed prior to fix of Bug #47889
        assertEquals("t='str'", cell.getStringCellValue());
        //revert to a blank cell
        cell.setCellValue((String) null);
        assertEquals(CellType.BLANK, cell.getCellTypeEnum());
        assertEquals(STCellType.N, ctCell.getT());
        assertEquals("", cell.getStringCellValue());
    } finally {
        wb.close();
    }
}
Also used : CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) Test(org.junit.Test)

Aggregations

CTCell (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell)8 Cell (org.apache.poi.ss.usermodel.Cell)2 Test (org.junit.Test)2 CTCellFormula (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula)2 AssertionFailedError (junit.framework.AssertionFailedError)1 CellReference (org.apache.poi.ss.util.CellReference)1 Internal (org.apache.poi.util.Internal)1 CalculationChain (org.apache.poi.xssf.model.CalculationChain)1 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)1 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)1 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)1 CTRow (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow)1 CTSheetData (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetData)1 CTWorksheet (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet)1