/** * this method return cell on given index if cell is null then create new * cell * * @param row * current row object * @param index * index of current cell * @return this method return sheet cell on given index * @since POI 4.0.0 */ private XSSFCell getCell(XSSFRow row, int index) { if (row.getCell(index) != null) { return row.getCell(index); } else { return row.createCell(index); } }
protected XSSFCell nextCell() { currentColumnNumber++; XSSFCell cell = currentRow.getCell(currentColumnNumber); // TODO HACK to workaround the fact that LibreOffice and Excel automatically remove empty trailing cells if (cell == null) { // Return dummy cell return currentRow.createCell(currentColumnNumber); } return cell; }
private void shiftCell(int columnIndex, int step/*pass negative value for left shift*/){ if(columnIndex + step < 0) { throw new IllegalStateException("Column index less than zero : " + (Integer.valueOf(columnIndex + step)).toString()); } XSSFCell currentCell = getCell(columnIndex); if(currentCell != null){ currentCell.setCellNum(columnIndex+step); _cells.put(columnIndex+step, currentCell); } else { _cells.remove(columnIndex+step); XSSFCell targetCell = getCell(columnIndex+step); if(targetCell != null) { targetCell.getCTCell().set(CTCell.Factory.newInstance()); } } } }
/** * Returns the cell at the given (0 based) index, * with the {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy} from the parent Workbook. * * @return the cell at the given (0 based) index */ @Override public XSSFCell getCell(int cellnum) { return getCell(cellnum, _sheet.getWorkbook().getMissingCellPolicy()); }
/** * Shifts column range [firstShiftColumnIndex-lastShiftColumnIndex] step places to the left. * @param firstShiftColumnIndex the column to start shifting * @param lastShiftColumnIndex the column to end shifting * @param step length of the shifting step */ @Override public void shiftCellsLeft(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { if(step < 0) { throw new IllegalArgumentException("Shifting step may not be negative "); } if(firstShiftColumnIndex > lastShiftColumnIndex) { throw new IllegalArgumentException(String.format(LocaleUtil.getUserLocale(), "Incorrect shifting range : %d-%d", firstShiftColumnIndex, lastShiftColumnIndex)); } if(firstShiftColumnIndex - step < 0) { throw new IllegalStateException("Column index less than zero : " + (Integer.valueOf(firstShiftColumnIndex + step)).toString()); } for (int columnIndex = firstShiftColumnIndex; columnIndex <= lastShiftColumnIndex; columnIndex++){ shiftCell(columnIndex, -step); } for (int columnIndex = lastShiftColumnIndex-step+1; columnIndex <= lastShiftColumnIndex; columnIndex++){ _cells.remove(columnIndex); XSSFCell targetCell = getCell(columnIndex); if(targetCell != null) { targetCell.getCTCell().set(CTCell.Factory.newInstance()); } } } private void shiftCell(int columnIndex, int step/*pass negative value for left shift*/){
/** * Shifts column range [firstShiftColumnIndex-lastShiftColumnIndex] step places to the right. * @param firstShiftColumnIndex the column to start shifting * @param lastShiftColumnIndex the column to end shifting * @param step length of the shifting step */ @Override public void shiftCellsRight(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { if(step < 0) { throw new IllegalArgumentException("Shifting step may not be negative "); } if(firstShiftColumnIndex > lastShiftColumnIndex) { throw new IllegalArgumentException(String.format(LocaleUtil.getUserLocale(), "Incorrect shifting range : %d-%d", firstShiftColumnIndex, lastShiftColumnIndex)); } for (int columnIndex = lastShiftColumnIndex; columnIndex >= firstShiftColumnIndex; columnIndex--){ // process cells backwards, because of shifting shiftCell(columnIndex, step); } for (int columnIndex = firstShiftColumnIndex; columnIndex <= firstShiftColumnIndex+step-1; columnIndex++) { _cells.remove(columnIndex); XSSFCell targetCell = getCell(columnIndex); if(targetCell != null) { targetCell.getCTCell().set(CTCell.Factory.newInstance()); } } } /**
protected CellValue getCellValueAt(int index) { if (index < 0 || index >= numOfCells) { throw new IndexOutOfBoundsException( "Index must be between 0 and " + (numOfCells - 1) + " (inclusive), given: " + index); } int firstRow = cellRangeAddress.getFirstRow(); int firstCol = cellRangeAddress.getFirstColumn(); int lastCol = cellRangeAddress.getLastColumn(); int width = lastCol - firstCol + 1; int rowIndex = firstRow + index / width; int cellIndex = firstCol + index % width; XSSFRow row = sheet.getRow(rowIndex); return (row == null) ? null : evaluator.evaluate(row.getCell(cellIndex)); } }
return null; XSSFCell cell = row.getCell(columnIndex); if (cell == null) { return null;
/* package */ XSSFCell getFirstCellInArrayFormula(XSSFCell cell) { for (CellRangeAddress range : arrayFormulas) { if (range.isInRange(cell.getRowIndex(), cell.getColumnIndex())) { return getRow(range.getFirstRow()).getCell(range.getFirstColumn()); } } return null; }
prev.removeCell(prev.getCell(prev.getFirstCellNum()));
XSSFCell cell = row.getCell(startColumnIndex + tableColumn.getColumnIndex()); if (cell != null) { XSSFXmlColumnPr xmlColumnPr = tableColumn.getXmlColumnPr();
private void readTimeslotDays() { columnIndexToDateMap.clear(); String previousDateString = null; for (int i = 0; i < currentRow.getLastCellNum(); i++) { XSSFCell cell = currentRow.getCell(i); if (!cell.getStringCellValue().isEmpty() || previousDateString != null) { if (!cell.getStringCellValue().isEmpty()) { previousDateString = cell.getStringCellValue(); } try { columnIndexToDateMap.put(i, LocalDate.parse(previousDateString, DAY_FORMATTER)); } catch (DateTimeParseException e) { throw new IllegalStateException(currentPosition() + ": The date (" + cell.getStringCellValue() + ") does not parse as a date."); } } } }
/** * Also creates cells if they don't exist */ private CellRange<XSSFCell> getCellRange(CellRangeAddress range) { int firstRow = range.getFirstRow(); int firstColumn = range.getFirstColumn(); int lastRow = range.getLastRow(); int lastColumn = range.getLastColumn(); int height = lastRow - firstRow + 1; int width = lastColumn - firstColumn + 1; List<XSSFCell> temp = new ArrayList<>(height * width); for (int rowIn = firstRow; rowIn <= lastRow; rowIn++) { for (int colIn = firstColumn; colIn <= lastColumn; colIn++) { XSSFRow row = getRow(rowIn); if (row == null) { row = createRow(rowIn); } XSSFCell cell = row.getCell(colIn); if (cell == null) { cell = row.createCell(colIn); } temp.add(cell); } } return SSCellRange.create(firstRow, firstColumn, height, width, temp, XSSFCell.class); }
XSSFCell cell = row.getCell(columnId); if (cell == null) { cell = row.createCell(columnId);
XSSFCell cell = row.getCell(colIn); if (cell == null) { continue;
/** * Gets the XSSFCell referenced by the R attribute or creates a new one if cell doesn't exists * @return the referenced XSSFCell, null if the cell reference is invalid */ public XSSFCell getReferencedCell(){ XSSFCell cell = null; CellReference cellReference = new CellReference(singleXmlCell.getR()); XSSFRow row = parent.getXSSFSheet().getRow(cellReference.getRow()); if(row==null){ row = parent.getXSSFSheet().createRow(cellReference.getRow()); } cell = row.getCell(cellReference.getCol()); if(cell==null){ cell = row.createCell(cellReference.getCol()); } return cell; }
/** * Construct a XSSFCell. * * @param row the parent row. * @param cell the xml bean containing information about the cell. */ protected XSSFCell(XSSFRow row, CTCell cell) { _cell = cell; _row = row; if (cell.getR() != null) { _cellNum = new CellReference(cell.getR()).getCol(); } else { int prevNum = row.getLastCellNum(); if(prevNum != -1){ _cellNum = row.getCell(prevNum-1, MissingCellPolicy.RETURN_NULL_AND_BLANK).getColumnIndex() + 1; } } _sharedStringSource = row.getSheet().getWorkbook().getSharedStringSource(); _stylesSource = row.getSheet().getWorkbook().getStylesSource(); }
if(ctTableColumns != null) { for (CTTableColumn col : ctTableColumns.getTableColumnList()) { XSSFCell cell = row.getCell(cellnum); if (cell != null) { col.setName(formatter.formatCellValue(cell));
/** * when a cell with a 'master' shared formula is removed, the next cell in the range becomes the master * @param cell The cell that is removed * @param evalWb BaseXSSFEvaluationWorkbook in use, if one exists */ protected void onDeleteFormula(XSSFCell cell, BaseXSSFEvaluationWorkbook evalWb){ CTCellFormula f = cell.getCTCell().getF(); if (f != null && f.getT() == STCellFormulaType.SHARED && f.isSetRef() && f.getStringValue() != null) { CellRangeAddress ref = CellRangeAddress.valueOf(f.getRef()); if(ref.getNumberOfCells() > 1){ DONE: for(int i = cell.getRowIndex(); i <= ref.getLastRow(); i++){ XSSFRow row = getRow(i); if(row != null) for(int j = cell.getColumnIndex(); j <= ref.getLastColumn(); j++){ XSSFCell nextCell = row.getCell(j); if(nextCell != null && nextCell != cell && nextCell.getCellType() == CellType.FORMULA){ CTCellFormula nextF = nextCell.getCTCell().getF(); nextF.setStringValue(nextCell.getCellFormula(evalWb)); CellRangeAddress nextRef = new CellRangeAddress( nextCell.getRowIndex(), ref.getLastRow(), nextCell.getColumnIndex(), ref.getLastColumn()); nextF.setRef(nextRef.formatAsString()); sharedFormulas.put((int)nextF.getSi(), nextF); break DONE; } } } } } }
XSSFRow row = getXSSFSheet().getRow(cellRef.getRow()); if (row != null) { XSSFCell cell = row.getCell(cellRef.getCol()); if (cell != null) { cell.setCellType(CellType.BLANK);