Example usage for org.apache.poi.ss.usermodel Cell getCellComment

List of usage examples for org.apache.poi.ss.usermodel Cell getCellComment

Introduction

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

Prototype

Comment getCellComment();

Source Link

Document

Returns comment associated with this cell

Usage

From source file:de.jlo.talendcomp.excel.SpreadsheetOutput.java

License:Apache License

private void setCellComment(Cell cell, String comment) {
    if (comment == null || comment.trim().isEmpty()) {
        cell.removeCellComment();/*w  ww  .ja  v a 2s.  co  m*/
    } else {
        Comment c = cell.getCellComment();
        if (c == null) {
            ClientAnchor anchor = creationHelper.createClientAnchor();
            anchor.setRow1(cell.getRowIndex());
            anchor.setRow2(cell.getRowIndex() + commentHeight);
            anchor.setCol1(cell.getColumnIndex() + 1);
            anchor.setCol2(cell.getColumnIndex() + commentWidth + 1);
            anchor.setAnchorType(AnchorType.MOVE_AND_RESIZE);
            c = getDrawing().createCellComment(anchor);
            c.setVisible(false);
            if (commentAuthor != null) {
                c.setAuthor(commentAuthor);
            }
            cell.setCellComment(c);
        }
        RichTextString rts = creationHelper.createRichTextString(comment);
        c.setString(rts);
    }
}

From source file:de.jlo.talendcomp.excel.SpreadsheetReferencedCellInput.java

License:Apache License

private boolean fetchCurrentCellValue(Cell cell) {
    if (cell != null) {
        currentCell = cell;//w  ww  .j av  a 2s .  c  o m
        currentCellValueString = getStringCellValue(cell);
        Comment comment = cell.getCellComment();
        if (comment != null) {
            currentCellComment = comment.getString().getString();
            currentCellCommentAuthor = comment.getAuthor();
        }
        CellType cellType = cell.getCellTypeEnum();
        if (cellType == CellType.BLANK) {
            currentCellValueClassName = "Object";
        } else if (cellType == CellType.STRING) {
            currentCellValueClassName = "String";
            currentCellValueObject = currentCellValueString;
        } else if (cellType == CellType.BOOLEAN) {
            currentCellValueClassName = "Boolean";
            currentCellValueBool = cell.getBooleanCellValue();
            currentCellValueObject = currentCellValueBool;
        } else if (cellType == CellType.ERROR) {
            currentCellValueClassName = "Byte";
            currentCellValueObject = cell.getErrorCellValue();
        } else if (cellType == CellType.FORMULA) {
            currentCellValueClassName = "String";
            currentCellFormula = cell.getCellFormula();
            currentCellValueString = getDataFormatter().formatCellValue(cell, getFormulaEvaluator());
            currentCellValueObject = currentCellValueString;
        } else if (cellType == CellType.NUMERIC) {
            if (DateUtil.isCellDateFormatted(cell)) {
                currentCellValueClassName = "java.util.Date";
                currentCellValueDate = cell.getDateCellValue();
                currentCellValueObject = currentCellValueDate;
            } else {
                currentCellValueClassName = "Double";
                currentCellValueNumber = cell.getNumericCellValue();
                currentCellValueObject = currentCellValueNumber;
            }
        }
        currentCellBgColor = getBgColor(cell);
        currentCellFgColor = getFgColor(cell);
        return currentCellValueObject != null;
    } else {
        return false;
    }
}

From source file:gov.nih.nci.evs.app.neopl.XLStoXLSX.java

License:Open Source License

/**
 * @param args/*from w w w  .ja  v  a 2  s.c  o m*/
 * @throws InvalidFormatException
 * @throws IOException
 */

