使用EPPlus如何复制/克隆Excel形状?

3

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

我知道可以使用以下代码获取现有对象:

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

ws 是工作表对象)

创建一个新形状:

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

然而,我找不到一种克隆shapeExisting的方法。

2个回答

1

看起来没有内置功能,所以在找到更好的解决方案之前,我将以下方法添加到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);
}

0

我刚刚找到了一个使用当前版本的EPPlus(5.2.0)克隆形状的解决方案,我想在这里发布给任何感兴趣的人:

var clonedShape = 
     ws.Drawings.AddShape(
         "NewName",
         ws.Drawings["ShapeName"].As.Shape
     );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接