使用HtmlAgilityPack將HtmlTable填入DataTable

       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 中
相關文章
相關標籤/搜索