public static void run(String inputfile, String outputfile) throws IOException {
    InputStream in = new BufferedInputStream(new FileInputStream(inputfile));
    try {
        Workbook wbIn = new HSSFWorkbook(in);
        File outFn = new File(outputfile);
        if (outFn.exists()) {
            outFn.delete();
        }

        Workbook wbOut = new XSSFWorkbook();
        int sheetCnt = wbIn.getNumberOfSheets();
        for (int i = 0; i < sheetCnt; i++) {
            Sheet sIn = wbIn.getSheetAt(0);
            Sheet sOut = wbOut.createSheet(sIn.getSheetName());
            Iterator<Row> rowIt = sIn.rowIterator();
            while (rowIt.hasNext()) {
                Row rowIn = rowIt.next();
                Row rowOut = sOut.createRow(rowIn.getRowNum());

                Iterator<Cell> cellIt = rowIn.cellIterator();
                while (cellIt.hasNext()) {
                    Cell cellIn = cellIt.next();
                    Cell cellOut = rowOut.createCell(cellIn.getColumnIndex(), cellIn.getCellType());

                    switch (cellIn.getCellType()) {
                    case Cell.CELL_TYPE_BLANK:
                        break;

                    case Cell.CELL_TYPE_BOOLEAN:
                        cellOut.setCellValue(cellIn.getBooleanCellValue());
                        break;

                    case Cell.CELL_TYPE_ERROR:
                        cellOut.setCellValue(cellIn.getErrorCellValue());
                        break;

                    case Cell.CELL_TYPE_FORMULA:
                        cellOut.setCellFormula(cellIn.getCellFormula());
                        break;

                    case Cell.CELL_TYPE_NUMERIC:
                        cellOut.setCellValue(cellIn.getNumericCellValue());
                        break;

                    case Cell.CELL_TYPE_STRING:
                        cellOut.setCellValue(cellIn.getStringCellValue());
                        break;
                    }

                    {
                        CellStyle styleIn = cellIn.getCellStyle();
                        CellStyle styleOut = cellOut.getCellStyle();
                        styleOut.setDataFormat(styleIn.getDataFormat());
                    }
                    cellOut.setCellComment(cellIn.getCellComment());

                    // HSSFCellStyle cannot be cast to XSSFCellStyle
                    // cellOut.setCellStyle(cellIn.getCellStyle());
                }
            }
        }
        OutputStream out = new BufferedOutputStream(new FileOutputStream(outFn));
        try {
            wbOut.write(out);
        } finally {
            out.close();
        }
    } finally {
        in.close();
    }
}

From source file:invoiceapplication.CopyRowOriginal.java

public static void copyRow(Sheet worksheet, int sourceRowNum, int destRowNum) {
    // Get the source / new row
    Row newRow = worksheet.getRow(destRowNum);
    Row sourceRow = worksheet.getRow(sourceRowNum);

    // If the row exists in destination, push down all rows by 1 else create a new row
    if (newRow != null) {
        worksheet.shiftRows(newRow.getRowNum(), worksheet.getLastRowNum(), 1, true, true);
    } else {/*w w  w  .ja v  a 2  s .c om*/
        newRow = worksheet.createRow(destRowNum);
    }
    copyAnyMergedRegions(worksheet, sourceRow, newRow);
    // Loops through source column to add to new row
    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        //Grab a copy of the old/new cell
        Cell oldCell = sourceRow.getCell(i);
        Cell newCell = newRow.createCell(i);

        // if the old cell is null jump to next cell
        if (oldCell == null) {
            newCell = null;
            continue;
        }

        // Use old cell style
        newCell.setCellStyle(oldCell.getCellStyle());

        // If there is a cell comment, copy
        if (newCell.getCellComment() != null) {
            newCell.setCellComment(oldCell.getCellComment());
        }

        // If there is a cell hyperlink, copy
        if (oldCell.getHyperlink() != null) {
            newCell.setHyperlink(oldCell.getHyperlink());
        }

        // Set the cell data type
        newCell.setCellType(oldCell.getCellType());

        // Set the cell data value
        switch (oldCell.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            newCell.setCellValue(oldCell.getCellFormula());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getStringCellValue());
            break;
        }
    }
}

From source file:jp.ryoyamamoto.poiutils.Cells.java

License:Apache License

private static void copyCellComment(Cell source, Cell target) {
    target.setCellComment(source.getCellComment());
}

From source file:org.alanwilliamson.openbd.plugin.spreadsheet.functions.SpreadsheetGetCellComment.java

License:Open Source License

