Smartart节点在excel工作表中错误地添加了c#中的interop-excel

问题描述 投票:0回答:1

我有一个c#控制台应用程序,该应用程序创建一个excel工作表,其中包含具有层次结构布局(组织结构图)的smartart对象。当我继续将节点添加到smartart对象时,它将节点放置在错误的级别。

创建的第一个节点称为“节点1”,并正确放置在第一层。然后,我从第一个节点创建4个新节点(节点1.1,节点1.2,节点1.3,节点1.4),并以节点1作为父节点放置在第二层。我还创建了一个以节点1.1作为父节点的第三级节点(节点1.1.1)。

我以某种方式得到以下结果:Result of current code

这是预期的结果:Intended result

这是我的代码:

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";
        }
    }
c# .net excel office-interop excel-interop
1个回答
0
投票

问题代码中缺少的是AddNode方法中的参数:Office.MsoSmartArtNodePosition,用于指定新节点相对于要添加到的节点的位置。

下面的示例代码一直使用.msoSmartArtNodeBelow,但也可以在之前,之后或之上添加节点。 (如果使用的是较旧版本的C#,则该代码甚至都不会编译,这说明了如何使语言“更宽容”,例如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";

    }
© www.soinside.com 2019 - 2024. All rights reserved.