posts - 124,  comments - 29,  trackbacks - 0
 class ReadExcelToTree
    
{
        
public ReadExcelToTree()
        
{

        }

        
/// <summary>
        
/// 读取Excel文件,内容存储在DataSet中
        
/// </summary>
        
/// <param name="opnFileName">Excel文件路径</param>
        
/// <returns>DataSet</returns>

         public static DataSet ExcelToDataSet(string FileName)
        
{
            
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
            OleDbConnection conn 
= new OleDbConnection(strConn);
            
string strExcel ="select * from [sheet1$]";
            OleDbDataAdapter XlsCommand 
= null;
            DataSet ds 
= new DataSet();
            
try
            
{
                conn.Open();
                XlsCommand 
= new OleDbDataAdapter(strExcel, strConn);
                XlsCommand.Fill(ds, 
"sheet1");
             
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(
"错误!:" + ex.Message.ToString());
            
            }

            
finally
            
{
                conn.Close();
                conn.Dispose();
                
            }

            
return ds;
        }


    }

主程序中调用:DataSet ds = ReadExcelToTree.ExcelToDataSet(@"CC08Help.xls");

生成树:
 1   private void InitTreeView(DataTable dt, TreeView tv)
 2        {
 3            TreeNode curNodeLevel0 = null, curNodeLevel1 = null, curNodeLevel2 = null,
 4                curNodeLevel3 = null, curNodeLevel4 = null, curNodeLevel5 = null,
 5                curNodeLevel6 = null, curNodeLevel7 = null;
 6
 7            //对第一层节点初始化.
 8            //就是Excel的 A1 单元格 我们定义该层节点为0层节点
 9            curNodeLevel0 = new TreeNode("联机帮助");
10
11            for (int i = 0; i < dt.Rows.Count; i++)
12            {
13                DataRow dr = dt.Rows[i];
14
15                // 如果dr[1]不为空,则说明这是一个新的一层节点.该层节点为 1 层
16                if (dr[1].ToString() != string.Empty)
17                {
18                    //创建一个节点.
19                    TreeNode t = new TreeNode(dr[1].ToString());
20
21                    //将该节点放到0层节点的子节点上,即一层节点
22                    curNodeLevel0.Nodes.Add(t);
23
24                    //设置当前的一层节点为刚刚生成的节点.
25                    curNodeLevel1 = t;
26                    //开始处理下一行.
27                    continue;
28                }

29
30
31                // 如果循环走到这儿,则说明dr[1].ToString() == String.Empty;
32                if (dr[2].ToString() != String.Empty)
33                {
34                    TreeNode t = new TreeNode(dr[2].ToString());
35                    curNodeLevel1.Nodes.Add(t);
36                    curNodeLevel2 = t;
37                    continue;
38                }

39
40                if (dr[3].ToString() != string.Empty)
41                {
42                    TreeNode t = new TreeNode(dr[3].ToString());
43                    curNodeLevel2.Nodes.Add(t);
44                    curNodeLevel3 = t;
45                    continue;
46                }

47
48                if (dr[4].ToString() != string.Empty)
49                {
50                    TreeNode t = new TreeNode(dr[4].ToString());
51                    curNodeLevel3.Nodes.Add(t);
52                    curNodeLevel4 = t;
53                    continue;
54                }

55                if (dr[5].ToString() != string.Empty)
56                {
57                    TreeNode t = new TreeNode(dr[5].ToString());
58                    curNodeLevel4.Nodes.Add(t);
59                    curNodeLevel5 = t;
60                    continue;
61                }

62                if (dr[6].ToString() != string.Empty)
63                {
64                    TreeNode t = new TreeNode(dr[6].ToString());
65                    curNodeLevel5.Nodes.Add(t);
66                    curNodeLevel6 = t;
67                    continue;
68                }

69                if (dr[7].ToString() != string.Empty)
70                {
71                    TreeNode t = new TreeNode(dr[7].ToString());
72                    curNodeLevel6.Nodes.Add(t);
73                    curNodeLevel7 = t;
74                    continue;
75                }

76            }

77
78            tv.Nodes.Add(curNodeLevel1);
79            tv.CollapseAll();
80        }


