Search in sources :

Example 1 with XSSFComment

use of org.apache.poi.xssf.usermodel.XSSFComment in project poi by apache.

the class XSSFSheetXMLHandler method outputEmptyCellComment.

/**
    * Output an empty-cell comment.
    */
private void outputEmptyCellComment(CellAddress cellRef) {
    XSSFComment comment = commentsTable.findCellComment(cellRef);
    output.cell(cellRef.formatAsString(), null, comment);
}
Also used : XSSFComment(org.apache.poi.xssf.usermodel.XSSFComment)

Example 2 with XSSFComment

use of org.apache.poi.xssf.usermodel.XSSFComment in project poi by apache.

the class XSSFSheetXMLHandler method endElement.

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    if (uri != null && !uri.equals(NS_SPREADSHEETML)) {
        return;
    }
    String thisStr = null;
    // v => contents of a cell
    if (isTextTag(localName)) {
        vIsOpen = false;
        // Process the value contents as required, now we have it all
        switch(nextDataType) {
            case BOOLEAN:
                char first = value.charAt(0);
                thisStr = first == '0' ? "FALSE" : "TRUE";
                break;
            case ERROR:
                thisStr = "ERROR:" + value;
                break;
            case FORMULA:
                if (formulasNotResults) {
                    thisStr = formula.toString();
                } else {
                    String fv = value.toString();
                    if (this.formatString != null) {
                        try {
                            // Try to use the value as a formattable number
                            double d = Double.parseDouble(fv);
                            thisStr = formatter.formatRawCellContents(d, this.formatIndex, this.formatString);
                        } catch (NumberFormatException e) {
                            // Formula is a String result not a Numeric one
                            thisStr = fv;
                        }
                    } else {
                        // No formatting applied, just do raw value in all cases
                        thisStr = fv;
                    }
                }
                break;
            case INLINE_STRING:
                // TODO: Can these ever have formatting on them?
                XSSFRichTextString rtsi = new XSSFRichTextString(value.toString());
                thisStr = rtsi.toString();
                break;
            case SST_STRING:
                String sstIndex = value.toString();
                try {
                    int idx = Integer.parseInt(sstIndex);
                    XSSFRichTextString rtss = new XSSFRichTextString(sharedStringsTable.getEntryAt(idx));
                    thisStr = rtss.toString();
                } catch (NumberFormatException ex) {
                    logger.log(POILogger.ERROR, "Failed to parse SST index '" + sstIndex, ex);
                }
                break;
            case NUMBER:
                String n = value.toString();
                if (this.formatString != null && n.length() > 0)
                    thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString);
                else
                    thisStr = n;
                break;
            default:
                thisStr = "(TODO: Unexpected type: " + nextDataType + ")";
                break;
        }
        // Do we have a comment for this cell?
        checkForEmptyCellComments(EmptyCellCommentsCheckType.CELL);
        XSSFComment comment = commentsTable != null ? commentsTable.findCellComment(new CellAddress(cellRef)) : null;
        // Output
        output.cell(cellRef, thisStr, comment);
    } else if ("f".equals(localName)) {
        fIsOpen = false;
    } else if ("is".equals(localName)) {
        isIsOpen = false;
    } else if ("row".equals(localName)) {
        // Handle any "missing" cells which had comments attached
        checkForEmptyCellComments(EmptyCellCommentsCheckType.END_OF_ROW);
        // Finish up the row
        output.endRow(rowNum);
        // some sheets do not have rowNum set in the XML, Excel can read them so we should try to read them as well
        nextRowNum = rowNum + 1;
    } else if ("sheetData".equals(localName)) {
        // Handle any "missing" cells which had comments attached
        checkForEmptyCellComments(EmptyCellCommentsCheckType.END_OF_SHEET_DATA);
    } else if ("oddHeader".equals(localName) || "evenHeader".equals(localName) || "firstHeader".equals(localName)) {
        hfIsOpen = false;
        output.headerFooter(headerFooter.toString(), true, localName);
    } else if ("oddFooter".equals(localName) || "evenFooter".equals(localName) || "firstFooter".equals(localName)) {
        hfIsOpen = false;
        output.headerFooter(headerFooter.toString(), false, localName);
    }
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFComment(org.apache.poi.xssf.usermodel.XSSFComment)

Example 3 with XSSFComment

use of org.apache.poi.xssf.usermodel.XSSFComment in project poi by apache.

the class CommentsTable method getCellComments.

/**
     * Returns all cell comments on this sheet.
     * @return A map of each Comment in this sheet, keyed on the cell address where
     * the comment is located.
     */
public Map<CellAddress, XSSFComment> getCellComments() {
    prepareCTCommentCache();
    final TreeMap<CellAddress, XSSFComment> map = new TreeMap<CellAddress, XSSFComment>();
    for (final Entry<CellAddress, CTComment> e : commentRefs.entrySet()) {
        map.put(e.getKey(), new XSSFComment(this, e.getValue(), null));
    }
    return map;
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) TreeMap(java.util.TreeMap) XSSFComment(org.apache.poi.xssf.usermodel.XSSFComment)

Aggregations

XSSFComment (org.apache.poi.xssf.usermodel.XSSFComment)3 CellAddress (org.apache.poi.ss.util.CellAddress)2 TreeMap (java.util.TreeMap)1 XSSFRichTextString (org.apache.poi.xssf.usermodel.XSSFRichTextString)1 CTComment (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment)1