Search in sources :

Example 11 with CommentsTable

use of org.apache.poi.xssf.model.CommentsTable in project poi by apache.

the class XSSFEventBasedExcelExtractor method getText.

/**
    * Processes the file and returns the text
    */
public String getText() {
    try {
        ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(container, concatenatePhoneticRuns);
        XSSFReader xssfReader = new XSSFReader(container);
        StylesTable styles = xssfReader.getStylesTable();
        XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
        StringBuffer text = new StringBuffer();
        SheetTextExtractor sheetExtractor = new SheetTextExtractor();
        while (iter.hasNext()) {
            InputStream stream = iter.next();
            if (includeSheetNames) {
                text.append(iter.getSheetName());
                text.append('\n');
            }
            CommentsTable comments = includeCellComments ? iter.getSheetComments() : null;
            processSheet(sheetExtractor, styles, comments, strings, stream);
            if (includeHeadersFooters) {
                sheetExtractor.appendHeaderText(text);
            }
            sheetExtractor.appendCellText(text);
            if (includeTextBoxes) {
                processShapes(iter.getShapes(), text);
            }
            if (includeHeadersFooters) {
                sheetExtractor.appendFooterText(text);
            }
            sheetExtractor.reset();
            stream.close();
        }
        return text.toString();
    } catch (IOException e) {
        LOGGER.log(POILogger.WARN, e);
        return null;
    } catch (SAXException se) {
        LOGGER.log(POILogger.WARN, se);
        return null;
    } catch (OpenXML4JException o4je) {
        LOGGER.log(POILogger.WARN, o4je);
        return null;
    }
}
Also used : ReadOnlySharedStringsTable(org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) InputStream(java.io.InputStream) StylesTable(org.apache.poi.xssf.model.StylesTable) IOException(java.io.IOException) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader) CommentsTable(org.apache.poi.xssf.model.CommentsTable) SAXException(org.xml.sax.SAXException)

Example 12 with CommentsTable

use of org.apache.poi.xssf.model.CommentsTable in project poi by apache.

the class XSSFDrawing method createCellComment.

/**
	 * Creates a comment.
	 * @param anchor the client anchor describes how this comment is attached
	 *               to the sheet.
	 * @return the newly created comment.
	 */
@Override
public XSSFComment createCellComment(ClientAnchor anchor) {
    XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
    XSSFSheet sheet = getSheet();
    //create comments and vmlDrawing parts if they don't exist
    CommentsTable comments = sheet.getCommentsTable(true);
    XSSFVMLDrawing vml = sheet.getVMLDrawing(true);
    com.microsoft.schemas.vml.CTShape vmlShape = vml.newCommentShape();
    if (ca.isSet()) {
        // convert offsets from emus to pixels since we get a DrawingML-anchor
        // but create a VML Drawing
        int dx1Pixels = ca.getDx1() / Units.EMU_PER_PIXEL;
        int dy1Pixels = ca.getDy1() / Units.EMU_PER_PIXEL;
        int dx2Pixels = ca.getDx2() / Units.EMU_PER_PIXEL;
        int dy2Pixels = ca.getDy2() / Units.EMU_PER_PIXEL;
        String position = ca.getCol1() + ", " + dx1Pixels + ", " + ca.getRow1() + ", " + dy1Pixels + ", " + ca.getCol2() + ", " + dx2Pixels + ", " + ca.getRow2() + ", " + dy2Pixels;
        vmlShape.getClientDataArray(0).setAnchorArray(0, position);
    }
    CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
    if (comments.findCellComment(ref) != null) {
        throw new IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref);
    }
    return new XSSFComment(comments, comments.newComment(ref), vmlShape);
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) CommentsTable(org.apache.poi.xssf.model.CommentsTable)

Aggregations

CommentsTable (org.apache.poi.xssf.model.CommentsTable)12 Test (org.junit.Test)7 CTShape (com.microsoft.schemas.vml.CTShape)4 CTComment (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment)4 InputStream (java.io.InputStream)3 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)3 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)2 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)2 Cell (org.apache.poi.ss.usermodel.Cell)2 CellAddress (org.apache.poi.ss.util.CellAddress)2 CellReference (org.apache.poi.ss.util.CellReference)2 ReadOnlySharedStringsTable (org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable)2 XSSFReader (org.apache.poi.xssf.eventusermodel.XSSFReader)2 StylesTable (org.apache.poi.xssf.model.StylesTable)2 XmlException (org.apache.xmlbeans.XmlException)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)1 POIXMLException (org.apache.poi.POIXMLException)1 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)1