Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在excel工作表中错误地添加Smartart节点,并在c中使用互操作excel#_C#_.net_Excel_Office Interop_Excel Interop - Fatal编程技术网

C# 在excel工作表中错误地添加Smartart节点,并在c中使用互操作excel#

C# 在excel工作表中错误地添加Smartart节点,并在c中使用互操作excel#,c#,.net,excel,office-interop,excel-interop,C#,.net,Excel,Office Interop,Excel Interop,我有一个c#控制台应用程序,它创建一个excel工作表,其中包含一个具有层次结构布局(组织结构图)的smartart对象。当我继续向smartart对象添加节点时,它会将节点放置在不正确的级别 创建的第一个节点称为“节点1”,并正确放置在第一层。然后,我从第一个节点创建4个新节点(节点1.1、节点1.2、节点1.3、节点1.4),将节点1作为父节点放置在第二级。我还创建了一个第三级节点(节点1.1.1),节点1.1作为parentnode 我不知怎么得到了以下结果: 如果这是预期结果: 这是

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

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

我不知怎么得到了以下结果:

如果这是预期结果:

这是我的密码:

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#,代码甚至不会编译,这说明试图让语言“更宽容”,比如VB语言……)

代码示例演示了两种方法:

  • 第一个是第二级四个节点的
    for
    循环;第三层中的一个单独插入。因此,最后一个节点位于正确的节点(第一个)下,在第一次迭代中,该节点被分配给特定的
    SmartArtNode
    对象
  • 第二个(注释掉)分别插入和标记每个节点
注意:由于没有理由删除第一个节点,因此此代码将其保持不变,对其进行标记并将其分配给
SmartArtNode
对象(顶级)

var myLayout=excelApp.smartlayouts[88];
var smartshape=ws.Shapes.AddSmartArt(myLayout,50,50,200,200);
if(smartshape.HasSmartArt==Office.MsoTriState.msoTrue)
{
Office.SmartArt-SmartArt=smartArtShape.SmartArt;
Office.smartNodes nds=smartArt.AllNodes;
Office.smartNode ndTop=null;
foreach(nds中的Office.smartNode nd)
{
如果(第二级!=1)
{
删除();
}
其他的
{
ndTop=nd;
ndTop.TextFrame2.TextRange.Text=“节点1”;
}
}
Office.smartNode ndLev2=null;
Office.smartNode ndLev2_1=null;

对于(int i=1;我感谢你..这非常有效。如果你知道如何向节点添加超链接,我会问以下问题:
    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";

    }