Beispiel #1
0
 public static IWorkbook Create(Stream inputStream)
 {
     inputStream = (Stream) new PushbackStream(inputStream);
     if (POIFSFileSystem.HasPOIFSHeader(inputStream))
     {
         return((IWorkbook) new HSSFWorkbook(inputStream));
     }
     inputStream.Position = 0L;
     if (POIXMLDocument.HasOOXMLHeader(inputStream))
     {
         return((IWorkbook) new XSSFWorkbook(OPCPackage.Open(inputStream)));
     }
     throw new ArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream.");
 }
Beispiel #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.");
 }
        private void ConfirmIsPOIFS(String sampleFileName, bool expectedResult)
        {
            Stream in1 = OpenSampleStream(sampleFileName);
            bool   actualResult;

            try
            {
                actualResult = POIFSFileSystem.HasPOIFSHeader(in1);
            }
            catch (IOException)
            {
                throw;
            }
            Assert.AreEqual(expectedResult, actualResult);
        }
Beispiel #4
0
        public void TestFileCorruption()
        {
            // create test InputStream
            byte[]      testData  = { (byte)1, (byte)2, (byte)3 };
            InputStream testInput = new ByteArrayInputStream(testData);

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

            Assert.IsFalse(POIFSFileSystem.HasPOIFSHeader(in1));

            // check if InputStream is still intact
            byte[] test = new byte[3];
            in1.Read(test);
            Assert.IsTrue(Arrays.Equals(testData, test));
            Assert.AreEqual(-1, in1.Read());
        }
 /// <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>
 /// <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)
 {
     // If Clearly doesn't do mark/reset, wrap up
     //if (!inp.MarkSupported())
     //{
     //    inp = new PushbackInputStream(inp, 8);
     //}
     inputStream = new PushbackStream(inputStream);
     if (POIFSFileSystem.HasPOIFSHeader(inputStream))
     {
         return(new HSSFWorkbook(inputStream));
     }
     inputStream.Position = 0;
     if (POIXMLDocument.HasOOXMLHeader(inputStream))
     {
         return(new XSSFWorkbook(OPCPackage.Open(inputStream)));
     }
     throw new ArgumentException("Your stream was neither an OLE2 stream, nor an OOXML stream.");
 }
        private void ConfirmIsPOIFS(String sampleFileName, bool expectedResult)
        {
            Stream in1 = OpenSampleStream(sampleFileName);

            try
            {
                bool actualResult;
                try
                {
                    actualResult = POIFSFileSystem.HasPOIFSHeader(in1);
                }
                catch (IOException ex)
                {
                    throw new RuntimeException(ex);
                }
                Assert.AreEqual(expectedResult, actualResult);
            }
            finally
            {
                in1.Close();
            }
        }
Beispiel #7
0
        public IWorkbook NPOIOpenExcel(string filename)
        {
            Stream excelStream = OpenResource(filename);

            if (POIFSFileSystem.HasPOIFSHeader(excelStream))
            {
                return(new HSSFWorkbook(excelStream));
            }
            if (POIXMLDocument.HasOOXMLHeader(excelStream))
            {
                return(new XSSFWorkbook(OPCPackage.Open(excelStream)));
            }
            if (filename.EndsWith(".xlsx"))
            {
                return(new XSSFWorkbook(excelStream));
            }
            if (filename.EndsWith(".xls"))
            {
                new HSSFWorkbook(excelStream);
            }
            throw new Exception("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
        }
Beispiel #8
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.");
        }
Beispiel #9
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.");
        }