Smartart节点在C#中使用Interop-Excel在Excel工作表中错误添加 [英] Smartart nodes being added incorrectly in excel worksheet with interop-excel in c#

查看:110
本文介绍了Smartart节点在C#中使用Interop-Excel在Excel工作表中错误添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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:

这是预期的结果:

这是我的代码:

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指定与该节点有关的新节点的位置.将其添加到的节点.

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.

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

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:

  • 第一个是第二级的四个节点的for循环;第三层中的一个单独插入.为了使最后一个位于正确的节点(第一个)之下,在第一个迭代中将其分配给特定的SmartArtNode对象.
  • 第二个(注释掉)分别插入并标记每个节点.
  • 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.

注意:由于没有理由删除第一个节点,因此此代码将其保留完整,对其进行标记并将其分配给SmartArtNode对象(顶级).

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

    }

这篇关于Smartart节点在C#中使用Interop-Excel在Excel工作表中错误添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