/// <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()方法!");
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        private static List <PicturesInfo> GetAllPictureInfos(XSSFSheet sheet, int?minRow, int?maxRow, int?minCol, int?maxCol, bool onlyInternal)
        {
            List <PicturesInfo> picturesInfoList = new List <PicturesInfo>();

            var documentPartList = sheet.GetRelations();

            foreach (var documentPart in documentPartList)
            {
                if (documentPart is XSSFDrawing)
                {
                    var drawing   = (XSSFDrawing)documentPart;
                    var shapeList = drawing.GetShapes();
                    foreach (var shape in shapeList)
                    {
                        if (shape is XSSFPicture)
                        {
                            var picture = (XSSFPicture)shape;
                            var anchor  = picture.GetPreferredSize();

                            if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal))
                            {
                                picturesInfoList.Add(new PicturesInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.Data));
                            }
                        }
                    }
                }
            }
            picturesInfoList.Sort(delegate(PicturesInfo x, PicturesInfo y)
            {
                return(x.MinRow.CompareTo(y.MinRow));
            });
            return(picturesInfoList);
        }
Пример #3
0
        /**
         * @return the list of Single Xml Cells that provide a map rule to this mapping.
         */
        public List <XSSFSingleXmlCell> GetRelatedSingleXMLCell()
        {
            List <XSSFSingleXmlCell> relatedSimpleXmlCells = new List <XSSFSingleXmlCell>();

            int sheetNumber = mapInfo.Workbook.NumberOfSheets;

            for (int i = 0; i < sheetNumber; i++)
            {
                XSSFSheet sheet = (XSSFSheet)mapInfo.Workbook.GetSheetAt(i);
                foreach (POIXMLDocumentPart p in sheet.GetRelations())
                {
                    if (p is SingleXmlCells)
                    {
                        SingleXmlCells SingleXMLCells = (SingleXmlCells)p;
                        foreach (XSSFSingleXmlCell cell in SingleXMLCells.GetAllSimpleXmlCell())
                        {
                            if (cell.GetMapId() == ctMap.ID)
                            {
                                relatedSimpleXmlCells.Add(cell);
                            }
                        }
                    }
                }
            }
            return(relatedSimpleXmlCells);
        }
Пример #4
0
        /// <summary>
        /// 删除指定区域的图片
        /// </summary>
        /// <param name="sheet">工作表</param>
        /// <param name="firstRow">起始行索引</param>
        /// <param name="lastRow">结束行索引</param>
        /// <param name="firstColumn">起始列索引</param>
        /// <param name="lastColumn">结束列索引</param>
        /// <param name="onlyInternal">是否内部区域</param>
        private static void RemovePictures(XSSFSheet sheet, int?firstRow, int?lastRow,
                                           int?firstColumn, int?lastColumn, bool onlyInternal)
        {
            throw new NotImplementedException($"XSSFSheet 未实现RemovePictures()方法!");
            var documentPartList = sheet.GetRelations();

            foreach (var documentPart in documentPartList)
            {
                if (documentPart is XSSFDrawing drawing)
                {
                    List <XSSFShape> shapes = drawing.GetShapes();
                    foreach (var shape in shapes)
                    {
                        if (shape is XSSFPicture picture)
                        {
                            IClientAnchor anchor = picture.GetPreferredSize();
                            if (IsInternalOrIntersect(firstRow, lastRow, firstColumn, lastColumn, anchor.Row1, anchor.Row2,
                                                      anchor.Col1, anchor.Row2, onlyInternal))
                            {
                            }
                        }
                    }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// 获取所有图片信息:高版本
        /// </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>
        /// <returns></returns>
        private static List <PictureInfo> GetAllPictureInfos(XSSFSheet sheet, int?firstRow, int?lastRow, int?firstCol, int?lastCol, bool onlyInternal)
        {
            List <PictureInfo> picturesInfoList = new List <PictureInfo>();

            var documentPartList = sheet.GetRelations();

            foreach (var documentPart in documentPartList)
            {
                if (documentPart is XSSFDrawing)
                {
                    var drawing   = (XSSFDrawing)documentPart;
                    var shapeList = drawing.GetShapes();
                    foreach (var shape in shapeList)
                    {
                        if (shape is XSSFPicture)
                        {
                            var picture = (XSSFPicture)shape;
                            picture.PictureData.SuggestFileExtension();
                            var anchor = picture.GetPreferredSize();

                            if (IsInternalOrIntersect(firstRow, lastRow, firstCol, lastCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal))
                            {
                                picturesInfoList.Add(new PictureInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.SuggestFileExtension(), picture.PictureData.Data));
                            }
                        }
                    }
                }
            }

            return(picturesInfoList);
        }
Пример #6
0
        private static Dictionary <int, PicturesInfo> GetAllPictureInfos(XSSFSheet sheet, int?minRow, int?maxRow, int?minCol, int?maxCol, bool onlyInternal)
        {
            Dictionary <int, PicturesInfo> picturesInfoList = new Dictionary <int, PicturesInfo>();

            var documentPartList = sheet.GetRelations();

            foreach (var documentPart in documentPartList)
            {
                if (documentPart is XSSFDrawing)
                {
                    var drawing   = (XSSFDrawing)documentPart;
                    var shapeList = drawing.GetShapes();
                    foreach (var shape in shapeList)
                    {
                        if (shape is XSSFPicture)
                        {
                            var picture = (XSSFPicture)shape;
                            var anchor  = picture.GetPreferredSize();

                            if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal) && !picturesInfoList.ContainsKey((anchor.Row1 - 1)))
                            {
                                picturesInfoList.Add(anchor.Row1 - 1, new PicturesInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.Data));
                            }
                        }
                    }
                }
            }

            return(picturesInfoList);
        }
