Search in sources :

Example 1 with XSSFExcelExtractor

use of org.apache.poi.xssf.extractor.XSSFExcelExtractor in project poi by apache.

the class TestExtractorFactory method testPackage.

@Test
public void testPackage() throws Exception {
    // Excel
    POIXMLTextExtractor extractor = ExtractorFactory.createExtractor(OPCPackage.open(xlsx.toString(), PackageAccess.READ));
    assertTrue(extractor instanceof XSSFExcelExtractor);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(OPCPackage.open(xlsx.toString()));
    assertTrue(extractor.getText().length() > 200);
    extractor.close();
    // Word
    extractor = ExtractorFactory.createExtractor(OPCPackage.open(docx.toString()));
    assertTrue(extractor instanceof XWPFWordExtractor);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(OPCPackage.open(docx.toString()));
    assertTrue(extractor.getText().length() > 120);
    extractor.close();
    // PowerPoint
    extractor = ExtractorFactory.createExtractor(OPCPackage.open(pptx.toString()));
    assertTrue(extractor instanceof XSLFPowerPointExtractor);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(OPCPackage.open(pptx.toString()));
    assertTrue(extractor.getText().length() > 120);
    extractor.close();
    // Visio
    extractor = ExtractorFactory.createExtractor(OPCPackage.open(vsdx.toString()));
    assertTrue(extractor instanceof XDGFVisioExtractor);
    assertTrue(extractor.getText().length() > 20);
    extractor.close();
    // Text
    try {
        ExtractorFactory.createExtractor(OPCPackage.open(txt.toString()));
        fail("TestExtractorFactory.testPackage() failed on " + txt);
    } catch (UnsupportedFileFormatException e) {
    // Good
    } catch (Exception e) {
        System.out.println("TestExtractorFactory.testPackage() failed on " + txt);
        throw e;
    }
}
Also used : UnsupportedFileFormatException(org.apache.poi.UnsupportedFileFormatException) XDGFVisioExtractor(org.apache.poi.xdgf.extractor.XDGFVisioExtractor) POIXMLTextExtractor(org.apache.poi.POIXMLTextExtractor) XSLFPowerPointExtractor(org.apache.poi.xslf.extractor.XSLFPowerPointExtractor) XSSFExcelExtractor(org.apache.poi.xssf.extractor.XSSFExcelExtractor) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) OldExcelFormatException(org.apache.poi.hssf.OldExcelFormatException) UnsupportedFileFormatException(org.apache.poi.UnsupportedFileFormatException) POIXMLException(org.apache.poi.POIXMLException) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with XSSFExcelExtractor

use of org.apache.poi.xssf.extractor.XSSFExcelExtractor in project poi by apache.

the class TestExtractorFactory method testInputStream.

