public List <object> GetShapes(ISheet sheet) { List <object> list = new List <object>(); IDrawing drawingPatriarch = sheet.DrawingPatriarch; if (sheet is HSSFSheet) { HSSFShapeContainer hSSFShapeContainer = sheet.DrawingPatriarch as HSSFShapeContainer; if (drawingPatriarch == null) { return(list); } IList <HSSFShape> children = hSSFShapeContainer.Children; using (IEnumerator <HSSFShape> enumerator = children.GetEnumerator()) { while (enumerator.MoveNext()) { HSSFShape current = enumerator.Current; if (current != null && current.Anchor is HSSFClientAnchor) { HSSFClientAnchor hSSFClientAnchor = current.Anchor as HSSFClientAnchor; if (this.IsInternalOrIntersect(this.ShapeRange.MinRow, this.ShapeRange.MaxRow, this.ShapeRange.MinColumn, this.ShapeRange.MaxColumn, hSSFClientAnchor.Row1, hSSFClientAnchor.Row2, hSSFClientAnchor.Col1, hSSFClientAnchor.Col2, true)) { list.Add(current); } } } return(list); } } List <POIXMLDocumentPart> relations = (sheet as XSSFSheet).GetRelations(); foreach (POIXMLDocumentPart current2 in relations) { if (current2 is XSSFDrawing) { XSSFDrawing xSSFDrawing = (XSSFDrawing)current2; List <XSSFShape> shapes = xSSFDrawing.GetShapes(); foreach (XSSFShape current3 in shapes) { XSSFAnchor anchor = current3.GetAnchor(); if (current3 != null && anchor is XSSFClientAnchor) { XSSFClientAnchor xSSFClientAnchor = anchor as XSSFClientAnchor; if (this.IsInternalOrIntersect(this.ShapeRange.MinRow, this.ShapeRange.MaxRow, this.ShapeRange.MinColumn, this.ShapeRange.MaxColumn, xSSFClientAnchor.Row1, xSSFClientAnchor.Row2, xSSFClientAnchor.Col1, xSSFClientAnchor.Col2, true)) { list.Add(current3); } } } } } return(list); }
/// <summary> /// 获取指定sheet的图片 /// </summary> /// <param name="sheetName"></param> /// <returns></returns> public IList <PictureInfo> GetPictures(string sheetName) { ISheet sheet = this._workbook.GetSheet(sheetName); IDrawing a = sheet.DrawingPatriarch; IList <PictureInfo> result = new List <PictureInfo>(); if (sheet == null) { throw new Exception($"名为{sheetName}的sheet不存在"); } if (sheet is XSSFSheet) { List <POIXMLDocumentPart> documentParts = (sheet as XSSFSheet).GetRelations(); foreach (var part in documentParts) { if (part is XSSFDrawing) { List <XSSFShape> shapes = (part as XSSFDrawing).GetShapes(); foreach (var shap in shapes) { if (shap is XSSFPicture) { XSSFPicture picture = shap as XSSFPicture; IClientAnchor anchor = picture.ClientAnchor; result.Add(new PictureInfo(anchor.Row1, anchor.Row1, anchor.Col1, anchor.Col1, picture.PictureData.Data)); } } } } } else if (sheet is HSSFSheet) { HSSFShapeContainer shanpContainer = sheet.DrawingPatriarch as HSSFShapeContainer; if (shanpContainer != null) { foreach (HSSFShape shape in shanpContainer.Children ?? new List <HSSFShape>()) { if (shape is HSSFPicture && shape.Anchor is HSSFClientAnchor) { HSSFPicture picture = shape as HSSFPicture; HSSFClientAnchor anchor = shape.Anchor as HSSFClientAnchor; result.Add(new PictureInfo(anchor.Row1, anchor.Row1, anchor.Col1, anchor.Col1, picture.PictureData.Data)); } } } } return(result); }
private void ConvertShapes(HSSFShapeContainer parent, EscherContainerRecord escherParent, Hashtable shapeToObj) { if (escherParent == null) { throw new ArgumentException("Parent record required"); } IList shapes = parent.Children; for (IEnumerator iterator = shapes.GetEnumerator(); iterator.MoveNext();) { HSSFShape shape = (HSSFShape)iterator.Current; if (shape is HSSFShapeGroup) { ConvertGroup((HSSFShapeGroup)shape, escherParent, shapeToObj); } else { AbstractShape shapeModel = AbstractShape.CreateShape( shape, drawingManager.AllocateShapeId(drawingGroupId)); shapeToObj[FindClientData(shapeModel.SpContainer)] = shapeModel.ObjRecord; if (shapeModel is TextboxShape) { EscherRecord escherTextbox = ((TextboxShape)shapeModel).EscherTextbox; shapeToObj[escherTextbox] = ((TextboxShape)shapeModel).TextObjectRecord; // escherParent.AddChildRecord(escherTextbox); if (shapeModel is CommentShape) { CommentShape comment = (CommentShape)shapeModel; tailRec.Add(comment.NoteRecord); } } escherParent.AddChildRecord(shapeModel.SpContainer); } } // drawingManager.newCluster( (short)1 ); // drawingManager.newCluster( (short)2 ); }
/// <summary> /// 删除单元格的图片 /// </summary> /// <param name="row"></param> /// <param name="col"></param> /// <param name="sheetName"></param> public void RemovePictureInCell(int row, int col, string sheetName) { ISheet sheet = this._workbook.GetSheet(sheetName); if (sheet == null) { throw new Exception($"名为{sheetName}的sheet不存在"); } if (sheet is HSSFSheet) { HSSFShapeContainer shanpContainer = sheet.DrawingPatriarch as HSSFShapeContainer; List <HSSFShape> indexes = new List <HSSFShape>(); if (shanpContainer != null) { foreach (HSSFShape shape in shanpContainer.Children ?? new List <HSSFShape>()) { if (shape is HSSFPicture && shape.Anchor is HSSFClientAnchor) { HSSFClientAnchor anchor = shape.Anchor as HSSFClientAnchor; if (anchor.Row1 == row && (anchor.Col1 == col || col < 0)) { indexes.Add(shape); } } } foreach (HSSFShape shap in indexes) { shanpContainer.RemoveShape(shap); } } } else if (sheet is XSSFSheet) { } }
private HSSFComment LookForComment(HSSFShapeContainer container, int row, int column) { foreach (Object obj in container.Children) { HSSFShape shape = (HSSFShape)obj; if (shape is HSSFShapeGroup) { HSSFShape res = LookForComment((HSSFShapeContainer)shape, row, column); if (null != res) { return (HSSFComment)res; } continue; } if (shape is HSSFComment) { HSSFComment comment = (HSSFComment)shape; if (comment.HasPosition && comment.Column == column && comment.Row == row) { return comment; } } } return null; }
/** * build shape tree from escher container * @param container root escher container from which escher records must be taken * @param agg - EscherAggregate * @param out - shape container to which shapes must be added * @param root - node to create HSSFObjectData shapes */ public static void CreateShapeTree(EscherContainerRecord container, EscherAggregate agg, HSSFShapeContainer out1, DirectoryNode root) { if (container.RecordId == EscherContainerRecord.SPGR_CONTAINER) { ObjRecord obj = null; EscherClientDataRecord clientData = (EscherClientDataRecord)((EscherContainerRecord)container.GetChild(0)).GetChildById(EscherClientDataRecord.RECORD_ID); if (null != clientData) { obj = (ObjRecord)agg.GetShapeToObjMapping()[clientData]; } HSSFShapeGroup group = new HSSFShapeGroup(container, obj); IList <EscherContainerRecord> children = container.ChildContainers; // skip the first child record, it is group descriptor for (int i = 0; i < children.Count; i++) { EscherContainerRecord spContainer = children[(i)]; if (i != 0) { CreateShapeTree(spContainer, agg, group, root); } } out1.AddShape(group); } else if (container.RecordId == EscherContainerRecord.SP_CONTAINER) { Dictionary <EscherRecord, Record.Record> shapeToObj = agg.GetShapeToObjMapping(); ObjRecord objRecord = null; TextObjectRecord txtRecord = null; foreach (EscherRecord record in container.ChildRecords) { switch (record.RecordId) { case EscherClientDataRecord.RECORD_ID: objRecord = (ObjRecord)shapeToObj[(record)]; break; case EscherTextboxRecord.RECORD_ID: txtRecord = (TextObjectRecord)shapeToObj[(record)]; break; } } if (IsEmbeddedObject(objRecord)) { HSSFObjectData objectData = new HSSFObjectData(container, objRecord, root); out1.AddShape(objectData); return; } CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord)objRecord.SubRecords[0]; HSSFShape shape; switch (cmo.ObjectType) { case CommonObjectType.Picture: shape = new HSSFPicture(container, objRecord); break; case CommonObjectType.Rectangle: shape = new HSSFSimpleShape(container, objRecord, txtRecord); break; case CommonObjectType.Line: shape = new HSSFSimpleShape(container, objRecord); break; case CommonObjectType.ComboBox: shape = new HSSFCombobox(container, objRecord); break; case CommonObjectType.MicrosoftOfficeDrawing: EscherOptRecord optRecord = (EscherOptRecord)container.GetChildById(EscherOptRecord.RECORD_ID); EscherProperty property = optRecord.Lookup(EscherProperties.GEOMETRY__VERTICES); if (null != property) { shape = new HSSFPolygon(container, objRecord, txtRecord); } else { shape = new HSSFSimpleShape(container, objRecord, txtRecord); } break; case CommonObjectType.Text: shape = new HSSFTextbox(container, objRecord, txtRecord); break; case CommonObjectType.Comment: shape = new HSSFComment(container, objRecord, txtRecord, agg.GetNoteRecordByObj(objRecord)); break; default: shape = new HSSFSimpleShape(container, objRecord, txtRecord); break; } out1.AddShape(shape); } }
/// <summary> /// Recursively iterates a shape container to get all embedded objects. /// </summary> /// <param name="parent">the parent.</param> /// <param name="objects">the list of embedded objects to populate.</param> private void GetAllEmbeddedObjects(HSSFShapeContainer parent, List<HSSFObjectData> objects) { foreach (HSSFShape shape in parent.Children) { if (shape is HSSFObjectData) { objects.Add((HSSFObjectData) shape); } else if (shape is HSSFShapeContainer) { GetAllEmbeddedObjects((HSSFShapeContainer) shape, objects); } } }
/** * build shape tree from escher container * @param container root escher container from which escher records must be taken * @param agg - EscherAggregate * @param out - shape container to which shapes must be added * @param root - node to create HSSFObjectData shapes */ public static void CreateShapeTree(EscherContainerRecord container, EscherAggregate agg, HSSFShapeContainer out1, DirectoryNode root) { if (container.RecordId == EscherContainerRecord.SPGR_CONTAINER) { ObjRecord obj = null; EscherClientDataRecord clientData = (EscherClientDataRecord)((EscherContainerRecord)container.GetChild(0)).GetChildById(EscherClientDataRecord.RECORD_ID); if (null != clientData) { obj = (ObjRecord)agg.GetShapeToObjMapping()[clientData]; } HSSFShapeGroup group = new HSSFShapeGroup(container, obj); IList<EscherContainerRecord> children = container.ChildContainers; // skip the first child record, it is group descriptor for (int i = 0; i < children.Count; i++) { EscherContainerRecord spContainer = children[(i)]; if (i != 0) { CreateShapeTree(spContainer, agg, group, root); } } out1.AddShape(group); } else if (container.RecordId == EscherContainerRecord.SP_CONTAINER) { Dictionary<EscherRecord, Record.Record> shapeToObj = agg.GetShapeToObjMapping(); ObjRecord objRecord = null; TextObjectRecord txtRecord = null; foreach (EscherRecord record in container.ChildRecords) { switch (record.RecordId) { case EscherClientDataRecord.RECORD_ID: objRecord = (ObjRecord)shapeToObj[(record)]; break; case EscherTextboxRecord.RECORD_ID: txtRecord = (TextObjectRecord)shapeToObj[(record)]; break; } } if (IsEmbeddedObject(objRecord)) { HSSFObjectData objectData = new HSSFObjectData(container, objRecord, root); out1.AddShape(objectData); return; } CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord)objRecord.SubRecords[0]; HSSFShape shape; switch (cmo.ObjectType) { case CommonObjectType.Picture: shape = new HSSFPicture(container, objRecord); break; case CommonObjectType.Rectangle: shape = new HSSFSimpleShape(container, objRecord, txtRecord); break; case CommonObjectType.Line: shape = new HSSFSimpleShape(container, objRecord); break; case CommonObjectType.ComboBox: shape = new HSSFCombobox(container, objRecord); break; case CommonObjectType.MicrosoftOfficeDrawing: EscherOptRecord optRecord = (EscherOptRecord)container.GetChildById(EscherOptRecord.RECORD_ID); EscherProperty property = optRecord.Lookup(EscherProperties.GEOMETRY__VERTICES); if (null != property) { shape = new HSSFPolygon(container, objRecord, txtRecord); } else { shape = new HSSFSimpleShape(container, objRecord, txtRecord); } break; case CommonObjectType.Text: shape = new HSSFTextbox(container, objRecord, txtRecord); break; case CommonObjectType.Comment: shape = new HSSFComment(container, objRecord, txtRecord, agg.GetNoteRecordByObj(objRecord)); break; default: shape = new HSSFSimpleShape(container, objRecord, txtRecord); break; } out1.AddShape(shape); } }
private void ConvertShapes(HSSFShapeContainer parent, EscherContainerRecord escherParent, Hashtable shapeToObj) { if (escherParent == null) throw new ArgumentException("Parent record required"); IList shapes = parent.Children; for (IEnumerator iterator = shapes.GetEnumerator(); iterator.MoveNext(); ) { HSSFShape shape = (HSSFShape)iterator.Current; if (shape is HSSFShapeGroup) { ConvertGroup((HSSFShapeGroup)shape, escherParent, shapeToObj); } else { AbstractShape shapeModel = AbstractShape.CreateShape( shape, drawingManager.AllocateShapeId(drawingGroupId)); shapeToObj[FindClientData(shapeModel.SpContainer)]=shapeModel.ObjRecord; if (shapeModel is TextboxShape) { EscherRecord escherTextbox = ((TextboxShape)shapeModel).EscherTextbox; shapeToObj[escherTextbox]=((TextboxShape)shapeModel).TextObjectRecord; // escherParent.AddChildRecord(escherTextbox); if (shapeModel is CommentShape) { CommentShape comment = (CommentShape)shapeModel; tailRec.Add(comment.NoteRecord); } } escherParent.AddChildRecord(shapeModel.SpContainer); } } // drawingManager.newCluster( (short)1 ); // drawingManager.newCluster( (short)2 ); }