Пример #7
0
        public void TestGetAccessors()
        {
            XSSFWorkbook wb = XSSFTestDataSamples.OpenSampleWorkbook("WithThreeCharts.xlsx");
            XSSFSheet    s1 = (XSSFSheet)wb.GetSheetAt(0);
            XSSFSheet    s2 = (XSSFSheet)wb.GetSheetAt(1);
            XSSFSheet    s3 = (XSSFSheet)wb.GetSheetAt(2);

            Assert.AreEqual(0, s1.GetRelations().Count);
            Assert.AreEqual(1, s2.GetRelations().Count);
            Assert.AreEqual(1, s3.GetRelations().Count);
        }
        /// <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>
        /// <returns></returns>
        private static List <PictureInfo> GetAllPictureInfos(XSSFSheet sheet, int?minRow, int?maxRow, int?minCol,
                                                             int?maxCol, bool onlyInternal)
        {
            var picturesInfoList = new List <PictureInfo>();

            List <POIXMLDocumentPart> documentPartList = sheet.GetRelations();

            foreach (POIXMLDocumentPart documentPart in documentPartList)
            {
                if (documentPart is XSSFDrawing)
                {
                    var drawing = (XSSFDrawing)documentPart;
                    List <XSSFShape> shapeList = drawing.GetShapes();
                    foreach (XSSFShape shape in shapeList)
                    {
                        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))
                            {
                                var picStyle = new PictureStyle
                                {
                                    AnchorDx1      = anchor.Dx1,
                                    AnchorDx2      = anchor.Dx2,
                                    AnchorDy1      = anchor.Dy1,
                                    AnchorDy2      = anchor.Dy2,
                                    IsNoFill       = picture.IsNoFill,
                                    LineStyle      = picture.LineStyle,
                                    LineStyleColor = picture.LineStyleColor,
                                    LineWidth      = picture.LineWidth,
                                    FillColor      = picture.FillColor
                                };
                                picturesInfoList.Add(new PictureInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2,
                                                                     picture.PictureData.Data, picStyle));
                            }
                        }
                    }
                }
            }

            return(picturesInfoList);
        }
