Search in sources :

Example 41 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestFormatTrackingHSSFListener method processFile.

private void processFile(String filename) throws Exception {
    HSSFRequest req = new HSSFRequest();
    mockListen = new MockHSSFListener();
    listener = new FormatTrackingHSSFListener(mockListen);
    req.addListenerForAllRecords(listener);
    File file = HSSFTestDataSamples.getSampleFile(filename);
    HSSFEventFactory factory = new HSSFEventFactory();
    POIFSFileSystem fs = new POIFSFileSystem(file);
    factory.processWorkbookEvents(req, fs);
    fs.close();
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) File(java.io.File)

Example 42 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestMissingRecordAwareHSSFListener method readRecords.

private void readRecords(String sampleFileName) {
    HSSFRequest req = new HSSFRequest();
    MockHSSFListener mockListen = new MockHSSFListener();
    MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(mockListen);
    req.addListenerForAllRecords(listener);
    HSSFEventFactory factory = new HSSFEventFactory();
    try {
        InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName);
        POIFSFileSystem fs = new POIFSFileSystem(is);
        factory.processWorkbookEvents(req, fs);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    r = mockListen.getRecords();
    assertTrue(r.length > 100);
}
Also used : InputStream(java.io.InputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) IOException(java.io.IOException)

Example 43 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestExcelExtractor method createExtractor.

private static ExcelExtractor createExtractor(String sampleFileName) throws IOException {
    File file = HSSFTestDataSamples.getSampleFile(sampleFileName);
    POIFSFileSystem fs = new POIFSFileSystem(file);
    ExcelExtractor extractor = new ExcelExtractor(fs);
    extractor.setFilesystem(fs);
    return extractor;
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) File(java.io.File)

Example 44 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestExcelExtractor method testWithEmbededInOwn.

/**
	 * Excel embeded in excel
	 */
@Test
public void testWithEmbededInOwn() throws Exception {
    POIDataSamples ssSamples = POIDataSamples.getSpreadSheetInstance();
    POIFSFileSystem fs = null;
    HSSFWorkbook wbA = null, wbB = null;
    ExcelExtractor exA = null, exB = null, ex = null;
    try {
        fs = new POIFSFileSystem(ssSamples.getFile("excel_with_embeded.xls"));
        DirectoryNode dirA = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B5");
        DirectoryNode dirB = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B4");
        wbA = new HSSFWorkbook(dirA, fs, true);
        wbB = new HSSFWorkbook(dirB, fs, true);
        exA = new ExcelExtractor(wbA);
        exB = new ExcelExtractor(wbB);
        assertEquals("Sheet1\nTest excel file\nThis is the first file\nSheet2\nSheet3\n", exA.getText());
        assertEquals("Sample Excel", exA.getSummaryInformation().getTitle());
        assertEquals("Sheet1\nAnother excel file\nThis is the second file\nSheet2\nSheet3\n", exB.getText());
        assertEquals("Sample Excel 2", exB.getSummaryInformation().getTitle());
        // And the base file too
        ex = new ExcelExtractor(fs);
        assertEquals("Sheet1\nI have lots of embeded files in me\nSheet2\nSheet3\n", ex.getText());
        assertEquals("Excel With Embeded", ex.getSummaryInformation().getTitle());
    } finally {
        if (ex != null)
            ex.close();
        if (exB != null)
            exB.close();
        if (exA != null)
            exA.close();
        if (wbB != null)
            wbB.close();
        if (wbA != null)
            wbA.close();
        if (fs != null)
            fs.close();
    }
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) POIDataSamples(org.apache.poi.POIDataSamples) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 45 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestExcelExtractor method testWithEmbeded.

/**
	 * Embeded in a non-excel file
	 */
@Test
public void testWithEmbeded() throws Exception {
    POIFSFileSystem fs = null;
    HSSFWorkbook wbA = null, wbB = null;
    ExcelExtractor exA = null, exB = null;
    try {
        fs = new POIFSFileSystem(POIDataSamples.getDocumentInstance().getFile("word_with_embeded.doc"));
        DirectoryNode objPool = (DirectoryNode) fs.getRoot().getEntry("ObjectPool");
        DirectoryNode dirA = (DirectoryNode) objPool.getEntry("_1269427460");
        DirectoryNode dirB = (DirectoryNode) objPool.getEntry("_1269427461");
        wbA = new HSSFWorkbook(dirA, fs, true);
        exA = new ExcelExtractor(wbA);
        wbB = new HSSFWorkbook(dirB, fs, true);
        exB = new ExcelExtractor(wbB);
        assertEquals("Sheet1\nTest excel file\nThis is the first file\nSheet2\nSheet3\n", exA.getText());
        assertEquals("Sample Excel", exA.getSummaryInformation().getTitle());
        assertEquals("Sheet1\nAnother excel file\nThis is the second file\nSheet2\nSheet3\n", exB.getText());
        assertEquals("Sample Excel 2", exB.getSummaryInformation().getTitle());
    } finally {
        if (exB != null)
            exB.close();
        if (wbB != null)
            wbB.close();
        if (exA != null)
            exA.close();
        if (wbA != null)
            wbA.close();
        if (fs != null)
            fs.close();
    }
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Aggregations

POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)121 Test (org.junit.Test)58 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)38 InputStream (java.io.InputStream)36 ByteArrayInputStream (java.io.ByteArrayInputStream)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 FileInputStream (java.io.FileInputStream)31 File (java.io.File)25 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)15 FileOutputStream (java.io.FileOutputStream)14 OutputStream (java.io.OutputStream)14 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)13 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)13 TempFile (org.apache.poi.util.TempFile)13 IOException (java.io.IOException)12 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)7 MutableSection (org.apache.poi.hpsf.MutableSection)7 HashMap (java.util.HashMap)6 DocumentEntry (org.apache.poi.poifs.filesystem.DocumentEntry)6 NDocumentOutputStream (org.apache.poi.poifs.filesystem.NDocumentOutputStream)6