Beispiel #1
0
        public void TestDetectAsPOIFS()
        {
            Stream in1;

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

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

            // text file isn't
            in1 = new PushbackStream(
                HSSFTestDataSamples.OpenSampleFileStream("SampleSS.txt")
                );
            Assert.IsFalse(POIXMLDocument.HasOOXMLHeader(in1));
            in1.Dispose();
        }
Beispiel #2
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.");
 }
 /// <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.");
 }
Beispiel #4
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());
        }
Beispiel #5
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");
        }