欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用HtmlAgilityPack将HtmlTable填入DataTable

程序员文章站 2023-01-29 13:58:00
HtmlAgilityPack.HtmlWeb hw = new HtmlAgilityPack.HtmlWeb(); HtmlAgilityPack.HtmlDocument doc = hw.Load("C:\\Users\\u1\\Desktop\\test html.html"); Html... ......
       htmlagilitypack.htmlweb hw = new htmlagilitypack.htmlweb();
            htmlagilitypack.htmldocument doc = hw.load("c:\\users\\u1\\desktop\\test html.html");
            htmlagilitypack.htmlnode rootnode = doc.documentnode;//获取文档的根节点                                                                                       //   //*[@id="report1"]/tbody
            string xpath = "//*[@id='report1']/tbody";
            htmlagilitypack.htmlnode node = rootnode.selectsinglenode(xpath);//获取表格
            foreach (var script in node.descendants("script").toarray())
                script.remove();
            foreach (var style in node.descendants("style").toarray())
                style.remove();
            string innertext = node.outerhtml;//到这里就是纯纯的表格了      
            var trnodes = node.selectnodes("tr");
            datatable dt = new datatable();
            for (int i = 0; i < 100; i++)   //给表格增加100个列
            {
                datacolumn dc = new datacolumn();
                dt.columns.add(dc);
            }
            foreach (var trnod in trnodes) //遍历行
            {
                //dt.rows.add()
                datarow dr = dt.newrow();
                var tdnodes = trnod.selectnodes("td");
                for (int i = 0; i < tdnodes.count; i++) //遍历列
                {
                    dr[i] = tdnodes[i].innertext;
                }
                dt.rows.add(dr);
            }
            // 所有的数据读取到了datatable 中