是否可以使用EPPlus库在Excel工作表中创建形状的副本/副本?

我知道我可以用

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


ws是工作表对象)

并用

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


但是,我找不到克隆shapeExisting的方法。

最佳答案

似乎没有内置功能,因此在找到更好的解决方案之前,我在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;
}


还有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);
}

关于c# - 使用EPPlus复制/克隆Excel形状吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18380656/

10-12 14:35