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"]/tbodystringxpath ="//*[@id='report1']/tbody";
HtmlAgilityPack.HtmlNode node = rootNode.SelectSingleNode(xpath);//获取表格foreach(varscriptinnode.Descendants("script").ToArray())
script.Remove();
foreach(varstyleinnode.Descendants("style").ToArray())
style.Remove();
stringinnerText = node.OuterHtml;//到这里就是纯纯的表格了 vartrNodes = node.SelectNodes("tr");
DataTable dt =new DataTable();
for(inti =0; i <100; i++)//给表格增加100个列 {
DataColumn dc =new DataColumn();
dt.Columns.Add(dc);
}
foreach(vartrnodintrNodes)//遍历行 {
//dt.Rows.Add()DataRow dr = dt.NewRow();
vartdNodes = trnod.SelectNodes("td");
for(inti =0; i < tdNodes.Count; i++)//遍历列 {
dr[i] = tdNodes[i].InnerText;
}
dt.Rows.Add(dr);
}
// 所有的数据读取到了datatable 中
网友评论