/** * this method return row on given index if row is null then create new row * * @param sheet * current sheet object * @param index * index of current row * @return this method return sheet row on given index * @since POI 4.0.0 */ private XSSFRow getRow(XSSFSheet sheet, int index) { if (sheet.getRow(index) != null) { return sheet.getRow(index); } else { return sheet.createRow(index); } }
/** * @param rowIndex the zero based row index to find from */ private int findStartOfRowOutlineGroup(int rowIndex) { // Find the start of the group. short level = getRow(rowIndex).getCTRow().getOutlineLevel(); int currentRow = rowIndex; while (getRow(currentRow) != null) { if (getRow(currentRow).getCTRow().getOutlineLevel() < level) { return currentRow + 1; } currentRow--; } return currentRow; }
/** * @param row the zero based row index to find from */ public int findEndOfRowOutlineGroup(int row) { short level = getRow(row).getCTRow().getOutlineLevel(); int currentRow; final int lastRowNum = getLastRowNum(); for (currentRow = row; currentRow < lastRowNum; currentRow++) { if (getRow(currentRow) == null || getRow(currentRow).getCTRow().getOutlineLevel() < level) { break; } } return currentRow; }
if (createRowIfMissing) { for (int i = startRowNum; i <= endRowNum; i++) { XSSFRow row = getRow(i); if (row == null) { row = createRow(i);
/** * @param sheet * @param row * @return height in twips (1/20th of point) for row or default */ private static float getRowHeight(XSSFSheet sheet, int row) { XSSFRow r = sheet.getRow(row); return r == null ? sheet.getDefaultRowHeightInPoints() : r.getHeightInPoints(); }
protected XSSFCell nextCellVertically(XSSFCellStyle cellStyle) { currentRowNumber++; currentRow = currentSheet.getRow(currentRowNumber); XSSFCell cell = currentRow.createCell(currentColumnNumber); cell.setCellStyle(cellStyle); return cell; }
boolean endHidden; int endOfOutlineGroupIdx = findEndOfRowOutlineGroup(row); if (getRow(endOfOutlineGroupIdx) == null) { endLevel = 0; endHidden = false; } else { endLevel = getRow(endOfOutlineGroupIdx).getCTRow().getOutlineLevel(); endHidden = getRow(endOfOutlineGroupIdx).getCTRow().getHidden(); int startOfOutlineGroupIdx = findStartOfRowOutlineGroup(row); if (startOfOutlineGroupIdx < 0 || getRow(startOfOutlineGroupIdx) == null) { startLevel = 0; startHidden = false; } else { startLevel = getRow(startOfOutlineGroupIdx).getCTRow() .getOutlineLevel(); startHidden = getRow(startOfOutlineGroupIdx).getCTRow() .getHidden();
/** * @param rowIndex the zero based row index to collapse */ private void collapseRow(int rowIndex) { XSSFRow row = getRow(rowIndex); if (row != null) { int startRow = findStartOfRowOutlineGroup(rowIndex); // Hide all the columns until the end of the group int lastRow = writeHidden(row, startRow, true); if (getRow(lastRow) != null) { getRow(lastRow).getCTRow().setCollapsed(true); } else { XSSFRow newRow = createRow(lastRow); newRow.getCTRow().setCollapsed(true); } } }
private void writeDaySheet(LocalDate day, List<Timeslot> timeslotList, List<Talk> talkList) { nextSheet(DAY_FORMATTER.format(day), 1, 1, true); nextRow(); nextHeaderCell(DAY_FORMATTER.format(day)); writeTimeslotHoursVertically(timeslotList); List<Room> dayRoomList = talkList.stream().map(Talk::getRoom).distinct().collect(toList()); dayRoomList.sort(Comparator.comparing(Room::getName)); for (Room room : dayRoomList) { currentColumnNumber++; currentRowNumber = -1; nextCellVertically().setCellValue(room.getName()); List<Talk> roomTalkList = talkList.stream() .filter(talk -> talk.getRoom() == room) .collect(toList()); writeRoomTalks(timeslotList, room, roomTalkList); } currentSheet.autoSizeColumn(0); for (int i = 1; i < currentSheet.getRow(0).getPhysicalNumberOfCells(); i++) { currentSheet.setColumnWidth(i, 15 * 256); } }
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)); } }
XSSFRow row = _xs.getRow(rowIndex); if (row == 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; }
/** * Tie a range of cell together so that they can be collapsed or expanded * * @param fromRow start row (0-based) * @param toRow end row (0-based) */ @Override public void groupRow(int fromRow, int toRow) { for (int i = fromRow; i <= toRow; i++) { XSSFRow xrow = getRow(i); if (xrow == null) { xrow = createRow(i); } CTRow ctrow = xrow.getCTRow(); short outlineLevel = ctrow.getOutlineLevel(); ctrow.setOutlineLevel((short) (outlineLevel + 1)); } setSheetFormatPrOutlineLevelRow(); }
/** * 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); }
/** * Ungroup a range of rows that were previously groupped * * @param fromRow start row (0-based) * @param toRow end row (0-based) */ @Override public void ungroupRow(int fromRow, int toRow) { for (int i = fromRow; i <= toRow; i++) { XSSFRow xrow = getRow(i); if (xrow != null) { CTRow ctRow = xrow.getCTRow(); int outlineLevel = ctRow.getOutlineLevel(); ctRow.setOutlineLevel((short) (outlineLevel - 1)); //remove a row only if the row has no cell and if the outline level is 0 if (outlineLevel == 1 && xrow.getFirstCellNum() == -1) { removeRow(xrow); } } } setSheetFormatPrOutlineLevelRow(); }
XSSFRow row = getRow(rowIn); if (row == 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; }
XSSFRow row = sheet.getRow(headerRow); DataFormatter formatter = new DataFormatter();
/** * 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; } } } } } }