Example usage for org.apache.poi.ss.usermodel Row removeCell

List of usage examples for org.apache.poi.ss.usermodel Row removeCell

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Row removeCell.

Prototype

void removeCell(Cell cell);

Source Link

Document

Remove the Cell from this row.

Usage

From source file:org.datanucleus.store.excel.ExcelPersistenceHandler.java

License:Open Source License

/**
 * Deletes a persistent object from the database.
 * @param op The Object Provider of the object to be deleted.
 * @throws NucleusDataStoreException when an error occurs in the datastore communication
 * @throws NucleusOptimisticException thrown if version checking fails on an optimistic transaction for this object
 *//*from   ww w .  ja v a 2s  .com*/
public void deleteObject(ObjectProvider op) {
    // Check if read-only so update not permitted
    assertReadOnlyForUpdateOfObject(op);

    ExecutionContext ec = op.getExecutionContext();
    ManagedConnection mconn = storeMgr.getConnection(ec);
    try {
        AbstractClassMetaData cmd = op.getClassMetaData();
        if (cmd.isVersioned()) {
            NucleusLogger.PERSISTENCE.warn(
                    "This datastore doesn't support optimistic version checks since the datastore file is for a single-connection");
        }

        Workbook wb = (Workbook) mconn.getConnection();
        Table table = ec.getStoreManager().getStoreDataForClass(op.getClassMetaData().getFullClassName())
                .getTable();
        final Sheet sheet = ExcelUtils.getSheetForClass(op, wb, table);

        // Invoke any cascade deletion
        op.loadUnloadedFields();
        op.provideFields(cmd.getAllMemberPositions(), new DeleteFieldManager(op));

        // Delete this object
        long startTime = System.currentTimeMillis();
        if (NucleusLogger.DATASTORE_PERSIST.isDebugEnabled()) {
            NucleusLogger.DATASTORE_PERSIST.debug(
                    Localiser.msg("Excel.Delete.Start", op.getObjectAsPrintable(), op.getInternalObjectId()));
        }

        int rowId = ExcelUtils.getRowNumberForObjectInWorkbook(op, wb, false, table);
        if (rowId < 0) {
            throw new NucleusObjectNotFoundException("object not found", op.getObject());
        }

        if (storeMgr instanceof XLSStoreManager && sheet.getLastRowNum() == rowId) {
            // Deleting top row which is last row so just remove all cells and leave row
            // otherwise Apache POI throws an ArrayIndexOutOfBoundsException
            Row row = sheet.getRow(rowId);
            Iterator<Cell> it = row.cellIterator();
            while (it.hasNext()) {
                row.removeCell(it.next());
            }
        } else {
            // Deleting top row so remove it
            sheet.removeRow(sheet.getRow(rowId));
            if (sheet.getLastRowNum() > rowId) {
                sheet.shiftRows(rowId + 1, sheet.getLastRowNum(), -1);
            }
        }

        if (NucleusLogger.DATASTORE_PERSIST.isDebugEnabled()) {
            NucleusLogger.DATASTORE_PERSIST
                    .debug(Localiser.msg("Excel.ExecutionTime", (System.currentTimeMillis() - startTime)));
        }
        if (ec.getStatistics() != null) {
            ec.getStatistics().incrementNumWrites();
            ec.getStatistics().incrementDeleteCount();
        }
    } finally {
        mconn.release();
    }
}

From source file:org.generationcp.middleware.util.PoiUtil.java

License:Open Source License

/**
 * Given a sheet, this method deletes a column from a sheet and moves
 * all the columns to the right of it to the left one cell.
 *
 * Note, this method will not update any formula references.
 *
 * @param sheet// w  ww  .j  ava2 s .co  m
 * @param column
 */
public static void deleteColumn(final Sheet sheet, final int columnToDelete) {
    int maxColumn = 0;
    for (int r = 0; r < sheet.getLastRowNum() + 1; r++) {
        final Row row = sheet.getRow(r);

        // if no row exists here; then nothing to do; next!
        if (row == null)
            continue;

        // if the row doesn't have this many columns then we are good; next!
        final int lastColumn = row.getLastCellNum();
        if (lastColumn > maxColumn)
            maxColumn = lastColumn;

        if (lastColumn < columnToDelete)
            continue;

        for (int x = columnToDelete + 1; x < lastColumn + 1; x++) {
            final Cell oldCell = row.getCell(x - 1);
            if (oldCell != null)
                row.removeCell(oldCell);

            final Cell nextCell = row.getCell(x);
            if (nextCell != null) {
                final Cell newCell = row.createCell(x - 1, nextCell.getCellType());
                cloneCell(newCell, nextCell);
            }
        }
    }

}

