Esempio n. 1
0
        public void TestDetectAsPOIFS()
        {
            Stream in1;

            // ooxml file is
            in1 = new PushbackStream(
                HSSFTestDataSamples.OpenSampleFileStream("SampleSS.xlsx")
                );
            Assert.IsTrue(DocumentFactoryHelper.HasOOXMLHeader(in1));
            in1.Close();

            // xls file isn't
            in1 = new PushbackStream(
                HSSFTestDataSamples.OpenSampleFileStream("SampleSS.xls")
                );
            Assert.IsFalse(DocumentFactoryHelper.HasOOXMLHeader(in1));
            in1.Close();

            // text file isn't
            in1 = new PushbackStream(
                HSSFTestDataSamples.OpenSampleFileStream("SampleSS.txt")
                );
            Assert.IsFalse(DocumentFactoryHelper.HasOOXMLHeader(in1));
            in1.Close();
        }
Esempio n. 2
0
 /// <summary>
 /// Creates the appropriate HSSFWorkbook / XSSFWorkbook from
 /// the given InputStream. The Stream is wraped inside a PushbackInputStream.
 /// </summary>
 /// <param name="inputStream">Input Stream of .xls or .xlsx file</param>
 /// <param name="password"></param>
 /// <returns>IWorkbook depending on the input HSSFWorkbook or XSSFWorkbook is returned.</returns>
 // Your input stream MUST either support mark/reset, or
 //  be wrapped as a {@link PushbackInputStream}!
 public static IWorkbook Create(Stream inputStream, bool bReadonly)
 {
     inputStream = new PushbackStream(inputStream);
     if (POIFSFileSystem.HasPOIFSHeader(inputStream))
     {
         return(new HSSFWorkbook(inputStream));
     }
     inputStream.Position = 0;
     if (DocumentFactoryHelper.HasOOXMLHeader(inputStream))
     {
         return(new XSSFWorkbook(OPCPackage.Open(inputStream, bReadonly)));
     }
     throw new ArgumentException("Your stream was neither an OLE2 stream, nor an OOXML stream.");
 }
Esempio n. 3
0
        public void TestFileCorruption()
        {
            // create test InputStream
            byte[] testData = { (byte)1, (byte)2, (byte)3 };
            ByteArrayInputStream testInput = new ByteArrayInputStream(testData);

            // detect header
            InputStream in1 = new PushbackInputStream(testInput, 10);

            Assert.IsFalse(DocumentFactoryHelper.HasOOXMLHeader(in1));
            //noinspection deprecation
            Assert.IsFalse(POIXMLDocument.HasOOXMLHeader(in1));

            // check if InputStream is still intact
            byte[] test = new byte[3];
            Assert.AreEqual(3, in1.Read(test));
            Assert.IsTrue(Arrays.Equals(testData, test));
            Assert.AreEqual(-1, in1.Read());
        }
Esempio n. 4
0
        /// <summary>
        /// Creates the appropriate HSSFWorkbook / XSSFWorkbook from
        /// the given InputStream. The Stream is wraped inside a PushbackInputStream.
        /// </summary>
        /// <param name="inputStream">Input Stream of .xls or .xlsx file</param>
        /// <param name="password"></param>
        /// <returns>IWorkbook depending on the input HSSFWorkbook or XSSFWorkbook is returned.</returns>
        // Your input stream MUST either support mark/reset, or
        //  be wrapped as a {@link PushbackInputStream}!
        public static IWorkbook Create(Stream inputStream, string password)
        {
            // If Clearly doesn't do mark/reset, wrap up
            //if (!inp.MarkSupported())
            //{
            //    inp = new PushbackInputStream(inp, 8);
            //}
            inputStream = new PushbackStream(inputStream);
            // Ensure that there is at least some data there
            //byte[] header8 = IOUtils.PeekFirst8Bytes(inputStream);

            if (POIFSFileSystem.HasPOIFSHeader(inputStream))
            {
                //NPOIFSFileSystem fs = new NPOIFSFileSystem(inputStream);
                //return Create(fs, password);
                return(new HSSFWorkbook(inputStream));
            }
            inputStream.Position = 0;
            if (DocumentFactoryHelper.HasOOXMLHeader(inputStream))
            {
                return(new XSSFWorkbook(OPCPackage.Open(inputStream)));
            }
            throw new InvalidFormatException("Your stream was neither an OLE2 stream, nor an OOXML stream.");
        }
Esempio n. 5
0
 public static bool HasOOXMLHeader(Stream inp)
 {
     return(DocumentFactoryHelper.HasOOXMLHeader(inp));
 }
Esempio n. 6
0
        /// <summary>
        /// Creates the appropriate HSSFWorkbook / XSSFWorkbook from
        /// the given File, which must exist and be readable.
        /// </summary>
        /// <param name="file"></param>
        /// <param name="password"></param>
        /// <param name="readOnly"></param>
        /// <returns></returns>
        /// <remarks>
        /// Note that for Workbooks opened this way, it is not possible
        /// to explicitly close the underlying File resource.
        /// </remarks>
        public static IWorkbook Create(Stream inputStream, string password, bool readOnly)
        {
            inputStream = new PushbackStream(inputStream);

            try
            {
                if (POIFSFileSystem.HasPOIFSHeader(inputStream))
                {
                    if (DocumentFactoryHelper.GetPasswordProtected(inputStream) == DocumentFactoryHelper.OfficeProtectType.ProtectedOOXML)
                    {
                        inputStream.Position = 0;
                        POIFSFileSystem fs = new POIFSFileSystem(inputStream);

                        var decriptedStream = DocumentFactoryHelper.GetDecryptedStream(fs, password);

                        return(new XSSFWorkbook(decriptedStream));
                    }

                    inputStream.Position = 0;
                    NPOIFSFileSystem nfs = new NPOIFSFileSystem(inputStream);

                    try
                    {
                        return(Create(nfs, password));
                    }
                    finally
                    {
                        // ensure that the file-handle is closed again
                        if (nfs != null)
                        {
                            nfs.Close();
                        }
                    }
                }

                inputStream.Position = 0;
                if (DocumentFactoryHelper.HasOOXMLHeader(inputStream))
                {
                    inputStream.Position = 0;
                    OPCPackage pkg = OPCPackage.Open(inputStream, readOnly);
                    try
                    {
                        return(new XSSFWorkbook(pkg));
                    }
                    catch (IOException ioe)
                    {
                        // ensure that file handles are closed (use revert() to not re-write the file)
                        pkg.Revert();
                        //pkg.close();

                        // rethrow exception
                        throw ioe;
                    }
                    catch (Exception ioe)
                    {
                        // ensure that file handles are closed (use revert() to not re-write the file)
                        pkg.Revert();
                        //pkg.close();

                        // rethrow exception
                        throw ioe;
                    }
                }
            }
            finally
            {
                if (inputStream != null)
                {
                    inputStream.Dispose();
                }
            }

            throw new InvalidFormatException("Your stream was neither an OLE2 stream, nor an OOXML stream.");
        }