Пример #9
0
        public void TestNew()
        {
            XSSFWorkbook wb    = new XSSFWorkbook();
            XSSFSheet    sheet = (XSSFSheet)wb.CreateSheet();
            //multiple calls of CreateDrawingPatriarch should return the same instance of XSSFDrawing
            XSSFDrawing dr1 = (XSSFDrawing)sheet.CreateDrawingPatriarch();
            XSSFDrawing dr2 = (XSSFDrawing)sheet.CreateDrawingPatriarch();

            Assert.AreSame(dr1, dr2);

            List <POIXMLDocumentPart> rels = sheet.GetRelations();

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

            XSSFDrawing drawing   = (XSSFDrawing)rels[0];
            String      drawingId = drawing.GetPackageRelationship().Id;

            //there should be a relation to this Drawing in the worksheet
            Assert.IsTrue(sheet.GetCTWorksheet().IsSetDrawing());
            Assert.AreEqual(drawingId, sheet.GetCTWorksheet().drawing.id);

            XSSFClientAnchor anchor = new XSSFClientAnchor();

            XSSFConnector c1 = drawing.CreateConnector(anchor);

            c1.LineWidth = 3;
            c1.LineStyle = SS.UserModel.LineStyle.DashDotSys;

            XSSFShapeGroup c2 = drawing.CreateGroup(anchor);

            XSSFSimpleShape c3 = drawing.CreateSimpleShape(anchor);

            c3.SetText(new XSSFRichTextString("Test String"));
            c3.SetFillColor(128, 128, 128);

            XSSFTextBox        c4 = (XSSFTextBox)drawing.CreateTextbox(anchor);
            XSSFRichTextString rt = new XSSFRichTextString("Test String");

            rt.ApplyFont(0, 5, wb.CreateFont());
            rt.ApplyFont(5, 6, wb.CreateFont());
            c4.SetText(rt);

            c4.IsNoFill = (true);
        }
Пример #10
0
        public void TestRead()
        {
            XSSFWorkbook wb    = XSSFTestDataSamples.OpenSampleWorkbook("WithDrawing.xlsx");
            XSSFSheet    sheet = (XSSFSheet)wb.GetSheetAt(0);
            //the sheet has one relationship and it is XSSFDrawing
            List <POIXMLDocumentPart> rels = sheet.GetRelations();

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

            XSSFDrawing drawing = (XSSFDrawing)rels[0];

            //sheet.CreateDrawingPatriarch() should return the same instance of XSSFDrawing
            Assert.AreSame(drawing, sheet.CreateDrawingPatriarch());
            String drawingId = drawing.GetPackageRelationship().Id;

            //there should be a relation to this Drawing in the worksheet
            Assert.IsTrue(sheet.GetCTWorksheet().IsSetDrawing());
            Assert.AreEqual(drawingId, sheet.GetCTWorksheet().drawing.id);
        }
Пример #11
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();
        }
Пример #12
0
        public void TestMapInfoExists()
        {
            XSSFWorkbook wb = XSSFTestDataSamples.OpenSampleWorkbook("CustomXMLMappings.xlsx");

            MapInfo        mapInfo        = null;
            SingleXmlCells SingleXMLCells = null;

            foreach (POIXMLDocumentPart p in wb.GetRelations())
            {
                if (p is MapInfo)
                {
                    mapInfo = (MapInfo)p;


                    CT_MapInfo ctMapInfo = mapInfo.GetCTMapInfo();

                    Assert.IsNotNull(ctMapInfo);

                    Assert.AreEqual(1, ctMapInfo.Schema.Count);

                    foreach (XSSFMap map in mapInfo.GetAllXSSFMaps())
                    {
                        string xmlSchema = map.GetSchema();
                        Assert.IsNotNull(xmlSchema);
                    }
                }
            }

            XSSFSheet sheet1 = (XSSFSheet)wb.GetSheetAt(0);

            foreach (POIXMLDocumentPart p in sheet1.GetRelations())
            {
                if (p is SingleXmlCells)
                {
                    SingleXMLCells = (SingleXmlCells)p;
                }
            }
            Assert.IsNotNull(mapInfo);
            Assert.IsNotNull(SingleXMLCells);
        }
