简体   繁体   English

如何使用C#从网站抓取表格并将其导出到Excel

[英]How to scrape table from website and export to excel using c#

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 我有一个链接,我想应用webscraping,通过它我可以获取表的信息,然后将此表导出到excel.Please建议

  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 im gonna use is above mentioned.As I am beginner so anyone ca tell me what to do 我将要使用的代码已在上面提到。作为我的初学者,所以任何人都可以告诉我该怎么做

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). 您可以使用任何第三方工具将值保存到Excel,例如二进制格式的NPOI (最高Excel 2003)或要使用OpenXML格式(Excel 2007及更高版本)的ClosedXML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM