Search in sources :

Example 36 with NPOIFSFileSystem

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

the class TestChartTitleFormatRecord method testRecord.

@Test
public void testRecord() throws Exception {
    NPOIFSFileSystem fs = new NPOIFSFileSystem(HSSFTestDataSamples.getSampleFile("WithFormattedGraphTitle.xls"));
    // Check we can open the file via usermodel
    HSSFWorkbook hssf = new HSSFWorkbook(fs);
    // Now process it through eventusermodel, and
    //  look out for the title records
    ChartTitleFormatRecordGrabber grabber = new ChartTitleFormatRecordGrabber();
    InputStream din = fs.createDocumentInputStream("Workbook");
    HSSFRequest req = new HSSFRequest();
    req.addListenerForAllRecords(grabber);
    HSSFEventFactory factory = new HSSFEventFactory();
    factory.processEvents(req, din);
    din.close();
    // Should've found one
    assertEquals(1, grabber.chartTitleFormatRecords.size());
    // And it should be of something interesting
    ChartTitleFormatRecord r = grabber.chartTitleFormatRecords.get(0);
    assertEquals(3, r.getFormatCount());
    hssf.close();
    fs.close();
}
Also used : HSSFEventFactory(org.apache.poi.hssf.eventusermodel.HSSFEventFactory) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) InputStream(java.io.InputStream) HSSFRequest(org.apache.poi.hssf.eventusermodel.HSSFRequest) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 37 with NPOIFSFileSystem

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

the class TestXorEncryption method testUserFile.

@SuppressWarnings("static-access")
@Test
public void testUserFile() throws IOException {
    Biff8EncryptionKey.setCurrentUserPassword("abc");
    NPOIFSFileSystem fs = new NPOIFSFileSystem(samples.getSampleFile("xor-encryption-abc.xls"), true);
    HSSFWorkbook hwb = new HSSFWorkbook(fs.getRoot(), true);
    HSSFSheet sh = hwb.getSheetAt(0);
    assertEquals(1.0, sh.getRow(0).getCell(0).getNumericCellValue(), 0.0);
    assertEquals(2.0, sh.getRow(1).getCell(0).getNumericCellValue(), 0.0);
    assertEquals(3.0, sh.getRow(2).getCell(0).getNumericCellValue(), 0.0);
    hwb.close();
    fs.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 38 with NPOIFSFileSystem

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

the class TestHSSFWorkbook method testRewriteFileBug58480.

@Test
public void testRewriteFileBug58480() throws IOException {
    final File file = TempFile.createTempFile("TestHSSFWorkbook", ".xls");
    try {
        // create new workbook
        {
            final Workbook workbook = new HSSFWorkbook();
            final Sheet sheet = workbook.createSheet("foo");
            final Row row = sheet.createRow(1);
            row.createCell(1).setCellValue("bar");
            writeAndCloseWorkbook(workbook, file);
        }
        // edit the workbook
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(file, false);
            try {
                DirectoryNode root = fs.getRoot();
                final Workbook workbook = new HSSFWorkbook(root, true);
                final Sheet sheet = workbook.getSheet("foo");
                sheet.getRow(1).createCell(2).setCellValue("baz");
                writeAndCloseWorkbook(workbook, file);
            } finally {
                fs.close();
            }
        }
    } finally {
        assertTrue(file.exists());
        assertTrue(file.delete());
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) Row(org.apache.poi.ss.usermodel.Row) TempFile(org.apache.poi.util.TempFile) File(java.io.File) InternalSheet(org.apache.poi.hssf.model.InternalSheet) Sheet(org.apache.poi.ss.usermodel.Sheet) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) BaseTestWorkbook(org.apache.poi.ss.usermodel.BaseTestWorkbook) Test(org.junit.Test)

Example 39 with NPOIFSFileSystem

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

the class TestHSSFWorkbook method closeDoesNotModifyWorkbook.

@Test
public void closeDoesNotModifyWorkbook() throws IOException {
    final String filename = "SampleSS.xls";
    final File file = POIDataSamples.getSpreadSheetInstance().getFile(filename);
    Workbook wb;
    // File via POIFileStream (java.io)
    wb = new HSSFWorkbook(new POIFSFileSystem(file));
    assertCloseDoesNotModifyFile(filename, wb);
    // File via NPOIFileStream (java.nio)
    wb = new HSSFWorkbook(new NPOIFSFileSystem(file));
    assertCloseDoesNotModifyFile(filename, wb);
    // InputStream
    wb = new HSSFWorkbook(new FileInputStream(file));
    assertCloseDoesNotModifyFile(filename, wb);
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) TempFile(org.apache.poi.util.TempFile) File(java.io.File) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) BaseTestWorkbook(org.apache.poi.ss.usermodel.BaseTestWorkbook) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 40 with NPOIFSFileSystem

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

the class BaseTestSlideShowFactory method testFactoryFromNative.

@SuppressWarnings("resource")
protected static void testFactoryFromNative(String file) throws Exception {
    SlideShow<?, ?> ss;
    // from NPOIFS
    if (file.endsWith(".ppt")) {
        NPOIFSFileSystem npoifs = new NPOIFSFileSystem(fromFile(file));
        ss = SlideShowFactory.create(npoifs);
        assertNotNull(ss);
        npoifs.close();
        assertCloseDoesNotModifyFile(file, ss);
    } else // from OPCPackage
    if (file.endsWith(".pptx")) {
        // not implemented
        throw new UnsupportedOperationException("Test not implemented");
    } else {
        fail("Unexpected file extension: " + file);
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem)

Aggregations

NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)101 Test (org.junit.Test)57 File (java.io.File)35 InputStream (java.io.InputStream)26 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 MAPIMessage (org.apache.poi.hsmf.MAPIMessage)14 FileOutputStream (java.io.FileOutputStream)12 TempFile (org.apache.poi.util.TempFile)12 FileInputStream (java.io.FileInputStream)11 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)10 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)10 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)9 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)9 IOException (java.io.IOException)8 OutputStream (java.io.OutputStream)8 SummaryInformation (org.apache.poi.hpsf.SummaryInformation)7 TikaInputStream (org.apache.tika.io.TikaInputStream)6 AgileDecryptor (org.apache.poi.poifs.crypt.agile.AgileDecryptor)5 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)5