Ejemplo n.º 1
0
        public void SetUp()
        {
            POIFSFileSystem filesystem = new POIFSFileSystem(
                    POIDataSamples.GetDocumentInstance().OpenResourceAsStream("test.doc"));

            DocumentEntry documentProps =
              (DocumentEntry)filesystem.Root.GetEntry("WordDocument");
            _mainStream = new byte[documentProps.Size];
            filesystem.CreateDocumentInputStream("WordDocument").Read(_mainStream);

            // use the fib to determine the name of the table stream.
            _fib = new FileInformationBlock(_mainStream);

            String name = "0Table";
            if (_fib.IsFWhichTblStm())
            {
                name = "1Table";
            }

            // read in the table stream.
            DocumentEntry tableProps =
              (DocumentEntry)filesystem.Root.GetEntry(name);
            _tableStream = new byte[tableProps.Size];
            filesystem.CreateDocumentInputStream(name).Read(_tableStream);

            _fib.FillVariableFields(_mainStream, _tableStream);
        }
        public void TestRecord()
        {
            POIFSFileSystem fs = new POIFSFileSystem(
                    HSSFTestDataSamples.OpenSampleFileStream("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();
            Stream 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
            Assert.AreEqual(1, grabber.chartTitleFormatRecords.Count);
            // And it should be of something interesting
            AlRunsRecord r =
                (AlRunsRecord)grabber.chartTitleFormatRecords[0];
            Assert.AreEqual(3, r.GetFormatCount());
        }
Ejemplo n.º 3
0
        public void TestFail()  
        {
            Stream is1 = HSSFTestDataSamples.OpenSampleFileStream("Simple.xls");
            POIFSFileSystem fs = new POIFSFileSystem(is1);

            HSSFWorkbook wb = new HSSFWorkbook(fs);

            //set POIFS properties after constructing HSSFWorkbook
            //(a piece of code that used to work up to POI 3.0.2)
            SummaryInformation summary1 = (SummaryInformation)PropertySetFactory.Create(fs.CreateDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
            summary1.Title=(title);
            //Write the modified property back to POIFS
            fs.Root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME).Delete();
            fs.CreateDocument(summary1.ToStream(), SummaryInformation.DEFAULT_STREAM_NAME);

            //save the workbook and read the property
            MemoryStream out1 = new MemoryStream();
            wb.Write(out1);
            out1.Close();

            POIFSFileSystem fs2 = new POIFSFileSystem(new MemoryStream(out1.ToArray()));
            SummaryInformation summary2 = (SummaryInformation)PropertySetFactory.Create(fs2.CreateDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));

            //Assert.Failing assertion
            Assert.AreEqual(title, summary2.Title);
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            FileStream stream = new FileStream("thumbs.db", FileMode.Open, FileAccess.Read);
            POIFSFileSystem poifs = new POIFSFileSystem(stream);
            var entries = poifs.Root.Entries;
            //POIFSDocumentReader catalogdr = poifs.CreatePOIFSDocumentReader("Catalog");
            //byte[] b1=new byte[catalogdr.Length-4];
            //catalogdr.Read(b1,4,b1.Length);
            //Dictionary<string, string> indexList = new Dictionary<string, string>();
            //for (int j = 0; j < b1.Length; j++)
            //{ 
            //    if(b1[0]
            //}

            while (entries.MoveNext())
            {
                DocumentNode entry = entries.Current as DocumentNode;
                DocumentInputStream dr = poifs.CreateDocumentInputStream(entry.Name);

                if (entry.Name.ToLower() == "catalog")
                    continue;

                byte[] buffer = new byte[dr.Length];
                dr.Read(buffer);
                int startpos = 0;

                //detect jfif header
                for (int i = 3; i < buffer.Length; i++)
                {
                    if (buffer[i - 3] == 0xFF
                        && buffer[i - 2] == 0xD8
                        && buffer[i - 1] == 0xFF
                        && buffer[i] == 0xE0)
                    {
                        startpos = i - 3;
                        break;
                    }
                }
                if (startpos == 0)
                    continue;

                FileStream jpeg = File.Create(entry.Name + ".jpeg");
                jpeg.Write(buffer, startpos, buffer.Length - startpos);
                jpeg.Close();
            }

            stream.Close();
        }
Ejemplo n.º 5
0
        /**
         * Creates an instance of this class from an embedded OLE Object. The OLE Object is expected
         * to include a stream &quot;{01}Ole10Native&quot; which Contains the actual
         * data relevant for this class.
         *
         * @param poifs POI Filesystem object
         * @return Returns an instance of this class
         * @throws IOException on IO error
         * @throws Ole10NativeException on invalid or unexcepted data format
         */
        public static Ole10Native CreateFromEmbeddedOleObject(POIFSFileSystem poifs)
        {
            bool plain = false;

            try
            {
                poifs.Root.GetEntry("\x0001Ole10ItemName");             
                plain = true;
            }
            catch (FileNotFoundException)
            {
                plain = false;
            }

            DocumentInputStream dis = poifs.CreateDocumentInputStream(OLE10_NATIVE);
            using (MemoryStream bos = new MemoryStream())
            {
                IOUtils.Copy(dis, bos);
                byte[] data = bos.ToArray();

                return new Ole10Native(data, 0, plain);
            }
        }
Ejemplo n.º 6
0
        /**
         * Creates an instance of this class from an embedded OLE Object. The OLE Object is expected
         * to include a stream &quot;{01}Ole10Native&quot; which Contains the actual
         * data relevant for this class.
         *
         * @param poifs POI Filesystem object
         * @return Returns an instance of this class
         * @throws IOException on IO error
         * @throws Ole10NativeException on invalid or unexcepted data format
         */
        public static Ole10Native CreateFromEmbeddedOleObject(POIFSFileSystem poifs)
        {
            bool plain = false;

            try
            {
                poifs.Root.GetEntry("\x0001Ole10ItemName");
                plain = true;
            }
            catch (FileNotFoundException)
            {
                plain = false;
            }

            DocumentInputStream dis = poifs.CreateDocumentInputStream(OLE10_NATIVE);

            using (MemoryStream bos = new MemoryStream())
            {
                IOUtils.Copy(dis, bos);
                byte[] data = bos.ToArray();

                return(new Ole10Native(data, 0, plain));
            }
        }
Ejemplo n.º 7
0
        public void TestOK()
        {
            Stream is1 = HSSFTestDataSamples.OpenSampleFileStream("Simple.xls");
            POIFSFileSystem fs = new POIFSFileSystem(is1);

            //set POIFS properties before constructing HSSFWorkbook
            SummaryInformation summary1 = (SummaryInformation)PropertySetFactory.Create(fs.CreateDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
            summary1.Title = (title);

            fs.Root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME).Delete();
            fs.CreateDocument(summary1.ToStream(), SummaryInformation.DEFAULT_STREAM_NAME);

            HSSFWorkbook wb = new HSSFWorkbook(fs);

            MemoryStream out1 = new MemoryStream();
            wb.Write(out1);
            out1.Close();

            //read the property
            POIFSFileSystem fs2 = new POIFSFileSystem(new MemoryStream(out1.ToArray()));
            SummaryInformation summary2 = (SummaryInformation)PropertySetFactory.Create(fs2.CreateDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
            Assert.AreEqual(title, summary2.Title);

        }
Ejemplo n.º 8
0
        public void NPOIFSReadCopyWritePOIFSRead()
        {
            FileStream testFile = POIDataSamples.GetSpreadSheetInstance().GetFile("Simple.xls");
            NPOIFSFileSystem src = new NPOIFSFileSystem(testFile);
            byte[] wbDataExp = IOUtils.ToByteArray(src.CreateDocumentInputStream("Workbook"));

            NPOIFSFileSystem nfs = new NPOIFSFileSystem();
            EntryUtils.CopyNodes(src.Root, nfs.Root);
            src.Close();

            MemoryStream bos = new MemoryStream();
            nfs.WriteFileSystem(bos);
            nfs.Close();

            POIFSFileSystem pfs = new POIFSFileSystem(new MemoryStream(bos.ToArray()));
            byte[] wbDataAct = IOUtils.ToByteArray(pfs.CreateDocumentInputStream("Workbook"));

            Assert.That(wbDataExp, new EqualConstraint(wbDataAct));
        }
Ejemplo n.º 9
0
        public void Test56138()
        {
            DocumentInputStream dis;
            POIFSFileSystem fs =
                    new POIFSFileSystem(_samples.OpenResourceAsStream("TestZeroLengthCodePage.mpp"));

            dis = fs.CreateDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME);
            SummaryInformation si = (SummaryInformation)PropertySetFactory.Create(dis);

            dis = fs.CreateDocumentInputStream(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            DocumentSummaryInformation dsi = (DocumentSummaryInformation)PropertySetFactory.Create(dis);

            // Test
            Assert.AreEqual("MSProject", si.ApplicationName);
            Assert.AreEqual("project1", si.Title);
            Assert.AreEqual("Jon Iles", si.Author);

            Assert.AreEqual("", dsi.Company);
            Assert.AreEqual(2, dsi.SectionCount);
        }
Ejemplo n.º 10
0
        public void Test54233()
        {
            DocumentInputStream dis;
            POIFSFileSystem fs =
                    new POIFSFileSystem(_samples.OpenResourceAsStream("TestNon4ByteBoundary.doc"));

            dis = fs.CreateDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME);
            SummaryInformation si = (SummaryInformation)PropertySetFactory.Create(dis);

            dis = fs.CreateDocumentInputStream(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            DocumentSummaryInformation dsi = (DocumentSummaryInformation)PropertySetFactory.Create(dis);

            // Test
            Assert.AreEqual("Microsoft Word 10.0", si.ApplicationName);
            Assert.AreEqual("", si.Title);
            Assert.AreEqual("", si.Author);
            Assert.AreEqual("Cour de Justice", dsi.Company);


            // Write out and read back, should still be valid
            POIDocument doc = new HPSFPropertiesOnlyDocument(fs);
            MemoryStream baos = new MemoryStream();
            doc.Write(baos);
            MemoryStream bais = new MemoryStream(baos.ToArray());
            doc = new HPSFPropertiesOnlyDocument(new POIFSFileSystem(bais));

            // Check properties are still there
            Assert.AreEqual("Microsoft Word 10.0", si.ApplicationName);
            Assert.AreEqual("", si.Title);
            Assert.AreEqual("", si.Author);
            Assert.AreEqual("Cour de Justice", dsi.Company);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Processes a file into essentially record events.
 /// </summary>
 /// <param name="req">an Instance of HSSFRequest which has your registered listeners</param>
 /// <param name="fs">a POIFS filesystem containing your workbook</param>
 /// <returns>numeric user-specified result code.</returns>
 public short AbortableProcessWorkbookEvents(HSSFRequest req, POIFSFileSystem fs)
 {
     Stream in1 = fs.CreateDocumentInputStream("Workbook");
     return AbortableProcessEvents(req, in1);
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Processes a file into essentially record events.
        /// </summary>
        /// <param name="req">an Instance of HSSFRequest which has your registered listeners</param>
        /// <param name="fs">a POIFS filesystem containing your workbook</param>
        public void ProcessWorkbookEvents(HSSFRequest req, POIFSFileSystem fs)
        {
            Stream in1 = fs.CreateDocumentInputStream("Workbook");

            ProcessEvents(req, in1);
        }