Exemplo n.º 1
0
        private void check58325(XSSFWorkbook wb, int expectedShapes)
        {
            XSSFSheet sheet = wb.GetSheet("MetasNM001") as XSSFSheet;

            Assert.IsNotNull(sheet);
            StringBuilder str = new StringBuilder();

            str.Append("sheet " + sheet.SheetName + " - ");
            XSSFDrawing drawing = sheet.GetDrawingPatriarch();
            //drawing = ((XSSFSheet)sheet).createDrawingPatriarch();
            List <XSSFShape> shapes = drawing.GetShapes();

            str.Append("drawing.Shapes.size() = " + shapes.Count);
            IEnumerator <XSSFShape> it = shapes.GetEnumerator();

            while (it.MoveNext())
            {
                XSSFShape shape = it.Current;
                str.Append(", " + shape.ToString());
                str.Append(", Col1:" + ((XSSFClientAnchor)shape.GetAnchor()).Col1);
                str.Append(", Col2:" + ((XSSFClientAnchor)shape.GetAnchor()).Col2);
                str.Append(", Row1:" + ((XSSFClientAnchor)shape.GetAnchor()).Row1);
                str.Append(", Row2:" + ((XSSFClientAnchor)shape.GetAnchor()).Row2);
            }

            Assert.AreEqual(expectedShapes, shapes.Count,
                            "Having shapes: " + str);
        }
        /// <summary>
        ///     XSSFSheet清除指定区域的图片
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="minRow"></param>
        /// <param name="maxRow"></param>
        /// <param name="minCol"></param>
        /// <param name="maxCol"></param>
        /// <param name="onlyInternal"></param>
        private static void RemovePictures(XSSFSheet sheet, int?minRow, int?maxRow, int?minCol, int?maxCol,
                                           bool onlyInternal)
        {
            List <POIXMLDocumentPart> documentPartList = sheet.GetRelations();

            foreach (POIXMLDocumentPart documentPart in documentPartList)
            {
                if (documentPart is XSSFDrawing)
                {
                    var drawing = (XSSFDrawing)documentPart;
                    List <XSSFShape> shapeList = drawing.GetShapes();

                    for (int i = 0; i < shapeList.Count; i++)
                    {
                        XSSFShape shape = shapeList[i];
                        if (shape is XSSFPicture)
                        {
                            var           picture = (XSSFPicture)shape;
                            IClientAnchor anchor  = picture.GetPreferredSize();

                            if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2,
                                                      anchor.Col1, anchor.Col2, onlyInternal))
                            {
                                throw new NotImplementedException("XSSFSheet未实现ClearPictures()方法!");
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 図形オブジェクトの線の色を設定します。
 /// </summary>
 /// <param name="shape">図形オブジェクト。</param>
 /// <param name="color">新しく設定する色。</param>
 /// <exception cref="ArgumentNullException"><paramref name="shape"/> または <paramref name="color"/> が <c>null</c> です。</exception>
 public static void SetLineStyleColor([NotNull] this XSSFShape shape, [NotNull] IColor color)
 {
     if (shape == null)
     {
         throw new ArgumentNullException(nameof(shape));
     }
     if (color == null)
     {
         throw new ArgumentNullException(nameof(color));
     }
     shape.SetLineStyleColor(color.R, color.G, color.B);
 }
Exemplo n.º 4
0
        public void TestClone()
        {
            XSSFWorkbook wb     = XSSFTestDataSamples.OpenSampleWorkbook("WithDrawing.xlsx");
            XSSFSheet    sheet1 = wb.GetSheetAt(0) as XSSFSheet;

            XSSFSheet sheet2 = wb.CloneSheet(0) as XSSFSheet;

            //the source sheet has one relationship and it is XSSFDrawing
            List <POIXMLDocumentPart> rels1 = sheet1.GetRelations();

            Assert.AreEqual(1, rels1.Count);
            Assert.IsTrue(rels1[(0)] is XSSFDrawing);

            List <POIXMLDocumentPart> rels2 = sheet2.GetRelations();

            Assert.AreEqual(1, rels2.Count);
            Assert.IsTrue(rels2[(0)] is XSSFDrawing);

            XSSFDrawing drawing1 = (XSSFDrawing)rels1[0];
            XSSFDrawing drawing2 = (XSSFDrawing)rels2[0];

            Assert.AreNotSame(drawing1, drawing2);  // Drawing2 is a clone of Drawing1

            List <XSSFShape> shapes1 = drawing1.GetShapes();
            List <XSSFShape> shapes2 = drawing2.GetShapes();

            Assert.AreEqual(shapes1.Count, shapes2.Count);

            for (int i = 0; i < shapes1.Count; i++)
            {
                XSSFShape sh1 = (XSSFShape)shapes1[(i)];
                XSSFShape sh2 = (XSSFShape)shapes2[i];

                Assert.IsTrue(sh1.GetType() == sh2.GetType());
                Assert.AreEqual(sh1.GetShapeProperties().ToString(), sh2.GetShapeProperties().ToString());
            }

            checkRewrite(wb);
            wb.Close();
        }
        public override void Format(SheetAdapter sheetAdapter)
        {
            ISheet        currentSheet = sheetAdapter.CurrentSheet;
            List <object> shapes       = this.PictureInfo.GetShapes(currentSheet);
            bool          flag         = false;

            if (currentSheet is HSSFSheet)
            {
                flag = true;
            }
            if (shapes == null || shapes.Count <= 0)
            {
                throw new Exception(string.Format("未能获取到工作薄[{0}]指定区域的图形对象列表!", currentSheet.SheetName));
            }
            byte[]        pictureData = File.ReadAllBytes(this.PictureInfo.FilePath);
            IClientAnchor anchor;
            IDrawing      drawing;

            if (flag)
            {
                HSSFShape hSSFShape = shapes[0] as HSSFShape;
                anchor              = (hSSFShape.Anchor as IClientAnchor);
                drawing             = hSSFShape.Patriarch;
                hSSFShape.LineStyle = LineStyle.None;
            }
            else
            {
                XSSFShape xSSFShape = shapes[0] as XSSFShape;
                anchor              = (xSSFShape.GetAnchor() as IClientAnchor);
                drawing             = xSSFShape.GetDrawing();
                xSSFShape.LineStyle = LineStyle.None;
            }
            int      pictureIndex = currentSheet.Workbook.AddPicture(pictureData, this.PictureInfo.PictureType);
            IPicture picture      = drawing.CreatePicture(anchor, pictureIndex);

            if (this.PictureInfo.AutoSize)
            {
                picture.Resize();
            }
        }
Exemplo n.º 6
0
        public void TestReadAnchors()
        {
            XSSFWorkbook wb1     = new XSSFWorkbook();
            XSSFSheet    sheet   = wb1.CreateSheet() as XSSFSheet;
            XSSFDrawing  Drawing = sheet.CreateDrawingPatriarch() as XSSFDrawing;

            XSSFClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4);
            XSSFShape        shape1  = Drawing.CreateTextbox(anchor1) as XSSFShape;

            Assert.IsNotNull(shape1);

            XSSFClientAnchor anchor2 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 5);
            XSSFShape        shape2  = Drawing.CreateTextbox(anchor2) as XSSFShape;

            Assert.IsNotNull(shape2);

            int pictureIndex         = wb1.AddPicture(new byte[] { }, XSSFWorkbook.PICTURE_TYPE_PNG);
            XSSFClientAnchor anchor3 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 6);
            XSSFShape        shape3  = Drawing.CreatePicture(anchor3, pictureIndex) as XSSFShape;

            Assert.IsNotNull(shape3);

            XSSFWorkbook wb2 = XSSFTestDataSamples.WriteOutAndReadBack(wb1);

            wb1.Close();
            sheet   = wb2.GetSheetAt(0) as XSSFSheet;
            Drawing = sheet.CreateDrawingPatriarch() as XSSFDrawing;
            List <XSSFShape> shapes = Drawing.GetShapes();

            Assert.AreEqual(3, shapes.Count);
            Assert.AreEqual(shapes[0].GetAnchor(), anchor1);
            Assert.AreEqual(shapes[1].GetAnchor(), anchor2);
            Assert.AreEqual(shapes[2].GetAnchor(), anchor3);

            checkRewrite(wb2);
            wb2.Close();
        }