2013-08-22 81 views
3

是否可以使用EPPlus庫在Excel工作表中創建形狀的複製/克隆?使用EPPlus複製/克隆Excel形狀?

我知道我能得到一個現有對象與

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

ws作爲工作表對象)

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

但是一個創造新的形狀,我不能找到一種方法克隆shapeExisting

回答

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