Reputation: 193
I have a link and i want to apply webscraping through which i can get the info of table and and then export this table into excel.Please suggest
HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
var myTable = doc.DocumentNode
.Descendants("table")
.Where(t => t.Attributes["id"].Value == someTableId)
.FirstOrDefault();
if (myTable != null)
{
///further parsing here
}
the code i m gonna use is above mentioned.As I am beginner so anyone ca tell me what to do
Upvotes: 0
Views: 1137
Reputation: 63
You may just continue iterating through table rows and cells:
if (myTable != null)
{
int iRow=0;
var tableRows = myTable
.Descendants("tr");
foreach (var tableRow in tableRows)
{
var rowCells = tableRow
.Descendants("td");
int iColumn=0;
foreach (var cell in rowCells)
{
//Save to Excel code
//Perform any checks here to ensure youre getting a valid value from the cell contents
//Excel.Cell[iRow,iColumn++]=cell.InnerText;
}
iRow++;
}
}
}
You may use any third party tool to save the values to Excel, like NPOI for binary format (up to Excel 2003) or ClosedXML if you want to use OpenXML format (Excel 2007 and above).
Upvotes: 1