public cfData execute(cfSession _session, List<cfData> parameters) throws cfmRunTimeException {
    Collections.reverse(parameters);

    if (parameters.size() == 2) {
        throwException(_session, "please specify both a row and a column");
    }//from   w w  w.j  a  va  2s.co  m

    cfSpreadSheetData spreadsheet = (cfSpreadSheetData) parameters.get(0);
    Sheet sheet = spreadsheet.getActiveSheet();

    if (parameters.size() == 3) {
        int rowNo = parameters.get(1).getInt() - 1;
        int columnNo = parameters.get(0).getInt() - 1;

        if (rowNo < 0)
            throwException(_session, "row must be 1 or greater (" + rowNo + ")");
        if (columnNo < 0)
            throwException(_session, "column must be 1 or greater (" + columnNo + ")");

        cfStructData sd = new cfStructData();

        Row row = sheet.getRow(rowNo);
        if (row != null) {
            Cell cell = row.getCell(columnNo);
            if (cell != null) {
                Comment comment = cell.getCellComment();
                if (comment != null) {
                    sd.setData("column", new cfNumberData(columnNo));
                    sd.setData("row", new cfNumberData(rowNo));
                    sd.setData("author", new cfStringData(comment.getAuthor()));
                    sd.setData("comment", new cfStringData(comment.getString().getString()));
                }
            }
        }

        return sd;
    } else {
        cfArrayData arr = cfArrayData.createArray(1);

        Iterator<Row> rowIT = sheet.rowIterator();
        while (rowIT.hasNext()) {
            Row row = rowIT.next();

            Iterator<Cell> cellIT = row.cellIterator();
            while (cellIT.hasNext()) {
                Cell cell = cellIT.next();
                Comment comment = cell.getCellComment();
                if (comment != null) {
                    cfStructData sd = new cfStructData();
                    sd.setData("column", new cfNumberData(cell.getColumnIndex() + 1));
                    sd.setData("row", new cfNumberData(row.getRowNum() + 1));
                    sd.setData("author", new cfStringData(comment.getAuthor()));
                    sd.setData("comment", new cfStringData(comment.getString().getString()));
                    arr.addElement(sd);
                }
            }
        }

        return arr;
    }

}

From source file:org.alanwilliamson.openbd.plugin.spreadsheet.SheetUtility.java

License:Open Source License

public static void cloneCell(Cell cNew, Cell cOld) {
    cNew.setCellComment(cOld.getCellComment());
    cNew.setCellStyle(cOld.getCellStyle());
    cNew.setCellType(cOld.getCellType());

    switch (cNew.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN: {
        cNew.setCellValue(cOld.getBooleanCellValue());
        break;/* ww w .  j av  a2s .  c  o  m*/
    }
    case Cell.CELL_TYPE_NUMERIC: {
        cNew.setCellValue(cOld.getNumericCellValue());
        break;
    }
    case Cell.CELL_TYPE_STRING: {
        cNew.setCellValue(cOld.getStringCellValue());
        break;
    }
    case Cell.CELL_TYPE_ERROR: {
        cNew.setCellValue(cOld.getErrorCellValue());
        break;
    }
    case Cell.CELL_TYPE_FORMULA: {
        cNew.setCellFormula(cOld.getCellFormula());
        break;
    }
    case Cell.CELL_TYPE_BLANK: {
        cNew.setCellValue(cOld.getNumericCellValue());
        break;
    }

    }

}

From source file:org.bbreak.excella.core.util.PoiUtil.java

License:Open Source License

/**
 * ?/*from w  ww .  ja v  a 2  s .c  o  m*/
 * 
 * @param fromCell 
 * @param toCell 
 */
public static void copyCell(Cell fromCell, Cell toCell) {

    if (fromCell != null) {

        // 
        CellType cellType = fromCell.getCellTypeEnum();
        switch (cellType) {
        case BLANK:
            break;
        case FORMULA:
            String cellFormula = fromCell.getCellFormula();
            toCell.setCellFormula(cellFormula);
            break;
        case BOOLEAN:
            toCell.setCellValue(fromCell.getBooleanCellValue());
            break;
        case ERROR:
            toCell.setCellErrorValue(fromCell.getErrorCellValue());
            break;
        case NUMERIC:
            toCell.setCellValue(fromCell.getNumericCellValue());
            break;
        case STRING:
            toCell.setCellValue(fromCell.getRichStringCellValue());
            break;
        default:
        }

        // 
        if (fromCell.getCellStyle() != null
                && fromCell.getSheet().getWorkbook().equals(toCell.getSheet().getWorkbook())) {
            toCell.setCellStyle(fromCell.getCellStyle());
        }

        // 
        if (fromCell.getCellComment() != null) {
            toCell.setCellComment(fromCell.getCellComment());
        }
    }
}

From source file:org.bbreak.excella.reports.tag.ImageParamParser.java

License:Open Source License

/**
 * ????????//from  ww  w .  j a  v  a 2  s.co  m
 * 
 * @param sheet 
 * @return true?false
 */
