Example #1
0
        public void Test44916()
        {
            HSSFWorkbook wb = new HSSFWorkbook();

            NPOI.SS.UserModel.ISheet sheet = wb.CreateSheet();

            // 1. Create drawing patriarch
            IDrawing patr = sheet.CreateDrawingPatriarch();

            // 2. Try to re-get the patriarch
            IDrawing existingPatr;

            try
            {
                existingPatr = sheet.DrawingPatriarch;
            }
            catch (NullReferenceException)
            {
                throw new AssertionException("Identified bug 44916");
            }

            // 3. Use patriarch
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 600, 245, (short)1, 1, (short)1, 2);

            anchor.AnchorType = (3);
            byte[] pictureData = HSSFTestDataSamples.GetTestDataFileContent("logoKarmokar4.png");
            int    idx1        = wb.AddPicture(pictureData, PictureType.PNG);

            patr.CreatePicture(anchor, idx1);

            // 4. Try to re-use patriarch later
            existingPatr = sheet.DrawingPatriarch;
            Assert.IsNotNull(existingPatr);
        }
Example #2
0
 /**
  * Closes the sample workbook read in from filename.
  * Throws an exception if closing the workbook results in the file on disk getting modified.
  *
  * @param filename the sample workbook to read in
  * @param wb the workbook to close
  * @throws IOException
  */
 private static void AssertCloseDoesNotModifyFile(String filename, IWorkbook wb)
 {
     byte[] before = HSSFTestDataSamples.GetTestDataFileContent(filename);
     CloseOrRevert(wb);
     byte[] after = HSSFTestDataSamples.GetTestDataFileContent(filename);
     CollectionAssert.AreEqual(before, after, filename + " sample file was modified as a result of closing the workbook");
 }
Example #3
0
        public void TestCreateGeneric()
        {
            IWorkbook wb;

            // InputStream -> either
            wb = WorkbookFactory.Create(
                HSSFTestDataSamples.OpenSampleFileStream(xls)
                );
            Assert.IsNotNull(wb);
            Assert.IsTrue(wb is HSSFWorkbook);
            AssertCloseDoesNotModifyFile(xls, wb);

            wb = WorkbookFactory.Create(
                HSSFTestDataSamples.OpenSampleFileStream(xlsx)
                );
            Assert.IsNotNull(wb);
            Assert.IsTrue(wb is XSSFWorkbook);
            AssertCloseDoesNotModifyFile(xlsx, wb);

            // File -> either
            wb = WorkbookFactory.Create(
                Path.GetFullPath(testdataPath + xls)
                );
            Assert.IsNotNull(wb);
            Assert.IsTrue(wb is HSSFWorkbook);
            AssertCloseDoesNotModifyFile(xls, wb);

            wb = WorkbookFactory.Create(
                testdataPath + xlsx
                );
            Assert.IsNotNull(wb);
            Assert.IsTrue(wb is XSSFWorkbook);
            AssertCloseDoesNotModifyFile(xlsx, wb);

            // Invalid type -> exception
            byte[] before = HSSFTestDataSamples.GetTestDataFileContent(txt);
            try
            {
                Stream stream = HSSFTestDataSamples.OpenSampleFileStream(txt);
                try
                {
                    wb = WorkbookFactory.Create(stream);
                }
                finally
                {
                    stream.Close();
                }
                Assert.Fail();
            }
            catch (InvalidFormatException)
            {
                // Good
            }
            byte[] after = HSSFTestDataSamples.GetTestDataFileContent(txt);
            CollectionAssert.AreEqual(before, after, "Invalid type file was modified after trying to open the file as a spreadsheet");
        }
Example #4
0
        public void Test45829()
        {
            HSSFWorkbook wb = new HSSFWorkbook();

            NPOI.SS.UserModel.ISheet sh1 = wb.CreateSheet();
            IDrawing p1 = sh1.CreateDrawingPatriarch();

            byte[]   pictureData = HSSFTestDataSamples.GetTestDataFileContent("45829.png");
            int      idx1        = wb.AddPicture(pictureData, PictureType.PNG);
            IPicture pic         = p1.CreatePicture(new HSSFClientAnchor(), idx1);

            pic.Resize();
        }
Example #5
0
        private void verifyBug54084Unicode(IWorkbook wb)
        {
            // expected data is stored in UTF-8 in a text-file
            byte[] data     = HSSFTestDataSamples.GetTestDataFileContent("54084 - Greek - beyond BMP.txt");
            String testData = Encoding.UTF8.GetString(data).Trim();

            ISheet sheet = wb.GetSheetAt(0);
            IRow   row   = sheet.GetRow(0);
            ICell  cell  = row.GetCell(0);

            String value = cell.StringCellValue;

            //Console.WriteLine(value);

            Assert.AreEqual(testData, value, "The data in the text-file should exactly match the data that we read from the workbook");
        }
