使用HtmlAgilityPack將HtmlTable填入DataTable

kqw發表於2018-11-28
       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 中

 

相關文章