使用EPPlus将Excel形状复制/克隆到其他工作表?

时间:2015-06-02 13:51:39

标签: c# excel epplus

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

此处显示的同一Excel工作表中已有解决方案:Copy/clone an Excel shape with EPPlus?

但我需要将其复制到其他工作表(从模板到目的地)的可能性。有谁知道解决方法吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

实际上我找到了解决这个问题的方法。 我将此代码添加到ExcelDrawings.cs文件中:

    /// <summary>
    /// Add a new shape to the worksheet
    /// </summary>
    /// <param name="Name">Name</param>
    /// <param name="Source">Source shape</param>
    /// <returns>The shape object</returns>

    public ExcelShape AddShape(string Name, ExcelShape Source)
    {
        if (Worksheet is ExcelChartsheet && _drawings.Count > 0)
        {
            throw new InvalidOperationException("Chart worksheets can't have more than one drawing");
        }
        if (_drawingNames.ContainsKey(Name))
        {
            throw new Exception("Name already exists in the drawings collection");
        }
        XmlElement drawNode = CreateDrawingXml();
        drawNode.InnerXml = Source.TopNode.InnerXml;

        ExcelShape shape = new ExcelShape(this, drawNode);
        shape.Name = Name;
        shape.Style = Source.Style;
        _drawings.Add(shape);
        _drawingNames.Add(Name, _drawings.Count - 1);
        return shape;
    }