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(); }
/* * [Test] * public void TestPowerPoint() * { * POIXMLDocument doc = new XSLFSlideShow(OPCPackage.Open( * POIDataSamples.GetSlideShowInstance().OpenResourceAsStream("PPTWithAttachments.pptm")) * ); * Test(doc, 4); * }*/ private void Test(POIXMLDocument doc, int expectedCount) { Assert.IsNotNull(doc.GetAllEmbedds()); Assert.AreEqual(expectedCount, doc.GetAllEmbedds().Count); for (int i = 0; i < doc.GetAllEmbedds().Count; i++) { PackagePart pp = doc.GetAllEmbedds()[i]; Assert.IsNotNull(pp); byte[] b = IOUtils.ToByteArray(pp.GetStream(System.IO.FileMode.Open)); Assert.IsTrue(b.Length > 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."); }
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()); }
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"); }
public void TestCustomProperties() { POIXMLDocument wb1 = new XSSFWorkbook(); CustomProperties customProps = wb1.GetProperties().CustomProperties; customProps.AddProperty("test-1", "string val"); customProps.AddProperty("test-2", 1974); customProps.AddProperty("test-3", 36.6); //Adding a duplicate try { customProps.AddProperty("test-3", 36.6); Assert.Fail("expected exception"); } catch (ArgumentException e) { Assert.AreEqual("A property with this name already exists in the custom properties", e.Message); } customProps.AddProperty("test-4", true); POIXMLDocument wb2 = (XSSFWorkbook)XSSFTestDataSamples.WriteOutAndReadBack((XSSFWorkbook)wb1); wb1.Close(); CT_CustomProperties ctProps = wb2.GetProperties().CustomProperties.GetUnderlyingProperties(); Assert.AreEqual(6, ctProps.sizeOfPropertyArray()); CT_Property p; p = ctProps.GetPropertyArray(0); Assert.AreEqual("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.fmtid); Assert.AreEqual("test-1", p.name); Assert.AreEqual("string val", p.Item.ToString()); Assert.AreEqual(2, p.pid); p = ctProps.GetPropertyArray(1); Assert.AreEqual("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.fmtid); Assert.AreEqual("test-2", p.name); Assert.AreEqual(1974, p.Item); Assert.AreEqual(3, p.pid); p = ctProps.GetPropertyArray(2); Assert.AreEqual("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.fmtid); Assert.AreEqual("test-3", p.name); Assert.AreEqual(36.6, p.Item); Assert.AreEqual(4, p.pid); p = ctProps.GetPropertyArray(3); Assert.AreEqual("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.fmtid); Assert.AreEqual("test-4", p.name); Assert.AreEqual(true, p.Item); Assert.AreEqual(5, p.pid); p = ctProps.GetPropertyArray(4); Assert.AreEqual("Generator", p.name); Assert.AreEqual("NPOI", p.Item); Assert.AreEqual(6, p.pid); //p = ctProps.GetPropertyArray(5); //Assert.AreEqual("Generator Version", p.name); //Assert.AreEqual("2.0.9", p.Item); //Assert.AreEqual(7, p.pid); wb2.Close(); }
public XSSFWorkbook(string path) : this(POIXMLDocument.OpenPackage(path)) { }