@Test
public void testInputStream() throws Exception {
    // Excel
    POITextExtractor extractor = ExtractorFactory.createExtractor(new FileInputStream(xls));
    assertTrue(extractor instanceof ExcelExtractor);
    assertTrue(extractor.getText().length() > 200);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(new FileInputStream(xlsx));
    assertTrue(extractor.getClass().getName(), extractor instanceof XSSFExcelExtractor);
    assertTrue(extractor.getText().length() > 200);
    // TODO Support OOXML-Strict, see bug #57699
    //        assertTrue(
    //                ExtractorFactory.createExtractor(new FileInputStream(xlsxStrict))
    //                instanceof XSSFExcelExtractor
    //        );
    //        assertTrue(
    //                ExtractorFactory.createExtractor(new FileInputStream(xlsxStrict)).getText().length() > 200
    //        );
    extractor.close();
    // Word
    extractor = ExtractorFactory.createExtractor(new FileInputStream(doc));
    assertTrue(extractor.getClass().getName(), extractor instanceof WordExtractor);
    assertTrue(extractor.getText().length() > 120);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(new FileInputStream(doc6));
    assertTrue(extractor.getClass().getName(), extractor instanceof Word6Extractor);
    assertTrue(extractor.getText().length() > 20);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(new FileInputStream(doc95));
    assertTrue(extractor.getClass().getName(), extractor instanceof Word6Extractor);
    assertTrue(extractor.getText().length() > 120);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(new FileInputStream(docx));
    assertTrue(extractor instanceof XWPFWordExtractor);
    assertTrue(extractor.getText().length() > 120);
    extractor.close();
    // PowerPoint
    extractor = ExtractorFactory.createExtractor(new FileInputStream(ppt));
    assertTrue(extractor instanceof PowerPointExtractor);
    assertTrue(extractor.getText().length() > 120);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(new FileInputStream(pptx));
    assertTrue(extractor instanceof XSLFPowerPointExtractor);
    assertTrue(extractor.getText().length() > 120);
    extractor.close();
    // Visio
    extractor = ExtractorFactory.createExtractor(new FileInputStream(vsd));
    assertTrue(extractor instanceof VisioTextExtractor);
    assertTrue(extractor.getText().length() > 50);
    extractor.close();
    // Visio - vsdx
    extractor = ExtractorFactory.createExtractor(new FileInputStream(vsdx));
    assertTrue(extractor instanceof XDGFVisioExtractor);
    assertTrue(extractor.getText().length() > 20);
    extractor.close();
    // Publisher
    extractor = ExtractorFactory.createExtractor(new FileInputStream(pub));
    assertTrue(extractor instanceof PublisherTextExtractor);
    assertTrue(extractor.getText().length() > 50);
    extractor.close();
    // Outlook msg
    extractor = ExtractorFactory.createExtractor(new FileInputStream(msg));
    assertTrue(extractor instanceof OutlookTextExtactor);
    assertTrue(extractor.getText().length() > 50);
    extractor.close();
    // Text
    try {
        FileInputStream stream = new FileInputStream(txt);
        try {
            ExtractorFactory.createExtractor(stream);
            fail();
        } finally {
            IOUtils.closeQuietly(stream);
        }
    } catch (IllegalArgumentException e) {
    // Good
    }
}
Also used : XDGFVisioExtractor(org.apache.poi.xdgf.extractor.XDGFVisioExtractor) XSSFExcelExtractor(org.apache.poi.xssf.extractor.XSSFExcelExtractor) Word6Extractor(org.apache.poi.hwpf.extractor.Word6Extractor) PowerPointExtractor(org.apache.poi.hslf.extractor.PowerPointExtractor) XSLFPowerPointExtractor(org.apache.poi.xslf.extractor.XSLFPowerPointExtractor) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) PublisherTextExtractor(org.apache.poi.hpbf.extractor.PublisherTextExtractor) FileInputStream(java.io.FileInputStream) WordExtractor(org.apache.poi.hwpf.extractor.WordExtractor) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) OutlookTextExtactor(org.apache.poi.hsmf.extractor.OutlookTextExtactor) XSLFPowerPointExtractor(org.apache.poi.xslf.extractor.XSLFPowerPointExtractor) POITextExtractor(org.apache.poi.POITextExtractor) XSSFExcelExtractor(org.apache.poi.xssf.extractor.XSSFExcelExtractor) ExcelExtractor(org.apache.poi.hssf.extractor.ExcelExtractor) XSSFEventBasedExcelExtractor(org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor) EventBasedExcelExtractor(org.apache.poi.hssf.extractor.EventBasedExcelExtractor) VisioTextExtractor(org.apache.poi.hdgf.extractor.VisioTextExtractor) Test(org.junit.Test)

Example 3 with XSSFExcelExtractor

use of org.apache.poi.xssf.extractor.XSSFExcelExtractor in project poi by apache.

the class TestXMLPropertiesTextExtractor method testGetFromMainExtractor.

public void testGetFromMainExtractor() throws Exception {
    OPCPackage pkg = PackageHelper.open(_ssSamples.openResourceAsStream("ExcelWithAttachments.xlsm"));
    XSSFWorkbook wb = new XSSFWorkbook(pkg);
    XSSFExcelExtractor ext = new XSSFExcelExtractor(wb);
    POIXMLPropertiesTextExtractor textExt = ext.getMetadataTextExtractor();
    // Check basics
    assertNotNull(textExt);
    assertTrue(textExt.getText().length() > 0);
    // Check some of the content
    String text = textExt.getText();
    String cText = textExt.getCorePropertiesText();
    assertContains(text, "LastModifiedBy = Yury Batrakov");
    assertContains(cText, "LastModifiedBy = Yury Batrakov");
    textExt.close();
    ext.close();
}
Also used : XSSFExcelExtractor(org.apache.poi.xssf.extractor.XSSFExcelExtractor) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 4 with XSSFExcelExtractor

