Smartart nodes being added incorrectly in excel worksheet with interop-excel in c#

北战南征 提交于 2020-01-16 14:27:05

问题


I have a c# console application which creates an excel worksheet containing of a smartart object with the hiearchy layout (org chart). When i proceed to add nodes to the smartart object, it places the nodes in incorrect levels.

The first node created is called "node 1" and is correctly placed at the first level. I then create 4 new nodes(node 1.1, node 1.2, node 1.3, node 1.4) from the first node to be placed at the second level with node 1 as a parent node. I also created a third level node (node 1.1.1) with node 1.1 as parentnode.

I somehow get the following result:

Where this is the intended result:

Here is my code:

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

回答1:


What's missing in the code in the question is the parameter in the AddNode method: Office.MsoSmartArtNodePosition that specifies where the new node whould be in relation to the node to which it is being added.

The sample code below uses .msoSmartArtNodeBelow through out, but it's also possible to add nodes before, after or above. (If an older version of C# were being used, the code would not even have compiled, which says something about trying to make the language "more forgiving", like VB languages...)

The code sample demonstrates two approaches:

  • the first is a for loop for the four nodes at the second level; the one in the third level is inserted individually. So that this last comes under the correct node (the first), that one is assigned to a specific SmartArtNode object on the first iteration.
  • the second (commented out) inserts and labels each node individually.

Note: Since there's no reason to delete the first node, this code leaves it intact, labels it and assigns it to a SmartArtNode object (top-level).

    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";

    }


来源:https://stackoverflow.com/questions/58455280/smartart-nodes-being-added-incorrectly-in-excel-worksheet-with-interop-excel-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!