是否可以使用 EPPlus 库在 Excel 工作表中创建形状的副本/克隆?
我知道我可以得到一个现有的对象
var shapeExisting = ws.Drawings["ShapeName"];
(ws
作为 Worksheet 对象)
和一个创造新的形状
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);
}
我刚刚找到了一个使用当前版本的 EPPlus (5.2.0) 克隆形状的解决方案,我想在这里为任何感兴趣的人发布:
var clonedShape =
ws.Drawings.AddShape(
"NewName",
ws.Drawings["ShapeName"].As.Shape
);