use of org.apache.poi.xssf.extractor.XSSFExcelExtractor in project Gargoyle by callakrsos.

the class DocxFileParser method docxFileContentParser.

public String docxFileContentParser(String fileName) {
    try {
        FileInputStream fs = new FileInputStream(new File(fileName));
        OPCPackage d = OPCPackage.open(fs);
        if (fileName.endsWith(".docx")) {
            XWPFWordExtractor xw = new XWPFWordExtractor(d);
            return xw.getText();
        } else if (fileName.endsWith(".pptx")) {
            XSLFPowerPointExtractor xp = new XSLFPowerPointExtractor(d);
            return xp.getText();
        } else if (fileName.endsWith(".xlsx")) {
            XSSFExcelExtractor xe = new XSSFExcelExtractor(d);
            xe.setFormulasNotResults(true);
            xe.setIncludeSheetNames(true);
            return xe.getText();
        }
    } catch (Exception e) {
        System.out.println("# DocxFileParser Error :" + e.getMessage());
    }
    return "";
}
Also used : XSLFPowerPointExtractor(org.apache.poi.xslf.extractor.XSLFPowerPointExtractor) XSSFExcelExtractor(org.apache.poi.xssf.extractor.XSSFExcelExtractor) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) FileInputStream(java.io.FileInputStream)

Example 5 with XSSFExcelExtractor

use of org.apache.poi.xssf.extractor.XSSFExcelExtractor in project poi by apache.

the class ExtractorFactory method createExtractor.

/**
     * Tries to determine the actual type of file and produces a matching text-extractor for it.
     *
     * @param pkg An {@link OPCPackage}.
     * @return A {@link POIXMLTextExtractor} for the given file.
     * @throws IOException If an error occurs while reading the file 
     * @throws OpenXML4JException If an error parsing the OpenXML file format is found. 
     * @throws XmlException If an XML parsing error occurs.
     * @throws IllegalArgumentException If no matching file type could be found.
     */