From source file:org.joeffice.spreadsheet.cell.CellUtils.java

License:Apache License

/**
 * Copy a cell to another column in the same row
 *
 * @param input/*www.  j  a v a  2 s  .co m*/
 * @param column
 */
public static void copyCellToColumn(Row row, Cell input, int column) {
    if (input == null) {
        Cell destCell = row.getCell(column);
        if (destCell != null) {
            row.removeCell(destCell);
        }
    } else {
        Cell destCell = row.getCell(column);
        if (destCell == null) {
            destCell = row.createCell(column, input.getCellType());
        }
        copyCell(input, destCell);
    }
}

From source file:org.joeffice.spreadsheet.sheet.SheetTableModel.java

License:Apache License

public void removeColumns(int... columns) {
    for (int rowIndex = 0; rowIndex < sheet.getLastRowNum(); rowIndex++) {
        Row row = sheet.getRow(rowIndex);
        if (row != null) {
            short lastColumn = row.getLastCellNum();
            for (int i = columns.length; i >= 0; i--) {
                int columnIndex = columns[i];
                if (columnIndex <= lastColumn) {
                    Cell cell = row.getCell(columnIndex);
                    // I'm afraid that this only clear the cell and doesn't shift
                    // Also shiting columns is not supported in POI, so nothing happens for empty cells
                    if (cell != null) {
                        row.removeCell(cell);
                    }//from w w w .j  av  a2s . c o m
                }
            }
        }
    }
    fireTableStructureChanged();
}

From source file:org.joeffice.spreadsheet.sheet.SheetTableModel.java

License:Apache License

public void deleteCell(int rowIndex, int columnIndex) {
    Row row = sheet.getRow(rowIndex);
    if (row != null) {
        Cell cell = row.getCell(columnIndex);
        if (cell != null) {
            row.removeCell(cell);
        }// ww  w  .j  av  a  2s .  co  m
    }
}

From source file:org.openelis.bean.QcChartReport1Bean.java

License:Open Source License

private void removeColumn(HSSFSheet sheet, Integer columnIndex) {
    int i, j;/*from ww w  .  j a v a 2s . c  om*/
    Cell cell;
    Row row;

    for (i = 31; i <= sheet.getLastRowNum(); i++) {
        row = sheet.getRow(i);
        cell = row.getCell(columnIndex);
        if (cell != null)
            row.removeCell(row.getCell(columnIndex));
        for (j = columnIndex + 1; j < row.getLastCellNum(); j++) {
            cell = row.getCell(j);
            if (cell != null)
                ((HSSFRow) row).moveCell((HSSFCell) cell, (short) (j - 1));
        }
    }
}

From source file:org.spdx.spdxspreadsheet.OriginsSheetV1d2.java

License:Apache License

public void setCreatedBy(String[] createdBy) {
    if (createdBy == null || createdBy.length < 1) {
        setDataCellStringValue(CREATED_BY_COL, "");
        int i = firstRowNum + DATA_ROW_NUM + 1;
        Row nextRow = sheet.getRow(i);/*w  w  w .  ja  v a  2 s  .com*/
        while (nextRow != null) {
            Cell createdByCell = nextRow.getCell(CREATED_BY_COL);
            if (createdByCell != null) {
                createdByCell.setCellValue("");
            }
            i++;
            nextRow = sheet.getRow(i);
        }
        return;
    }
    setDataCellStringValue(CREATED_BY_COL, createdBy[0]);
    for (int i = 1; i < createdBy.length; i++) {
        Row row = getDataRow(i);
        Cell cell = row.getCell(CREATED_BY_COL);
        if (cell == null) {
            cell = row.createCell(CREATED_BY_COL);
        }
        cell.setCellValue(createdBy[i]);
    }
    // delete any remaining rows
    for (int i = firstRowNum + DATA_ROW_NUM + createdBy.length; i <= this.lastRowNum; i++) {
        Row row = sheet.getRow(i);
        Cell cell = row.getCell(CREATED_BY_COL);
        if (cell != null) {
            row.removeCell(cell);
        }
    }
}

From source file:org.spdx.spdxspreadsheet.OriginsSheetV2d0.java

License:Apache License

