【发布时间】:2015-06-18 14:23:52
【问题描述】:
是否可以使用 EPPlus 库将形状复制/克隆到其他 Excel 工作表?
此处显示的同一个 Excel 工作表中已有解决方案:Copy/clone an Excel shape with EPPlus?
但我需要将其复制到其他工作表(从模板到目标)的可能性。有人知道解决方法吗?
提前致谢。
【问题讨论】:
是否可以使用 EPPlus 库将形状复制/克隆到其他 Excel 工作表?
此处显示的同一个 Excel 工作表中已有解决方案:Copy/clone an Excel shape with EPPlus?
但我需要将其复制到其他工作表(从模板到目标)的可能性。有人知道解决方法吗?
提前致谢。
【问题讨论】:
其实我找到了解决这个问题的办法。 我将此代码添加到 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;
}
【讨论】: