Copy/clone an Excel shape with EPPlus?

筅森魡賤 提交于 2019-12-10 19:38:18

问题


Is it possible to create a copy/clone of a shape in an Excel worksheet using the EPPlus library?

I know I can get an existing object with

var shapeExisting = ws.Drawings["ShapeName"];

(ws being the Worksheet object)

and a create new shape with

var shapeNew = ws.Drawings.AddShape("NewName", eShapeStyle.RtTriangle);

However, I'm unable to find a way to clone shapeExisting.


回答1:


Seems like there's no built-in functionality, so until I find a better solution, I added the following method to EPPlus\Drawings\ExcelDrawings.cs

public ExcelShape CloneShape(string SourceName, string TargetName)
{
    if ( _drawingNames.ContainsKey(TargetName.ToLower()))
    {
        throw new Exception("Target name already exists in the drawings collection");
    }

    if (!_drawingNames.ContainsKey(SourceName.ToLower()))
    {
        throw new Exception("Source shape does not exist in the drawings collection");
    }

    ExcelShape shape = new ExcelShape(this, this._drawingsXml,
                               (ExcelShape) this[SourceName]);
    shape.Name = TargetName;
    _drawings.Add(shape);
    _drawingNames.Add(TargetName.ToLower(), _drawings.Count - 1);
    return shape;
}

and also this constructor in ExcelShape.cs:

internal ExcelShape(ExcelDrawings drawings, XmlDocument DrawingsXml, ExcelShape shapeSource) :
            base(drawings, shapeSource._topNode.Clone(), "xdr:sp/xdr:nvSpPr/xdr:cNvPr/@name")

{
     this.init();
     XmlNode colNode = DrawingsXml.SelectSingleNode("//xdr:wsDr", NameSpaceManager);             
     colNode.AppendChild(this._topNode);
}


来源:https://stackoverflow.com/questions/18380656/copy-clone-an-excel-shape-with-epplus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!