2017-03-01 69 views
1

我想導出一個treeview,其中有幾個節點,要優秀的子節點。目前的進展是下面的代碼。TreeNode導出爲Excel

如何提取所有節點和子節點以分隔列?

string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
using (StreamWriter sw = new StreamWriter(mydocpath + @"\MaterialList.csv")) 
foreach (TreeNode node in treeView1.Nodes) 
{ 
     sw.WriteLine(node); 
} 
+0

你能舉一個簡單的輸入/輸出情況的一個例子,(即給予一定的樹,這將是從這個方法您的輸出)?這有助於給你的問題一個更明確的答案。 – tjcertified

+0

從下面的Web圖像中看到的樹形圖如下所示。但沒有複選框https://www.codeproject.com/Articles/202435/Tri-State-Tree-View – FlaxoIce

+0

樹視圖看起來像Lets例如我有這棵樹 - 從你的一個例子中得到這個。 動物 狗 貓 魚 淡水 羅奇 魴 鹹水 滑板 SkateChild 靈魂 – FlaxoIce

回答

0

我相信你正在尋找中序遍歷樹?除非我誤解了某些東西。

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()); 

    } 
} 

這個小功能使用遞歸調用打印出他們似乎開始「根」的順序樹的所有節點。我認爲,從這個出發點出發,你應該能夠相當容易地弄清楚這一切。如果沒有,請讓我知道(或者如果這不是你要找的東西)。這是一個C++的例子,但它應該仍然幫助你。

編輯:跟蹤穿越深度(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 
    } 
} 

編輯2:既然你仍然不確定如何使用這個,我在外面寫的代碼。

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

要在代碼中使用這樣的:

void main(void){ 
    InorderTraversal(treeView1.Nodes); 
} 
+0

請參閱此圖像。 http://imgh.us/TreeviewToExcel.png – FlaxoIce

+0

是的,我看到了,你需要修改我給你的代碼來計算你在樹中的深度。如果你處於「第一級」(或根部),那麼你需要插入第1列,並在第2列和第3列中列出。 http://stackoverflow.com/questions/443695/traversing-a-tree-of-objects-in-c-sharp 這應該會幫助你跟蹤你在遞歸中的嵌套程度。 – user3164339

+0

我會很感激,如果有一個簡單的答案,我是新編程 – FlaxoIce