Пример #13
0
        /// <summary>
        /// 获取指定区域包含图片信息的列表
        /// </summary>
        /// <param name="sheet">工作表</param>
        /// <param name="firstRow">起始行索引</param>
        /// <param name="lastRow">结束行索引</param>
        /// <param name="firstColumn">起始列索引</param>
        /// <param name="lastColumn">结束列索引</param>
        /// <param name="onlyInternal">是否内部区域</param>
        /// <returns></returns>
        private static List <PictureInfo> GetAllPictureInfos(XSSFSheet sheet, int?firstRow, int?lastRow,
                                                             int?firstColumn, int?lastColumn, bool onlyInternal)
        {
            var pictures         = new List <PictureInfo>();
            var documentPartList = sheet.GetRelations();

            foreach (var documentPart in documentPartList)
            {
                if (documentPart is XSSFDrawing drawing)
                {
                    List <XSSFShape> shapes = drawing.GetShapes();
                    foreach (var shape in shapes)
                    {
                        if (shape is XSSFPicture picture)
                        {
                            IClientAnchor anchor = picture.GetPreferredSize();
                            if (IsInternalOrIntersect(firstRow, lastRow, firstColumn, lastColumn, anchor.Row1, anchor.Row2,
                                                      anchor.Col1, anchor.Row2, onlyInternal))
                            {
                                var pictureStyle = new PictureStyle()
                                {
                                    AnchorDx1      = anchor.Dx1,
                                    AnchorDx2      = anchor.Dx2,
                                    AnchorDy1      = anchor.Dy1,
                                    AnchorDy2      = anchor.Dy2,
                                    IsNoFill       = picture.IsNoFill,
                                    LineStyleColor = picture.LineStyleColor,
                                    LineWidth      = picture.LineWidth,
                                    FillColor      = picture.FillColor,
                                };
                                pictures.Add(new PictureInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2,
                                                             picture.PictureData.Data, pictureStyle));
                            }
                        }
                    }
                }
            }
            return(pictures);
        }
        /// <summary>
        /// 获取所有图片信息列表
        /// </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 List <PictureInfo> GetAllPictureInfos(XSSFSheet sheet, int?minRow,
                                                             int?maxRow, int?minCol, int?maxCol, bool onlyInternal)
        {
            var result = new List <PictureInfo>();

            foreach (var documentPart in sheet.GetRelations())
            {
                if (documentPart is XSSFDrawing drawing)
                {
                    foreach (var shape in drawing.GetShapes())
                    {
                        var picture = (XSSFPicture)shape;
                        var anchor  = picture.GetPreferredSize();
                        if (!IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2,
                                                   anchor.Col1,
                                                   anchor.Col2, onlyInternal))
                        {
                            continue;
                        }
                        var picStyle = new PictureStyle()
                        {
                            AnchorDx1 = anchor.Dx1,
                            AnchorDx2 = anchor.Dx2,
                            AnchorDy1 = anchor.Dy1,
                            AnchorDy2 = anchor.Dy2,
                            IsNoFill  = picture.IsNoFill,
                            //LineStyle = picture.LineStyle,
                            LineStyleColor = picture.LineStyleColor,
                            LineWidth      = picture.LineWidth,
                            FillColor      = picture.FillColor,
                        };
                        result.Add(new PictureInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2,
                                                   picture.PictureData.Data, picStyle));
                    }
                }
            }
            return(result);
        }