public static POIXMLTextExtractor createExtractor(OPCPackage pkg) throws IOException, OpenXML4JException, XmlException {
    try {
        // Check for the normal Office core document
        PackageRelationshipCollection core;
        core = pkg.getRelationshipsByType(CORE_DOCUMENT_REL);
        // If nothing was found, try some of the other OOXML-based core types
        if (core.size() == 0) {
            // Could it be an OOXML-Strict one?
            core = pkg.getRelationshipsByType(STRICT_DOCUMENT_REL);
        }
        if (core.size() == 0) {
            // Could it be a visio one?
            core = pkg.getRelationshipsByType(VISIO_DOCUMENT_REL);
            if (core.size() == 1)
                return new XDGFVisioExtractor(pkg);
        }
        // Should just be a single core document, complain if not
        if (core.size() != 1) {
            throw new IllegalArgumentException("Invalid OOXML Package received - expected 1 core document, found " + core.size());
        }
        // Grab the core document part, and try to identify from that
        final PackagePart corePart = pkg.getPart(core.getRelationship(0));
        final String contentType = corePart.getContentType();
        // Is it XSSF?
        for (XSSFRelation rel : XSSFExcelExtractor.SUPPORTED_TYPES) {
            if (rel.getContentType().equals(contentType)) {
                if (getPreferEventExtractor()) {
                    return new XSSFEventBasedExcelExtractor(pkg);
                }
                return new XSSFExcelExtractor(pkg);
            }
        }
        // Is it XWPF?
        for (XWPFRelation rel : XWPFWordExtractor.SUPPORTED_TYPES) {
            if (rel.getContentType().equals(contentType)) {
                return new XWPFWordExtractor(pkg);
            }
        }
        // Is it XSLF?
        for (XSLFRelation rel : XSLFPowerPointExtractor.SUPPORTED_TYPES) {
            if (rel.getContentType().equals(contentType)) {
                return new XSLFPowerPointExtractor(pkg);
            }
        }
        // special handling for SlideShow-Theme-files, 
        if (XSLFRelation.THEME_MANAGER.getContentType().equals(contentType)) {
            return new XSLFPowerPointExtractor(new XSLFSlideShow(pkg));
        }
        // How about xlsb?
        for (XSSFRelation rel : XSSFBEventBasedExcelExtractor.SUPPORTED_TYPES) {
            if (rel.getContentType().equals(contentType)) {
                return new XSSFBEventBasedExcelExtractor(pkg);
            }
        }
        throw new IllegalArgumentException("No supported documents found in the OOXML package (found " + contentType + ")");
    } catch (IOException e) {
        // ensure that we close the package again if there is an error opening it, however
        // we need to revert the package to not re-write the file via close(), which is very likely not wanted for a TextExtractor!
        pkg.revert();
        throw e;
    } catch (OpenXML4JException e) {
        // ensure that we close the package again if there is an error opening it, however
        // we need to revert the package to not re-write the file via close(), which is very likely not wanted for a TextExtractor!
        pkg.revert();
        throw e;
    } catch (XmlException e) {
        // ensure that we close the package again if there is an error opening it, however
        // we need to revert the package to not re-write the file via close(), which is very likely not wanted for a TextExtractor!
        pkg.revert();
        throw e;
    } catch (RuntimeException e) {
        // ensure that we close the package again if there is an error opening it, however
        // we need to revert the package to not re-write the file via close(), which is very likely not wanted for a TextExtractor!
        pkg.revert();
        throw e;
    }
}
Also used : XSSFRelation(org.apache.poi.xssf.usermodel.XSSFRelation) XDGFVisioExtractor(org.apache.poi.xdgf.extractor.XDGFVisioExtractor) XSSFBEventBasedExcelExtractor(org.apache.poi.xssf.extractor.XSSFBEventBasedExcelExtractor) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) XSSFExcelExtractor(org.apache.poi.xssf.extractor.XSSFExcelExtractor) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) IOException(java.io.IOException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) XSLFSlideShow(org.apache.poi.xslf.usermodel.XSLFSlideShow) XWPFRelation(org.apache.poi.xwpf.usermodel.XWPFRelation) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) XSSFEventBasedExcelExtractor(org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor) XSLFPowerPointExtractor(org.apache.poi.xslf.extractor.XSLFPowerPointExtractor) XmlException(org.apache.xmlbeans.XmlException) XSLFRelation(org.apache.poi.xslf.usermodel.XSLFRelation)

Aggregations

XSSFExcelExtractor (org.apache.poi.xssf.extractor.XSSFExcelExtractor)7 XSLFPowerPointExtractor (org.apache.poi.xslf.extractor.XSLFPowerPointExtractor)5 XWPFWordExtractor (org.apache.poi.xwpf.extractor.XWPFWordExtractor)5 XDGFVisioExtractor (org.apache.poi.xdgf.extractor.XDGFVisioExtractor)4 XSSFEventBasedExcelExtractor (org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor)4 Test (org.junit.Test)4 FileInputStream (java.io.FileInputStream)3 POITextExtractor (org.apache.poi.POITextExtractor)3 EventBasedExcelExtractor (org.apache.poi.hssf.extractor.EventBasedExcelExtractor)3 ExcelExtractor (org.apache.poi.hssf.extractor.ExcelExtractor)3 IOException (java.io.IOException)2 POIXMLException (org.apache.poi.POIXMLException)2 VisioTextExtractor (org.apache.poi.hdgf.extractor.VisioTextExtractor)2 PublisherTextExtractor (org.apache.poi.hpbf.extractor.PublisherTextExtractor)2 PowerPointExtractor (org.apache.poi.hslf.extractor.PowerPointExtractor)2 OutlookTextExtactor (org.apache.poi.hsmf.extractor.OutlookTextExtactor)2 Word6Extractor (org.apache.poi.hwpf.extractor.Word6Extractor)2 WordExtractor (org.apache.poi.hwpf.extractor.WordExtractor)2 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)2 File (java.io.File)1