@Override
public void setDocumentDescribes(String[] contents) {
    if (contents == null || contents.length < 1) {
        setDataCellStringValue(DOCUMENT_DESCRIBES_COL, "");
        int i = firstRowNum + DATA_ROW_NUM + 1;
        Row nextRow = sheet.getRow(i);//from w  w w.  j  a  v a  2s .  c o  m
        while (nextRow != null) {
            Cell documentDescribesCell = nextRow.getCell(DOCUMENT_DESCRIBES_COL);
            if (documentDescribesCell != null) {
                documentDescribesCell.setCellValue("");
            }
            i++;
            nextRow = sheet.getRow(i);
        }
        return;
    }
    setDataCellStringValue(DOCUMENT_DESCRIBES_COL, contents[0]);
    for (int i = 1; i < contents.length; i++) {
        Row row = getDataRow(i);
        Cell cell = row.getCell(DOCUMENT_DESCRIBES_COL);
        if (cell == null) {
            cell = row.createCell(DOCUMENT_DESCRIBES_COL);
        }
        cell.setCellValue(contents[i]);
    }
    // delete any remaining rows
    for (int i = firstRowNum + DATA_ROW_NUM + contents.length; i <= this.lastRowNum; i++) {
        Row row = sheet.getRow(i);
        Cell cell = row.getCell(DOCUMENT_DESCRIBES_COL);
        if (cell != null) {
            row.removeCell(cell);
        }
    }
}

From source file:org.spdx.spdxspreadsheet.OriginsSheetV2d0.java

License:Apache License

@Override
public void setExternalDocumentRefs(ExternalDocumentRef[] externalDocumentRefs) throws SpreadsheetException {
    if (externalDocumentRefs == null || externalDocumentRefs.length < 1) {
        setDataCellStringValue(EXTERNAL_DOC_REFS_COL, "");
        int i = firstRowNum + DATA_ROW_NUM + 1;
        Row nextRow = sheet.getRow(i);/*from   ww w .j a v  a 2  s  . c o m*/
        while (nextRow != null) {
            Cell externalDocRefsCell = nextRow.getCell(EXTERNAL_DOC_REFS_COL);
            if (externalDocRefsCell != null) {
                externalDocRefsCell.setCellValue("");
            }
            i++;
            nextRow = sheet.getRow(i);
        }
        return;
    }
    try {
        setDataCellStringValue(EXTERNAL_DOC_REFS_COL, externalDocRefToStr(externalDocumentRefs[0]));
    } catch (InvalidSPDXAnalysisException e) {
        throw (new SpreadsheetException("Error getting external document reference", e));
    }
    for (int i = 1; i < externalDocumentRefs.length; i++) {
        Row row = getDataRow(i);
        Cell cell = row.getCell(EXTERNAL_DOC_REFS_COL);
        if (cell == null) {
            cell = row.createCell(EXTERNAL_DOC_REFS_COL);
        }
        try {
            cell.setCellValue(externalDocRefToStr(externalDocumentRefs[i]));
        } catch (InvalidSPDXAnalysisException e) {
            throw (new SpreadsheetException("Error getting external document reference", e));
        }
    }
    // delete any remaining rows
    for (int i = firstRowNum + DATA_ROW_NUM + externalDocumentRefs.length; i <= this.lastRowNum; i++) {
        Row row = sheet.getRow(i);
        Cell cell = row.getCell(EXTERNAL_DOC_REFS_COL);
        if (cell != null) {
            row.removeCell(cell);
        }
    }
}

From source file:org.tiefaces.components.websheet.utility.CellUtility.java

License:MIT License

/**
 * Copy cell.// w w w  . ja  v  a  2  s .  co m
 *
 * @param destSheet
 *            the dest sheet
 * @param sourceRow
 *            the source row
 * @param newRow
 *            the new row
 * @param cellIndex
 *            the cell index
 * @param checkLock
 *            the check lock
 * @return the int
 */
public static Cell copyCell(final Sheet destSheet, final Row sourceRow, final Row newRow, final int cellIndex,
        final boolean checkLock) {
    // If the old cell is null jump to next cell
    Cell sourceCell = sourceRow.getCell(cellIndex);
    if (sourceCell == null) {
        return null;
    }
    // If source cell is dest cell refresh it
    boolean refreshCell = false;
    if (sourceRow.equals(newRow) && (sourceCell.getColumnIndex() == cellIndex)) {
        sourceRow.removeCell(sourceCell);
        refreshCell = true;
    }
    Cell newCell = newRow.createCell(cellIndex);
    try {
        if (!refreshCell && (sourceCell.getCellComment() != null)) {
            // If there is a cell comment, copy
            cloneComment(sourceCell, newCell);
        }
        copyCellSetStyle(destSheet, sourceCell, newCell);
        copyCellSetValue(sourceCell, newCell, checkLock);
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "copy cell set error = " + ex.getLocalizedMessage(), ex);
    }

    return newCell;
}