Example usage for org.apache.poi.hssf.usermodel HSSFCell getCellComment

List of usage examples for org.apache.poi.hssf.usermodel HSSFCell getCellComment

Introduction

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

Prototype

public HSSFComment getCellComment() 

Source Link

Document

Returns comment associated with this cell

Usage

From source file:citibob.reports.PoiXlsWriter.java

License:Open Source License

void copyCellFormatting(HSSFCell c0, HSSFCell c1) {
    if (c0.getCellComment() != null)
        c1.setCellComment(c0.getCellComment());
    //   c1.setCellNum(c0.getCellNum());
    if (c0.getCellStyle() != null)
        c1.setCellStyle(c0.getCellStyle());
}

From source file:citibob.reports.PoiXlsWriter.java

License:Open Source License

int replaceOneHolder(HSSFSheet sheet, int row, int col, Map<String, Object> models, String rsname) {
    // Do the replacement
    Object mod = (models.size() == 1 ? models.values().iterator().next() : models.get(rsname));
    if (mod == null)
        return NOROWCHANGE;
    if (mod instanceof TableModel)
        return replaceOneHolder(sheet, row, col, (TableModel) mod);

    // It's just a simple item; put it in
    HSSFRow row0 = sheet.getRow(row);/*from   w w  w  .j  a v  a2 s  .com*/
    HSSFCell c0 = row0.getCell((short) col);
    HSSFComment comment = c0.getCellComment();
    HSSFCellStyle style = c0.getCellStyle();
    row0.removeCell(c0);
    HSSFCell c1 = row0.createCell((short) col);
    if (comment != null)
        c1.setCellComment(comment);
    if (style != null)
        c1.setCellStyle(style);
    setValue(c1, mod);
    return NOROWCHANGE;
}

From source file:com.eryansky.core.excelTools.ExcelUtils.java

License:Apache License

public static void copyCell(HSSFWorkbook destwb, HSSFCell dest, HSSFWorkbook srcwb, HSSFCell src) {
    if (src == null) {
        dest.setCellType(HSSFCell.CELL_TYPE_BLANK);
        return;//from w  ww. j  a  va2 s  . co  m
    }

    if (src.getCellComment() != null)
        dest.setCellComment(src.getCellComment());
    if (src.getCellStyle() != null) {
        HSSFCellStyle nstyle = findStyle(src.getCellStyle(), srcwb, destwb);
        if (nstyle == null) {
            nstyle = destwb.createCellStyle();
            copyCellStyle(destwb, nstyle, srcwb, src.getCellStyle());
        }
        dest.setCellStyle(nstyle);
    }
    dest.setCellType(src.getCellType());

    switch (src.getCellType()) {
    case HSSFCell.CELL_TYPE_BLANK:

        break;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        dest.setCellValue(src.getBooleanCellValue());
        break;
    case HSSFCell.CELL_TYPE_FORMULA:
        dest.setCellFormula(src.getCellFormula());
        break;
    case HSSFCell.CELL_TYPE_ERROR:
        dest.setCellErrorValue(src.getErrorCellValue());
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        dest.setCellValue(src.getNumericCellValue());
        break;
    default:
        dest.setCellValue(new HSSFRichTextString(src.getRichStringCellValue().getString()));
        break;
    }
}

From source file:org.hil.children.service.impl.ChildrenManagerImpl.java

License:Open Source License

private static void copyRow(HSSFWorkbook workbook, HSSFSheet worksheet, int sourceRowNum,
        int destinationRowNum) {
    // Get the source / new row
    HSSFRow newRow = worksheet.getRow(destinationRowNum);
    HSSFRow sourceRow = worksheet.getRow(sourceRowNum);

    // If the row exist in destination, push down all rows by 1 else create a new row
    if (newRow != null) {
        worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
    } else {/*from   www.j  a v  a2 s . co  m*/
        newRow = worksheet.createRow(destinationRowNum);
    }

    // Loop through source columns to add to new row
    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        HSSFCell oldCell = sourceRow.getCell(i);
        HSSFCell newCell = newRow.createCell(i);

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

        // Copy style from old cell and apply to new cell
        HSSFCellStyle newCellStyle = workbook.createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());

        newCell.setCellStyle(newCellStyle);

        // 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:
            newCell.setCellValue(oldCell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_ERROR:
            newCell.setCellErrorValue(oldCell.getErrorCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            newCell.setCellFormula(oldCell.getCellFormula());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        }
    }

    // If there are are any merged regions in the source row, copy to new row
    for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
        CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
        if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
            CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(),
                    (newRow.getRowNum() + (cellRangeAddress.getFirstRow() - cellRangeAddress.getLastRow())),
                    cellRangeAddress.getFirstColumn(), cellRangeAddress.getLastColumn());
            worksheet.addMergedRegion(newCellRangeAddress);
        }
    }
}

From source file:org.modeshape.sequencer.msoffice.excel.ExcelMetadataReader.java

License:Apache License

public static ExcelMetadata instance(InputStream stream) throws IOException {
    ExcelMetadata metadata = new ExcelMetadata();
    HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(stream));

    List<ExcelSheetMetadata> sheets = new ArrayList<ExcelSheetMetadata>();

    for (int sheetInd = 0; sheetInd < wb.getNumberOfSheets(); sheetInd++) {
        ExcelSheetMetadata meta = new ExcelSheetMetadata();
        meta.setName(wb.getSheetName(sheetInd));
        sheets.add(meta);//from w  ww .  j ava 2s .c o  m

        HSSFSheet worksheet = wb.getSheetAt(sheetInd);
        int lastRowNum = worksheet.getLastRowNum();

        StringBuilder buff = new StringBuilder();
        for (int rowNum = worksheet.getFirstRowNum(); rowNum <= lastRowNum; rowNum++) {
            HSSFRow row = worksheet.getRow(rowNum);

            // Empty rows are returned as null
            if (row == null) {
                continue;
            }

            int lastCellNum = row.getLastCellNum();
            for (int cellNum = row.getFirstCellNum(); cellNum < lastCellNum; cellNum++) {
                HSSFCell cell = row.getCell(cellNum);

                // Undefined cells are returned as null
                if (cell == null) {
                    continue;
                }

                /*
                 * Builds a string of body content from all string, numeric,
                 * and formula values in the body of each worksheet.
                 * 
                 *  This code currently duplicates the POI 3.1 ExcelExtractor behavior of
                 *  combining the body text from all worksheets into a single string.
                 */
                switch (cell.getCellType()) {
                case HSSFCell.CELL_TYPE_STRING:
                    buff.append(cell.getRichStringCellValue().getString());
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    buff.append(cell.getNumericCellValue());
                    break;
                case HSSFCell.CELL_TYPE_FORMULA:
                    buff.append(cell.getCellFormula());
                    break;
                }

                HSSFComment comment = cell.getCellComment();
                if (comment != null) {
                    // Filter out row delimiter characters from comment
                    String commentText = comment.getString().getString().replace(ROW_DELIMITER_CHAR, ' ');

                    buff.append(" [");
                    buff.append(commentText);
                    buff.append(" by ");
                    buff.append(comment.getAuthor());
                    buff.append(']');
                }

                if (cellNum < lastCellNum - 1) {
                    buff.append(CELL_DELIMITER_CHAR);
                } else {
                    buff.append(ROW_DELIMITER_CHAR);
                }
            }
        }
        meta.setText(buff.toString());
    }

    metadata.setSheets(sheets);
    metadata.setMetadata(wb.getSummaryInformation());
    return metadata;
}