Пример #15
0
        public void TestRead()
        {
            XSSFWorkbook wb    = XSSFTestDataSamples.OpenSampleWorkbook("WithDrawing.xlsx");
            XSSFSheet    sheet = (XSSFSheet)wb.GetSheetAt(0);
            //the sheet has one relationship and it is XSSFDrawing
            List <POIXMLDocumentPart> rels = sheet.GetRelations();

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

            XSSFDrawing drawing = (XSSFDrawing)rels[0];

            //sheet.CreateDrawingPatriarch() should return the same instance of XSSFDrawing
            Assert.AreSame(drawing, sheet.CreateDrawingPatriarch());
            String drawingId = drawing.GetPackageRelationship().Id;

            //there should be a relation to this Drawing in the worksheet
            Assert.IsTrue(sheet.GetCTWorksheet().IsSetDrawing());
            Assert.AreEqual(drawingId, sheet.GetCTWorksheet().drawing.id);


            List <XSSFShape> shapes = drawing.GetShapes();

            Assert.AreEqual(6, shapes.Count);

            Assert.IsTrue(shapes[(0)] is XSSFPicture);
            Assert.IsTrue(shapes[(1)] is XSSFPicture);
            Assert.IsTrue(shapes[(2)] is XSSFPicture);
            Assert.IsTrue(shapes[(3)] is XSSFPicture);
            Assert.IsTrue(shapes[(4)] is XSSFSimpleShape);
            Assert.IsTrue(shapes[(5)] is XSSFPicture);

            foreach (XSSFShape sh in shapes)
            {
                Assert.IsNotNull(sh.GetAnchor());
            }
            Assert.IsNotNull(XSSFTestDataSamples.WriteOutAndReadBack(wb));
        }
Пример #16
0
        /**
         * @return the list of all Tables that provide a map rule to this mapping
         */
        public List <XSSFTable> GetRelatedTables()
        {
            List <XSSFTable> tables = new List <XSSFTable>();
            int sheetNumber         = mapInfo.Workbook.NumberOfSheets;

            for (int i = 0; i < sheetNumber; i++)
            {
                XSSFSheet sheet = (XSSFSheet)mapInfo.Workbook.GetSheetAt(i);
                foreach (POIXMLDocumentPart p in sheet.GetRelations())
                {
                    if (p.GetPackageRelationship().RelationshipType.Equals(XSSFRelation.TABLE.Relation))
                    {
                        XSSFTable table = (XSSFTable)p;
                        if (table.MapsTo(ctMap.ID))
                        {
                            tables.Add(table);
                        }
                    }
                }
            }

            return(tables);
        }
Пример #17
0
        public void TestNew()
        {
            XSSFWorkbook wb    = new XSSFWorkbook();
            XSSFSheet    sheet = (XSSFSheet)wb.CreateSheet();
            //multiple calls of CreateDrawingPatriarch should return the same instance of XSSFDrawing
            XSSFDrawing dr1 = (XSSFDrawing)sheet.CreateDrawingPatriarch();
            XSSFDrawing dr2 = (XSSFDrawing)sheet.CreateDrawingPatriarch();

            Assert.AreSame(dr1, dr2);

            List <POIXMLDocumentPart> rels = sheet.GetRelations();

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

            XSSFDrawing drawing   = (XSSFDrawing)rels[0];
            String      drawingId = drawing.GetPackageRelationship().Id;

            //there should be a relation to this Drawing in the worksheet
            Assert.IsTrue(sheet.GetCTWorksheet().IsSetDrawing());
            Assert.AreEqual(drawingId, sheet.GetCTWorksheet().drawing.id);

            //XSSFClientAnchor anchor = new XSSFClientAnchor();

            XSSFConnector c1 = drawing.CreateConnector(new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 2, 2));

            c1.LineWidth = 2.5;
            c1.LineStyle = SS.UserModel.LineStyle.DashDotSys;

            XSSFShapeGroup c2 = drawing.CreateGroup(new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 5, 5));

            Assert.IsNotNull(c2);

            XSSFSimpleShape c3 = drawing.CreateSimpleShape(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));

            c3.SetText(new XSSFRichTextString("Test String"));
            c3.SetFillColor(128, 128, 128);

            XSSFTextBox        c4 = (XSSFTextBox)drawing.CreateTextbox(new XSSFClientAnchor(0, 0, 0, 0, 4, 4, 5, 6));
            XSSFRichTextString rt = new XSSFRichTextString("Test String");

            rt.ApplyFont(0, 5, wb.CreateFont());
            rt.ApplyFont(5, 6, wb.CreateFont());
            c4.SetText(rt);

            c4.IsNoFill = (true);

            Assert.AreEqual(4, drawing.GetCTDrawing().SizeOfTwoCellAnchorArray());

            List <XSSFShape> shapes = drawing.GetShapes();

            Assert.AreEqual(4, shapes.Count);
            Assert.IsTrue(shapes[(0)] is XSSFConnector);
            Assert.IsTrue(shapes[(1)] is XSSFShapeGroup);
            Assert.IsTrue(shapes[(2)] is XSSFSimpleShape);
            Assert.IsTrue(shapes[(3)] is XSSFSimpleShape);

            // Save and re-load it
            wb    = XSSFTestDataSamples.WriteOutAndReadBack(wb) as XSSFWorkbook;
            sheet = wb.GetSheetAt(0) as XSSFSheet;

            // Check
            dr1 = sheet.CreateDrawingPatriarch() as XSSFDrawing;
            CT_Drawing ctDrawing = dr1.GetCTDrawing();

            // Connector, shapes and text boxes are all two cell anchors
            Assert.AreEqual(0, ctDrawing.SizeOfAbsoluteAnchorArray());
            Assert.AreEqual(0, ctDrawing.SizeOfOneCellAnchorArray());
            Assert.AreEqual(4, ctDrawing.SizeOfTwoCellAnchorArray());

            shapes = dr1.GetShapes();
            Assert.AreEqual(4, shapes.Count);
            Assert.IsTrue(shapes[0] is XSSFConnector);
            Assert.IsTrue(shapes[1] is XSSFShapeGroup);
            Assert.IsTrue(shapes[2] is XSSFSimpleShape);
            Assert.IsTrue(shapes[3] is XSSFSimpleShape); //

            // Ensure it got the right namespaces
            //String xml = ctDrawing.ToString();
            //Assert.IsTrue(xml.Contains("xmlns:xdr=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\""));
            //Assert.IsTrue(xml.Contains("xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\""));

            Assert.IsNotNull(XSSFTestDataSamples.WriteOutAndReadBack(wb));
        }
