Search in sources :

Example 6 with CommentsTable

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

the class TestXSSFReader method testComments.

public void testComments() throws Exception {
    OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("comments.xlsx");
    XSSFReader r = new XSSFReader(pkg);
    XSSFReader.SheetIterator it = (XSSFReader.SheetIterator) r.getSheetsData();
    int count = 0;
    while (it.hasNext()) {
        count++;
        InputStream inp = it.next();
        inp.close();
        if (count == 1) {
            assertNotNull(it.getSheetComments());
            CommentsTable ct = it.getSheetComments();
            assertEquals(1, ct.getNumberOfAuthors());
            assertEquals(3, ct.getNumberOfComments());
        } else {
            assertNull(it.getSheetComments());
        }
    }
    assertEquals(3, count);
}
Also used : InputStream(java.io.InputStream) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) CommentsTable(org.apache.poi.xssf.model.CommentsTable)

Example 7 with CommentsTable

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

the class XSSFExcelExtractorDecorator method buildXHTML.

/**
     * @see org.apache.poi.xssf.extractor.XSSFExcelExtractor#getText()
     */
@Override
protected void buildXHTML(XHTMLContentHandler xhtml) throws SAXException, XmlException, IOException {
    OPCPackage container = extractor.getPackage();
    ReadOnlySharedStringsTable strings;
    XSSFReader.SheetIterator iter;
    XSSFReader xssfReader;
    StylesTable styles;
    try {
        xssfReader = new XSSFReader(container);
        styles = xssfReader.getStylesTable();
        iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
        strings = new ReadOnlySharedStringsTable(container);
    } catch (InvalidFormatException e) {
        throw new XmlException(e);
    } catch (OpenXML4JException oe) {
        throw new XmlException(oe);
    }
    //temporary workaround for POI-61034
    //remove once POI 3.17-beta1 is released
    Set<String> seen = new HashSet<>();
    while (iter.hasNext()) {
        SheetTextAsHTML sheetExtractor = new SheetTextAsHTML(xhtml);
        PackagePart sheetPart = null;
        try (InputStream stream = iter.next()) {
            sheetPart = iter.getSheetPart();
            final String partName = sheetPart.getPartName().toString();
            if (seen.contains(partName)) {
                continue;
            }
            seen.add(partName);
            addDrawingHyperLinks(sheetPart);
            sheetParts.add(sheetPart);
            CommentsTable comments = iter.getSheetComments();
            // Start, and output the sheet name
            xhtml.startElement("div");
            xhtml.element("h1", iter.getSheetName());
            // Extract the main sheet contents
            xhtml.startElement("table");
            xhtml.startElement("tbody");
            processSheet(sheetExtractor, comments, styles, strings, stream);
        }
        xhtml.endElement("tbody");
        xhtml.endElement("table");
        //  do the headers before the contents)
        for (String header : sheetExtractor.headers) {
            extractHeaderFooter(header, xhtml);
        }
        for (String footer : sheetExtractor.footers) {
            extractHeaderFooter(footer, xhtml);
        }
        // Do text held in shapes, if required
        if (config.getIncludeShapeBasedContent()) {
            List<XSSFShape> shapes = iter.getShapes();
            processShapes(shapes, xhtml);
        }
        //for now dump sheet hyperlinks at bottom of page
        //consider a double-pass of the inputstream to reunite hyperlinks with cells/textboxes
        //step 1: extract hyperlink info from bottom of page
        //step 2: process as we do now, but with cached hyperlink relationship info
        extractHyperLinks(sheetPart, xhtml);
        // All done with this sheet
        xhtml.endElement("div");
    }
}
Also used : ReadOnlySharedStringsTable(org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable) InputStream(java.io.InputStream) StylesTable(org.apache.poi.xssf.model.StylesTable) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) CommentsTable(org.apache.poi.xssf.model.CommentsTable) XSSFShape(org.apache.poi.xssf.usermodel.XSSFShape) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) XmlException(org.apache.xmlbeans.XmlException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader) HashSet(java.util.HashSet)

Example 8 with CommentsTable

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

the class TestXSSFComment method author.

@Test
public void author() {
    CommentsTable sheetComments = new CommentsTable();
    CTComment ctComment = sheetComments.newComment(CellAddress.A1);
    assertEquals(1, sheetComments.getNumberOfAuthors());
    XSSFComment comment = new XSSFComment(sheetComments, ctComment, null);
    assertEquals("", comment.getAuthor());
    comment.setAuthor("Apache POI");
    assertEquals("Apache POI", comment.getAuthor());
    assertEquals(2, sheetComments.getNumberOfAuthors());
    comment.setAuthor("Apache POI");
    assertEquals(2, sheetComments.getNumberOfAuthors());
    comment.setAuthor("");
    assertEquals("", comment.getAuthor());
    assertEquals(2, sheetComments.getNumberOfAuthors());
}
Also used : CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) CommentsTable(org.apache.poi.xssf.model.CommentsTable) Test(org.junit.Test)

Example 9 with CommentsTable

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

the class TestXSSFComment method getSetRow.

@Test
public void getSetRow() {
    CommentsTable sheetComments = new CommentsTable();
    XSSFVMLDrawing vml = new XSSFVMLDrawing();
    CTComment ctComment = sheetComments.newComment(CellAddress.A1);
    CTShape vmlShape = vml.newCommentShape();
    XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
    comment.setRow(1);
    assertEquals(1, comment.getRow());
    assertEquals(1, new CellReference(ctComment.getRef()).getRow());
    assertEquals(1, vmlShape.getClientDataArray(0).getRowArray(0).intValue());
    comment.setRow(5);
    assertEquals(5, comment.getRow());
    assertEquals(5, new CellReference(ctComment.getRef()).getRow());
    assertEquals(5, vmlShape.getClientDataArray(0).getRowArray(0).intValue());
}
Also used : CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) CTShape(com.microsoft.schemas.vml.CTShape) CellReference(org.apache.poi.ss.util.CellReference) CommentsTable(org.apache.poi.xssf.model.CommentsTable) Test(org.junit.Test)

Example 10 with CommentsTable

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

the class TestXSSFSheet method setCellComment.

@Test
public void setCellComment() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet();
    XSSFDrawing dg = sheet.createDrawingPatriarch();
    XSSFComment comment = dg.createCellComment(new XSSFClientAnchor());
    Cell cell = sheet.createRow(0).createCell(0);
    CommentsTable comments = sheet.getCommentsTable(false);
    CTComments ctComments = comments.getCTComments();
    cell.setCellComment(comment);
    assertEquals("A1", ctComments.getCommentList().getCommentArray(0).getRef());
    comment.setAuthor("test A1 author");
    assertEquals("test A1 author", comments.getAuthor((int) ctComments.getCommentList().getCommentArray(0).getAuthorId()));
    workbook.close();
}
Also used : SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) CTComments(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments) CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) Cell(org.apache.poi.ss.usermodel.Cell) CommentsTable(org.apache.poi.xssf.model.CommentsTable) Test(org.junit.Test)

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