/** * Use this to create new cells within the row and return it. * <p> * The cell that is returned is a {@link CellType#BLANK}. The type can be changed * either through calling <code>setCellValue</code> or <code>setCellType</code>. * </p> * @param columnIndex - the column number this cell represents * @return Cell a high level representation of the created cell. * @throws IllegalArgumentException if columnIndex < 0 or greater than 16384, * the maximum number of columns supported by the SpreadsheetML format (.xlsx) */ @Override public XSSFCell createCell(int columnIndex) { return createCell(columnIndex, CellType.BLANK); }
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; }
protected XSSFCell nextCell(XSSFCellStyle cellStyle) { currentColumnNumber++; XSSFCell cell = currentRow.createCell(currentColumnNumber); cell.setCellStyle(cellStyle); return cell; }
/** * Returns the cell at the given (0 based) index, with the specified {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy} * * @return the cell at the given (0 based) index * @throws IllegalArgumentException if cellnum < 0 or the specified MissingCellPolicy is invalid */ @Override public XSSFCell getCell(int cellnum, MissingCellPolicy policy) { if(cellnum < 0) { throw new IllegalArgumentException("Cell index must be >= 0"); } // Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory final Integer colI = Integer.valueOf(cellnum); // NOSONAR XSSFCell cell = _cells.get(colI); switch (policy) { case RETURN_NULL_AND_BLANK: return cell; case RETURN_BLANK_AS_NULL: boolean isBlank = (cell != null && cell.getCellType() == CellType.BLANK); return (isBlank) ? null : cell; case CREATE_NULL_AS_BLANK: return (cell == null) ? createCell(cellnum, CellType.BLANK) : cell; default: throw new IllegalArgumentException("Illegal policy " + policy); } }
XSSFDataFormat df = workBook.createDataFormat(); cs.setDataFormat(df.getFormat("d-mmm-yy")); // Get / Create our cell XSSFRow row = sheet.createRow(2); XSSFCell cell = row.createCell(3); // Set it to be a date Calendar c = Calendar.getInstance(); c.set(2012,3-1,18); // Don't forget months are 0 based on Calendar cell.setCellValue( c.getTime() ); // Style it as a date cell.setCellStyle(cs);
protected XSSFCell nextCellVertically(XSSFCellStyle cellStyle) { currentRowNumber++; currentRow = currentSheet.getRow(currentRowNumber); XSSFCell cell = currentRow.createCell(currentColumnNumber); cell.setCellStyle(cellStyle); return cell; }
/** * 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); } }
/** * 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); }
cell = row.createCell(columnId);
/** * 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; }
for (final Cell c : srcRow){ final XSSFCell srcCell = (XSSFCell)c; final XSSFCell destCell = createCell(srcCell.getColumnIndex(), srcCell.getCellType()); destCell.copyCellFrom(srcCell, policy);
XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); XSSFRow row = sheet.createRow(0); XSSFCell cell = row.createCell( 0); cell.setCellValue("custom XSSF colors"); XSSFCellStyle style1 = wb.createCellStyle(); style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128))); style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
XSSFRow row = null; int rowCounter = 0; int cellCounter = 0; for (int i = 0; i < lotto.length; i++) { if ((i % 6) == 0) { cellCounter = 0; row = (XSSFRow) sheet.createRow(rowCounter); rowCounter++; } row.createCell(cellCounter).setCellValue(lotto[i]); cellCounter++; }
private XSSFCell createCell(XSSFSheet tableSheet, int cellRow, int cellColumn, Set<String> table) { String key = cellRow + "-" + cellColumn; if (!table.add(key)) { return null; } XSSFRow rh = tableSheet.getRow(cellRow); table.add(key); return rh.createCell(cellColumn); }
private void cleaner(Integer columnsCount, Integer rowsCount, Object object){ XSSFSheet sheet = workBook.getSheetAt(0); for (int lineId=0; lineId < rowsCount; lineId++) { XSSFRow row = sheet.getRow(lineId); if(row != null) for (int columnId=0; columnId < columnsCount; columnId++) row.createCell(columnId).setCellValue(object.toString()); } }
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 static XSSFRow createDataRow(XSSFSheet sheet, Set columnSet, Map dataMap, int rowIndex){ XSSFRow dataRow = sheet.createRow(rowIndex); dataRow.setHeight((short) 300); // Row Height int dataCellIndex = 0; for(Object columnName : columnSet){ // Column XSSFCell cell = dataRow.createCell((short) dataCellIndex); cell.setCellValue(dataMap.get(columnName).toString()); dataCellIndex++; } return dataRow; }
XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); XSSFRow row = sheet.createRow(0); XSSFCell cell = row.createCell( 0); cell.setCellValue("custom XSSF colors"); XSSFCellStyle style1 = wb.createCellStyle(); style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128))); style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
private static void addValueCell(XSSFSheet sheet, Object value) { short lastCell = sheet .getRow(sheet.getLastRowNum()) .getLastCellNum(); lastCell = lastCell < 0 ? 0 : lastCell; sheet .getRow(sheet.getLastRowNum()) .createCell(lastCell) .setCellValue("" + value); }
private static void addValueCell(XSSFSheet sheet, Object value) { short lastCell = sheet .getRow(sheet.getLastRowNum()) .getLastCellNum(); lastCell = lastCell < 0 ? 0 : lastCell; sheet .getRow(sheet.getLastRowNum()) .createCell(lastCell) .setCellValue("" + value); }