Пример #18
0
        /// <summary>
        /// 获取所有的图片信息
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="workSpace"></param>
        /// <returns></returns>
        private static List <ColumnFile> GetAllPictureInfos(XSSFSheet sheet, WorkSpace workSpace)
        {
            List <ColumnFile> fileInfoList = new List <ColumnFile>();

            var documentPartList = sheet.GetRelations();

            foreach (var documentPart in documentPartList)
            {
                if (documentPart is XSSFDrawing)
                {
                    var drawing   = (XSSFDrawing)documentPart;
                    var shapeList = drawing.GetShapes();
                    foreach (var shape in shapeList)
                    {
                        if (shape is XSSFPicture)
                        {
                            var picture = (XSSFPicture)shape;
                            var anchor  = picture.GetAnchor();
                            //var anchor = picture.GetPreferredSize();

                            int row1 = (int)anchor.GetType().GetProperty("Row1").GetValue(anchor);
                            int row2;
                            int col1 = (int)anchor.GetType().GetProperty("Col1").GetValue(anchor);
                            int col2;
                            try
                            {
                                row2 = (int)anchor.GetType().GetProperty("Row2").GetValue(anchor);
                                col2 = (int)anchor.GetType().GetProperty("Col2").GetValue(anchor);
                            }
                            catch
                            {
                                row2 = row1; //给默认值
                                col2 = col1; //给默认值
                            }

                            if (IsInternalOrIntersect(row1, row2, col1, col2, workSpace))
                            {
                                ColumnFile entity = new ColumnFile()
                                {
                                    //图片所在开始行
                                    MinRow = row1,
                                    //图片所在结束行
                                    MaxRow = row2,
                                    // 图片所在开始列
                                    MinCol = col1,
                                    //图片所在结束列
                                    MaxCol = col2,
                                    //图片数据
                                    FileBytes = picture.PictureData.Data,
                                    //文件名称
                                    //FileName = picture.FileName,
                                    //响应类型
                                    MimeType = picture.PictureData.MimeType,
                                    //扩展名称
                                    ExtensionName = picture.PictureData.SuggestFileExtension(),
                                    //图片索引
                                    //FileIndex = picture.PictureIndex
                                };
                                fileInfoList.Add(entity);
                            }
                        }
                    }
                }
            }

            return(fileInfoList);
        }