程序中调用:
        private void Help_Load(object sender, EventArgs e)
        {
            this.tvHelp.ImageList = this.imgLHelp;
            DataSet ds = ReadExcelToTree.ExcelToDataSet(@"CC08Help.xls");
            if (ds != null)
            {
                InitTreeView(ds.Tables[0], this.tvHelp);
            }
            foreach (TreeNode tn in tvHelp.Nodes)
            {
                SetLeafNodeImageIndex(tn);
            }

            this.webBrowser1.Url = new Uri(GetTNHtmlName(new TreeNode("(命令联机帮助)")));
        }



        private void TVHelp_MouseClick(object sender, MouseEventArgs e)
        {
            TreeNode selTreenode = tvHelp.GetNodeAt(e.Location);  //注意不要用tvHelp.selected
            if (e.Button == MouseButtons.Left)
            {
                if (selTreenode != null
                    && selTreenode.Nodes.Count == 0)//代表此节点是叶节点
                {
                    this.webBrowser1.Url = new Uri(GetTNHtmlName(selTreenode)) ;
                }
              
            }
        }

 1
 2        //设置叶子节点的图标
 3        private void SetLeafNodeImageIndex(TreeNode tn)
 4        {
 5            if (tn.Nodes.Count == 0)
 6            {
 7                tn.ImageIndex = 1;
 8                return;
 9            }

10            else
11            {
12                tn.ImageIndex = 0;
13            }

14            foreach (TreeNode childNode in tn.Nodes)
15            {
16                SetLeafNodeImageIndex(childNode);
17            }

18        }


 

 1   private string GetTNHtmlName(TreeNode tn)
 2        {
 3            int startIndex = tn.Text.LastIndexOf('(');
 4            int startIndex1 = tn.Text.LastIndexOf(''); ;
 5            int endIndex = tn.Text.LastIndexOf(')');
 6            int endIndex1 = tn.Text.LastIndexOf('');
 7
 8            if(startIndex1 > startIndex)
 9            {
10                startIndex = startIndex1;
11            }

12            if (endIndex1 > endIndex)
13            {
14                endIndex = endIndex1;
15            }

16
17            if (startIndex < 0 || endIndex < 0)
18            {
19                return "res://" + System.Environment.SystemDirectory + @"\system32\shdoclc.dll/http_404.htm";
20            }

21
22            string url = "file://" + Application.StartupPath + "/document/" + tn.Text.Substring(startIndex + 1, endIndex - startIndex - 1+ ".htm";
23            return url;
24        }


 生成树节点的简化代码——改版!


 private void InitTreeView(DataTable dt, TreeView tv)
        {
            int colums = dt.Columns.Count;
            TreeNode[] curNodeLevel = new TreeNode[colums];
            //对第一层节点初始化.
            //就是Excel的 A1 单元格 我们定义该层节点为0层节点
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                for (int j = 1; j <= colums; j++)
                {
                    if (j == 1 && dr[j].ToString()!=String.Empty)
                    {
                        TreeNode t = new TreeNode(dr[j].ToString());
                        curNodeLevel[0] = t;
                        break;
                    }
                    else if(j >= 2)
                    {
                        if (dr[j].ToString() != string.Empty)
                        {
                            TreeNode t = new TreeNode(dr[j].ToString());
                            curNodeLevel[j - 2].Nodes.Add(t);
                            curNodeLevel[j - 1] = t;
                            break;
                        }
                    }
                }
            }

            tv.Nodes.Add(curNodeLevel[0]);
            tv.Nodes[0].Expand();
        }

posted on 2008-09-19 20:25 天书 阅读(1000) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理



<2008年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(5)

随笔档案

文章分类

文章档案

好友的Bolg

搜索

  •  

最新评论

阅读排行榜

评论排行榜