private boolean hasComments(Sheet sheet) {
    if (sheet instanceof XSSFSheet) {
        XSSFSheet xssfSheet = (XSSFSheet) sheet;
        return xssfSheet.hasComments();
    } else if (sheet instanceof HSSFSheet) {
        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.iterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                Comment comment = cell.getCellComment();
                if (comment != null) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:org.eclipse.emfforms.internal.spreadsheet.core.renderer.EMFFormsSpreadsheetControlRenderer.java

License:Open Source License

/**
 * {@inheritDoc}/*w  w w . j  a v a2  s  .c  o m*/
 *
 * @see org.eclipse.emfforms.spi.spreadsheet.core.EMFFormsAbstractSpreadsheetRenderer#render(org.apache.poi.ss.usermodel.Workbook,
 *      org.eclipse.emf.ecp.view.spi.model.VElement, org.eclipse.emf.ecp.view.spi.context.ViewModelContext,
 *      org.eclipse.emfforms.spi.spreadsheet.core.EMFFormsSpreadsheetRenderTarget)
 */
@Override
public int render(Workbook workbook, VControl vElement, ViewModelContext viewModelContext,
        EMFFormsSpreadsheetRenderTarget renderTarget) {
    Sheet sheet = workbook.getSheet(renderTarget.getSheetName());
    if (sheet == null) {
        sheet = workbook.createSheet(renderTarget.getSheetName());
        setupSheetFormat(sheet);
    }
    Row labelRow = sheet.getRow(0);
    if (labelRow == null) {
        labelRow = sheet.createRow(0);
    }
    Row descriptionRow = sheet.getRow(1);
    if (descriptionRow == null) {
        descriptionRow = sheet.createRow(1);
    }
    Row formatRow = sheet.getRow(2);
    if (formatRow == null) {
        formatRow = sheet.createRow(2);
    }

    final CellStyle readOnly = (CellStyle) viewModelContext.getContextValue(EMFFormsCellStyleConstants.LOCKED);
    final CellStyle readOnlyWrap = (CellStyle) viewModelContext
            .getContextValue(EMFFormsCellStyleConstants.LOCKED_AND_WRAPPED);

    final Cell idCell = labelRow.getCell(0, Row.CREATE_NULL_AS_BLANK);
    idCell.setCellValue(EMFFormsIdProvider.ID_COLUMN);
    idCell.setCellStyle(readOnly);

    final Cell labelCell = labelRow.getCell(renderTarget.getColumn() + 1, Row.CREATE_NULL_AS_BLANK);
    labelCell.setCellStyle(readOnlyWrap);

    final Cell descriptionCell = descriptionRow.getCell(renderTarget.getColumn() + 1, Row.CREATE_NULL_AS_BLANK);
    descriptionCell.setCellStyle(readOnlyWrap);

    final Cell formatCell = formatRow.getCell(renderTarget.getColumn() + 1, Row.CREATE_NULL_AS_BLANK);
    formatCell.setCellStyle(readOnlyWrap);

    try {
        final EMFFormsExportTableParent exportTableParent = (EMFFormsExportTableParent) viewModelContext
                .getContextValue(EMFFormsExportTableParent.EXPORT_TABLE_PARENT);
        VDomainModelReference dmrToResolve = EcoreUtil.copy(vElement.getDomainModelReference());
        if (exportTableParent != null) {
            final VIndexDomainModelReference indexDMR = exportTableParent.getIndexDMRToExtend();
            indexDMR.setTargetDMR(dmrToResolve);

            dmrToResolve = exportTableParent.getIndexDMRToResolve();
        }

        if (labelCell.getCellComment() == null) {
            final EStructuralFeature structuralFeature = emfformsDatabinding
                    .getValueProperty(dmrToResolve, viewModelContext.getDomainModel()).getStructuralFeature();

            writeLabel(vElement, viewModelContext, labelCell, exportTableParent, dmrToResolve,
                    structuralFeature);

            final Comment comment = createComment(workbook, sheet, dmrToResolve, renderTarget.getRow(),
                    renderTarget.getColumn() + 1);
            labelCell.setCellComment(comment);

            writeDescription(viewModelContext, descriptionCell, dmrToResolve);

            writeFormatInformation(formatCell, structuralFeature);
        }
        if (viewModelContext.getDomainModel() != null) {
            writeValue(viewModelContext, renderTarget, sheet, dmrToResolve);
        }

        return 1;
    } catch (final DatabindingFailedException ex) {
        reportService.report(new EMFFormsSpreadsheetReport(ex, EMFFormsSpreadsheetReport.ERROR));
    } catch (final NoLabelFoundException ex) {
        reportService.report(new EMFFormsSpreadsheetReport(ex, EMFFormsSpreadsheetReport.ERROR));
    } catch (final IOException ex) {
        reportService.report(new EMFFormsSpreadsheetReport(ex, EMFFormsSpreadsheetReport.ERROR));
    } catch (final EMFFormsConverterException ex) {
        reportService.report(new EMFFormsSpreadsheetReport(ex, EMFFormsSpreadsheetReport.ERROR));
    }

    return 0;
}