c# 抓取网页数据并导出到excel表格

引用HtmlAgilityPack.dll程序集

关于HtmlAgilityPack的介绍参考下面这个链接

https://mp.weixin.qq.com/s?src=11×tamp=1541552604&ver=1229&signature=uDJvEN99KoVng8qAptQ1Sd-DXMbqVbqW0Y8FHAZUU7uw1gL5fPrQB2rxHrEtIWQwyWYoLKrgcF1zUiMHCDSbN-BaUPfBcvi7A-FNB51S6NEOv3izvwFLzSGdWHl83OJM&new=1

实现点击按钮获取数据传送至datatable和datagridview;在通过datatable将数据传给excel文件。

 HtmlWeb htmlweb = new HtmlWeb();
            HtmlAgilityPack.HtmlDocument htmldoc = htmlweb.Load(this.textBox1.Text);
          //  HtmlNode htmlNode = htmldoc.DocumentNode.SelectSingleNode("//*[@id=\"__layout\"]/div/section/section[2]/ul/li[1]/div[2]/p[1]");
            //var dd =htmlNode.SelectNodes(@"p[1]");
            // HtmlAgilityPack.HtmlNodeCollection collection = htmldoc.DocumentNode.SelectNodes("//*[starts-with(@id,'__layout')]/div/section/section[2]/ul");
            //*[@id="__layout"]/div/section/section[1]
            //*[@id="__layout"]/div/section/section[1]/ul
            //*[@id="__layout"]/div/section/section[2]
            HtmlAgilityPack.HtmlNodeCollection collection = htmldoc.DocumentNode.SelectNodes("//*[starts-with(@id,'__layout')]/div/section/section[@class='search-list search-list']/ul");
             if (dt.Columns.Count == 0)
             {
                 DataColumn dc = new DataColumn("序号", Type.GetType("System.Int32"));
                 DataColumn dc1 = new DataColumn("名称", Type.GetType("System.String"));
                 DataColumn dc2 = new DataColumn("地址", Type.GetType("System.String"));
                 DataColumn dc3 = new DataColumn("规格", Type.GetType("System.String"));
                 DataColumn dc4 = new DataColumn("价格", Type.GetType("System.String"));
                 dt.Columns.Add(dc);
                 dt.Columns.Add(dc1);
                 dt.Columns.Add(dc2);
                 dt.Columns.Add(dc3);
                 dt.Columns.Add(dc4);
             }
            foreach(HtmlNode node in collection)
            {
                var li = node.ChildNodes;
                int number = li.Count;
                for (int i = 0; i < number;i++ )
                {
                    var div = li[i].ChildNodes[1];
                    var h2 = div.ChildNodes[0];
                    var paddr = div.ChildNodes[1];
                    var psize = div.ChildNodes[2];
                    var pprice = div.ChildNodes[3];
                    //把数据依次写到datatable中
                    string name = h2.InnerText;
                    string addr = paddr.InnerText;
                    string size = psize.InnerText;
                    string price = pprice.InnerText;

                    DataRow dr = dt.NewRow();
                    dr["序号"] = (i+1).ToString();
                    dr["名称"] = name;
                    dr["地址"] = addr;
                    dr["规格"] = size;
                    dr["价格"] = price;
                    dt.Rows.Add(dr);

                  
                }               
            }
            dataGridView1.DataSource = dt;

            if (dt == null)
                return;
            int rowcount = dt.Rows.Count;
            int columncount = dt.Columns.Count;
            int rowIndex = 1;
            int columnIndex = 0;

            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlApp.DefaultFilePath = "";
            xlApp.DisplayAlerts = true;
            xlApp.SheetsInNewWorkbook = 1;
            Workbook xlBook = xlApp.Workbooks.Add(true);

            //将DataTable的列名导入Excel表第一行
            foreach (DataColumn dcc in dt.Columns)
            {
                columnIndex++;
                xlApp.Cells[rowIndex, columnIndex] = dcc.ColumnName;
            }

            //将DataTable中的数据导入Excel中
            for (int i = 0; i < rowcount; i++)
            {
                rowIndex++;
                columnIndex = 0;
                for (int j = 0; j < columncount; j++)
                {
                    columnIndex++;
                    xlApp.Cells[rowIndex, columnIndex] = dt.Rows[i][j].ToString();
                }
            }
            String strFileName = textBox2.Text.Trim();
            xlBook.SaveCopyAs(strFileName);

        

     
       

       

 

你可能感兴趣的:(数据操作)