Smartart 节点被错误地添加到 excel 工作表中,在 c# 中使用 interop-excel
Smartart nodes being added incorrectly in excel worksheet with interop-excel in c#
我有一个 c# 控制台应用程序,它创建一个 excel 工作表,其中包含一个具有层次结构布局(组织结构图)的 smartart 对象。当我继续向 smartart 对象添加节点时,它会将节点置于错误的级别。
创建的第一个节点称为 "node 1" 并正确放置在第一层。然后,我从第一个节点创建 4 个新节点(节点 1.1、节点 1.2、节点 1.3、节点 1.4)以将节点 1 作为父节点放置在第二层。我还创建了一个第三级节点(节点 1.1.1),节点 1.1 作为父节点。
我以某种方式得到以下结果:
这是预期的结果:
这是我的代码:
private static Excel.Workbook Wb = null;
private static Excel.Application Xl = null;
private static Excel.Worksheet Sheet = null;
static void Main(string[] args)
{
Xl = new Excel.Application();
Xl.Visible = true;
Wb = Xl.Workbooks.Add();
Sheet = Wb.Worksheets[1];
var myLayout = Xl.SmartArtLayouts[93];
var smartArtShape = Sheet.Shapes.AddSmartArt(myLayout, 50, 50, 600, 600);
smartArtShape.AlternativeText = "Test";
if (smartArtShape.HasSmartArt == Office.MsoTriState.msoTrue)
{
Office.SmartArt smartArt = smartArtShape.SmartArt;
Office.SmartArtNodes nds = smartArt.AllNodes;
//Delete template nodes
for (int i = nds.Count; i >= 1; i--)
{
nds[i].Delete();
}
//Add main node
Office.SmartArtNode main = smartArt.Nodes.Add();
main.TextFrame2.TextRange.Text = "Node 1";
//Add main child node
Office.SmartArtNode aNode = main.Nodes.Add();
aNode.TextFrame2.TextRange.Text = "Node 1.1";
//Add 1.1 child node
Office.SmartArtNode a2Node = aNode.Nodes.Add();
a2Node.TextFrame2.TextRange.Text = "Node 1.1.1";
//Add main child node
Office.SmartArtNode bNode = main.Nodes.Add();
bNode.TextFrame2.TextRange.Text = "Node 1.2";
//Add main child node
Office.SmartArtNode cNode = main.Nodes.Add();
cNode.TextFrame2.TextRange.Text = "Node 1.3";
//Add main child node
Office.SmartArtNode dNode = main.Nodes.Add();
dNode.TextFrame2.TextRange.Text = "Node 1.4";
}
}
问题代码中缺少的是 AddNode
方法中的参数:Office.MsoSmartArtNodePosition
指定新节点相对于要添加到的节点的位置。
下面的示例代码始终使用 .msoSmartArtNodeBelow
,但也可以在之前、之后或之上添加节点。 (如果使用的是旧版本的 C#,代码甚至不会编译,这说明试图使语言 "more forgiving",如 VB 语言...)
代码示例演示了两种方法:
- 第一个是第二层四个节点的
for
循环;第三层的那个是单独插入的。因此,最后一个位于正确的节点(第一个)下,在第一次迭代时将其分配给特定的 SmartArtNode
对象。
- 第二个(已注释掉)单独插入并标记每个节点。
注意:由于没有理由删除第一个节点,此代码将其保持原样,对其进行标记并将其分配给 SmartArtNode
对象(顶级)。
var myLayout = excelApp.SmartArtLayouts[88];
var smartArtShape = ws.Shapes.AddSmartArt(myLayout, 50, 50, 200, 200);
if (smartArtShape.HasSmartArt == Office.MsoTriState.msoTrue)
{
Office.SmartArt smartArt = smartArtShape.SmartArt;
Office.SmartArtNodes nds = smartArt.AllNodes;
Office.SmartArtNode ndTop = null;
foreach (Office.SmartArtNode nd in nds)
{
if (nd.Level != 1)
{
nd.Delete();
}
else
{
ndTop = nd;
ndTop.TextFrame2.TextRange.Text = "Node 1";
}
}
Office.SmartArtNode ndLev2 = null;
Office.SmartArtNode ndLev2_1 = null;
for (int i = 1; i <= 4; i++)
{
ndLev2 = ndTop.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow);
if (i == 1) ndLev2_1 = ndLev2;
ndLev2.TextFrame2.TextRange.Text = "Node 1." + i;
}
//Office.SmartArtNode ndLev2_1 = ndTop.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow);
//ndLev2_1.TextFrame2.TextRange.Text = "Node 1.1";
//Office.SmartArtNode ndLev2_2 = ndTop.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow);
//ndLev2_2.TextFrame2.TextRange.Text = "Node 1.2";
//Office.SmartArtNode ndLev2_3 = ndTop.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow);
//ndLev2_3.TextFrame2.TextRange.Text = "Node 1.3";
Office.SmartArtNode ndLev2_1_1 = ndLev2_1.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow);
ndLev2_1_1.TextFrame2.TextRange.Text = "Node 1.1.1";
}
我有一个 c# 控制台应用程序,它创建一个 excel 工作表,其中包含一个具有层次结构布局(组织结构图)的 smartart 对象。当我继续向 smartart 对象添加节点时,它会将节点置于错误的级别。
创建的第一个节点称为 "node 1" 并正确放置在第一层。然后,我从第一个节点创建 4 个新节点(节点 1.1、节点 1.2、节点 1.3、节点 1.4)以将节点 1 作为父节点放置在第二层。我还创建了一个第三级节点(节点 1.1.1),节点 1.1 作为父节点。
我以某种方式得到以下结果:
这是预期的结果:
这是我的代码:
private static Excel.Workbook Wb = null;
private static Excel.Application Xl = null;
private static Excel.Worksheet Sheet = null;
static void Main(string[] args)
{
Xl = new Excel.Application();
Xl.Visible = true;
Wb = Xl.Workbooks.Add();
Sheet = Wb.Worksheets[1];
var myLayout = Xl.SmartArtLayouts[93];
var smartArtShape = Sheet.Shapes.AddSmartArt(myLayout, 50, 50, 600, 600);
smartArtShape.AlternativeText = "Test";
if (smartArtShape.HasSmartArt == Office.MsoTriState.msoTrue)
{
Office.SmartArt smartArt = smartArtShape.SmartArt;
Office.SmartArtNodes nds = smartArt.AllNodes;
//Delete template nodes
for (int i = nds.Count; i >= 1; i--)
{
nds[i].Delete();
}
//Add main node
Office.SmartArtNode main = smartArt.Nodes.Add();
main.TextFrame2.TextRange.Text = "Node 1";
//Add main child node
Office.SmartArtNode aNode = main.Nodes.Add();
aNode.TextFrame2.TextRange.Text = "Node 1.1";
//Add 1.1 child node
Office.SmartArtNode a2Node = aNode.Nodes.Add();
a2Node.TextFrame2.TextRange.Text = "Node 1.1.1";
//Add main child node
Office.SmartArtNode bNode = main.Nodes.Add();
bNode.TextFrame2.TextRange.Text = "Node 1.2";
//Add main child node
Office.SmartArtNode cNode = main.Nodes.Add();
cNode.TextFrame2.TextRange.Text = "Node 1.3";
//Add main child node
Office.SmartArtNode dNode = main.Nodes.Add();
dNode.TextFrame2.TextRange.Text = "Node 1.4";
}
}
问题代码中缺少的是 AddNode
方法中的参数:Office.MsoSmartArtNodePosition
指定新节点相对于要添加到的节点的位置。
下面的示例代码始终使用 .msoSmartArtNodeBelow
,但也可以在之前、之后或之上添加节点。 (如果使用的是旧版本的 C#,代码甚至不会编译,这说明试图使语言 "more forgiving",如 VB 语言...)
代码示例演示了两种方法:
- 第一个是第二层四个节点的
for
循环;第三层的那个是单独插入的。因此,最后一个位于正确的节点(第一个)下,在第一次迭代时将其分配给特定的SmartArtNode
对象。 - 第二个(已注释掉)单独插入并标记每个节点。
注意:由于没有理由删除第一个节点,此代码将其保持原样,对其进行标记并将其分配给 SmartArtNode
对象(顶级)。
var myLayout = excelApp.SmartArtLayouts[88];
var smartArtShape = ws.Shapes.AddSmartArt(myLayout, 50, 50, 200, 200);
if (smartArtShape.HasSmartArt == Office.MsoTriState.msoTrue)
{
Office.SmartArt smartArt = smartArtShape.SmartArt;
Office.SmartArtNodes nds = smartArt.AllNodes;
Office.SmartArtNode ndTop = null;
foreach (Office.SmartArtNode nd in nds)
{
if (nd.Level != 1)
{
nd.Delete();
}
else
{
ndTop = nd;
ndTop.TextFrame2.TextRange.Text = "Node 1";
}
}
Office.SmartArtNode ndLev2 = null;
Office.SmartArtNode ndLev2_1 = null;
for (int i = 1; i <= 4; i++)
{
ndLev2 = ndTop.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow);
if (i == 1) ndLev2_1 = ndLev2;
ndLev2.TextFrame2.TextRange.Text = "Node 1." + i;
}
//Office.SmartArtNode ndLev2_1 = ndTop.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow);
//ndLev2_1.TextFrame2.TextRange.Text = "Node 1.1";
//Office.SmartArtNode ndLev2_2 = ndTop.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow);
//ndLev2_2.TextFrame2.TextRange.Text = "Node 1.2";
//Office.SmartArtNode ndLev2_3 = ndTop.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow);
//ndLev2_3.TextFrame2.TextRange.Text = "Node 1.3";
Office.SmartArtNode ndLev2_1_1 = ndLev2_1.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow);
ndLev2_1_1.TextFrame2.TextRange.Text = "Node 1.1.1";
}