/** * 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; if (_row.sizeOfCArray() != _cells.size()) isOrdered = false; else { int i = 0; CTCell[] xcell = _row.getCArray(); for (XSSFCell cell : _cells.values()) { CTCell c1 = cell.getCTCell(); CTCell c2 = xcell[i++]; String r1 = c1.getR(); String r2 = c2.getR(); if (!(r1 == null ? r2 == null : r1.equals(r2))) { isOrdered = false; break; } } } if (!isOrdered) { CTCell[] cArray = new CTCell[_cells.size()]; int i = 0; for (XSSFCell c : _cells.values()) { cArray[i++] = c.getCTCell(); } _row.setCArray(cArray); } }
/** * Use this to create new cells within the row and return it. * * @param columnIndex - the column number this cell represents * @param type - the cell's data type * @return XSSFCell a high level representation of the created cell. * @throws IllegalArgumentException if the specified cell type is invalid, columnIndex < 0 or * greater than 16384, the maximum number of columns supported by the SpreadsheetML format * (.xlsx) * @see Cell#CELL_TYPE_BLANK * @see Cell#CELL_TYPE_BOOLEAN * @see Cell#CELL_TYPE_ERROR * @see Cell#CELL_TYPE_FORMULA * @see Cell#CELL_TYPE_NUMERIC * @see Cell#CELL_TYPE_STRING */ public XSSFCell createCell(int columnIndex, int type) { CTCell ctCell; XSSFCell prev = _cells.get(columnIndex); if (prev != null) { ctCell = prev.getCTCell(); ctCell.set(CTCell.Factory.newInstance()); } else { ctCell = _row.addNewC(); } XSSFCell xcell = new XSSFCell(this, ctCell); xcell.setCellNum(columnIndex); if (type != Cell.CELL_TYPE_BLANK) { xcell.setCellType(type); } _cells.put(columnIndex, xcell); return xcell; }
/** * update cell references when shifting rows * * @param n the number of rows to move */ protected void shift(int n) { int rownum = getRowNum() + n; CalculationChain calcChain = _sheet.getWorkbook().getCalculationChain(); int sheetId = (int) _sheet.sheet.getSheetId(); String msg = "Row[rownum=" + getRowNum() + "] contains cell(s) included in a multi-cell array formula. " + "You cannot change part of an array."; for (Cell c : this) { XSSFCell cell = (XSSFCell) c; if (cell.isPartOfArrayFormulaGroup()) { cell.notifyArrayFormulaChanging(msg); } // remove the reference in the calculation chain if (calcChain != null) calcChain.removeItem(sheetId, cell.getReference()); CTCell ctCell = cell.getCTCell(); String r = new CellReference(rownum, cell.getColumnIndex()).formatAsString(); ctCell.setR(r); } setRowNum(rownum); }