Example #6
0
        public void TestResize()
        {
            HSSFWorkbook wb = new HSSFWorkbook();

            NPOI.SS.UserModel.Sheet sh1 = wb.CreateSheet();
            Drawing p1 = sh1.CreateDrawingPatriarch();

            byte[]       pictureData = HSSFTestDataSamples.GetTestDataFileContent("logoKarmokar4.png");
            int          idx1        = wb.AddPicture(pictureData, PictureType.PNG);
            Picture      picture1    = p1.CreatePicture(new HSSFClientAnchor(), idx1);
            ClientAnchor anchor1     = picture1.GetPreferredSize();

            //assert against what would BiffViewer print if we insert the image in xls and dump the file
            Assert.AreEqual(0, anchor1.Col1);
            Assert.AreEqual(0, anchor1.Row1);
            Assert.AreEqual(1, anchor1.Col2);
            Assert.AreEqual(9, anchor1.Row2);
            Assert.AreEqual(0, anchor1.Dx1);
            Assert.AreEqual(0, anchor1.Dy1);
            Assert.AreEqual(848, anchor1.Dx2);
            Assert.AreEqual(240, anchor1.Dy2);
        }
Example #7
0
        public void TestReallyEmbedSomething()
        {
            HSSFWorkbook  wb        = new HSSFWorkbook();
            ISheet        sheet     = wb.CreateSheet();
            HSSFPatriarch patriarch = sheet.CreateDrawingPatriarch() as HSSFPatriarch;

            byte[]          pictureData = HSSFTestDataSamples.GetTestDataFileContent("logoKarmokar4.png");
            byte[]          picturePPT  = POIDataSamples.GetSlideShowInstance().ReadFile("clock.jpg");
            int             imgIdx      = wb.AddPicture(pictureData, PictureType.PNG);
            POIFSFileSystem pptPoifs    = GetSamplePPT();
            int             pptIdx      = wb.AddOlePackage(pptPoifs, "Sample-PPT", "sample.ppt", "sample.ppt");
            POIFSFileSystem xlsPoifs    = GetSampleXLS();
            int             imgPPT      = wb.AddPicture(picturePPT, PictureType.JPEG);
            int             xlsIdx      = wb.AddOlePackage(xlsPoifs, "Sample-XLS", "sample.xls", "sample.xls");
            int             txtIdx      = wb.AddOlePackage(GetSampleTXT(), "Sample-TXT", "sample.txt", "sample.txt");

            int rowoffset = 5;
            int coloffset = 5;

            ICreationHelper  ch     = wb.GetCreationHelper();
            HSSFClientAnchor anchor = (HSSFClientAnchor)ch.CreateClientAnchor();

            anchor.SetAnchor((short)(2 + coloffset), 1 + rowoffset, 0, 0, (short)(3 + coloffset), 5 + rowoffset, 0, 0);
            anchor.AnchorType = (/*setter*/ AnchorType.DontMoveAndResize);

            patriarch.CreateObjectData(anchor, pptIdx, imgPPT);

            anchor = (HSSFClientAnchor)ch.CreateClientAnchor();
            anchor.SetAnchor((short)(5 + coloffset), 1 + rowoffset, 0, 0, (short)(6 + coloffset), 5 + rowoffset, 0, 0);
            anchor.AnchorType = (/*setter*/ AnchorType.DontMoveAndResize);

            patriarch.CreateObjectData(anchor, xlsIdx, imgIdx);

            anchor = (HSSFClientAnchor)ch.CreateClientAnchor();
            anchor.SetAnchor((short)(3 + coloffset), 10 + rowoffset, 0, 0, (short)(5 + coloffset), 11 + rowoffset, 0, 0);
            anchor.AnchorType = (/*setter*/ AnchorType.DontMoveAndResize);

            patriarch.CreateObjectData(anchor, txtIdx, imgIdx);

            anchor = (HSSFClientAnchor)ch.CreateClientAnchor();
            anchor.SetAnchor((short)(1 + coloffset), -2 + rowoffset, 0, 0, (short)(7 + coloffset), 14 + rowoffset, 0, 0);
            anchor.AnchorType = (/*setter*/ AnchorType.DontMoveAndResize);

            HSSFSimpleShape circle = patriarch.CreateSimpleShape(anchor);

            circle.ShapeType = (/*setter*/ HSSFSimpleShape.OBJECT_TYPE_OVAL);
            circle.IsNoFill  = (/*setter*/ true);

            if (false)
            {
                FileStream fos = new FileStream("embed.xls", FileMode.Create);
                wb.Write(fos);
                fos.Close();
            }

            wb = HSSFTestDataSamples.WriteOutAndReadBack(wb as HSSFWorkbook);

            MemoryStream   bos   = new MemoryStream();
            HSSFObjectData od    = wb.GetAllEmbeddedObjects()[0];
            Ole10Native    ole10 = Ole10Native.CreateFromEmbeddedOleObject((DirectoryNode)od.GetDirectory());

            bos = new MemoryStream();
            pptPoifs.WriteFileSystem(bos);
            Assert.IsTrue(Arrays.Equals(ole10.DataBuffer, bos.ToArray()));

            od    = wb.GetAllEmbeddedObjects()[1];
            ole10 = Ole10Native.CreateFromEmbeddedOleObject((DirectoryNode)od.GetDirectory());
            bos   = new MemoryStream();
            xlsPoifs.WriteFileSystem(bos);
            Assert.IsTrue(Arrays.Equals(ole10.DataBuffer, bos.ToArray()));

            od    = wb.GetAllEmbeddedObjects()[2];
            ole10 = Ole10Native.CreateFromEmbeddedOleObject((DirectoryNode)od.GetDirectory());
            Assert.IsTrue(Arrays.Equals(ole10.DataBuffer, GetSampleTXT()));
        }