1

I am trying to export a treeview which has several nodes, child nodes to excel. the progress so far is the below code.

How can I extract all the nodes and childnodes to seperate columns?

string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
using (StreamWriter sw = new StreamWriter(mydocpath + @"\MaterialList.csv"))
foreach (TreeNode node in treeView1.Nodes)
{
      sw.WriteLine(node);
}
tjcertified
  • 694
  • 7
  • 18
Mahi
  • 21
  • 3
  • Can you give an example of a simple input/output case, (i.e. given a certain tree, what would be your output from this method)? That would help give a more definite answer to your question. – tjcertified Mar 01 '17 at 19:07
  • The treeview looks something like this from the web image below. but there're no checkbox https://www.codeproject.com/Articles/202435/Tri-State-Tree-View – Mahi Mar 01 '17 at 19:11
  • The treeview looks something like Lets for example I have this Tree - got this from one of your examples. Animal Dog Cat Fish Fresh Water Roach Bream Salt Water Skate SkateChild Soul – Mahi Mar 01 '17 at 19:17
  • Ok. Can you possibly edit your question to add the structure of that tree, and then also add your expected output of your method above? I am imagining a table with certain columns, but I'm not sure where you would want the columns to stop. Would columns be 'Dog', 'Cat', 'Fish' in this case, etc.? – tjcertified Mar 01 '17 at 19:20
  • Similar to this image, if it helps to understand. https://social.msdn.microsoft.com/Forums/getfile/115327 – Mahi Mar 01 '17 at 19:29
  • That's perfect. – tjcertified Mar 01 '17 at 19:34

1 Answers1

1

I believe you are looking for Inorder Tree Traversal? Unless I've misunderstood something.

http://www.cplusplus.com/forum/beginner/131457/

void Tree:: Inorder(Node* Root)
{
    if(Root != NULL)
    {
        Inorder(Root->Left());
        cout << Root->Key() << endl;
        Inorder(Root->Right());

    }
}

This little function uses recursive calls to print out all the nodes of a tree in the order they appear starting at 'root'. I think with this starting point you should be able to figure it all out fairly easily. If not, please let me know (or if this isn't even what you're looking for). It's a c++ example, but it should still help you out.

EDIT: to keep track of traversal depth (Traversing a tree of objects in c#):

printRoot(Node node)
{
  printNode(node, 0);
}

printNode(Node node, int level)
{
  printTitle(node.title)
  foreach (Node child in node.children)
  {
    printNode(child, level + 1); //<-- recursive
  }
}

EDIT 2: Since you're still unsure of how to use this i'll write the code out.

public void InorderTraversal(TreeNode root){
  InorderTraversal(root, 0);
}

public void InorderTraversal(TreeNode root, int level){
  //this is where you write to excel.  You can also use 'level' to decide if you insert in column 1, 2, or 3 just by using a simple if statement like this:
  if(level == 0)
    sw.WriteLine(node, level.ToString());

  foreach (TreeNode child in root.children){  //depending on how your tree is implemented this is probably either going to be root.children or root.next
    InorderTraversal(child, level + 1);
  } 
}

To use this in your code:

void main(void){
  InorderTraversal(treeView1.Nodes);
}
Community
  • 1
  • 1
user3164339
  • 165
  • 14
  • yes i saw it, you'll need to modify the code I gave you to count how deep you are in the tree. if you're on the '1st level' (or root) you instert to column 1, and exct for 2nd and 3rd column. http://stackoverflow.com/questions/443695/traversing-a-tree-of-objects-in-c-sharp that should help you keep track of how deeply nested you are in the recursion. – user3164339 Mar 01 '17 at 20:46
  • I would appreciate if there's a simple answer, I'm new to programming – Mahi Mar 02 '17 at 08:01
  • 1
    @Flaxolce I hope my edit helps some more. Honestly if you're that new to programming, it might be more useful to you to start with writing an array to excel first. Tree traversal is a bit more advanced, you've kinda jumped straight into the deep-end of the pool without knowing what swimming is. Either way, I'll still be here to help you more if this is still confusing – user3164339 Mar 02 '17 at 16:59