예제 #1
0
        private void buttonX2_Click(object sender, EventArgs e)
        {
            var selectedDataTable = standardTable.Clone();

            foreach (DataGridViewRow row in dataGridViewX1.SelectedRows)
            {
                if (row.DataBoundItem == null)
                {
                    continue;
                }

                var newrow = ((System.Data.DataRowView)(row.DataBoundItem)).Row.ItemArray;

                selectedDataTable.Rows.Add(newrow);
            }

            var dataString = DataTableSerializer.SerializeDataTableXml(selectedDataTable);

            SendToCMSForm f = new SendToCMSForm(dataString);

            f.StartPosition = FormStartPosition.CenterParent;
            f.ShowDialog();
        }
예제 #2
0
        private void btndownload_Click(object sender, EventArgs e)
        {
            var htmlDoc = webBrowser1.Document.Window.Frames["iframeResult"].Document;


            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(htmlDoc.Body.InnerHtml);
            var table = doc.DocumentNode.SelectSingleNode("//table[@class='GridTableContent']");

            if (table == null)
            {
                MessageBox.Show("列表不存在,请检查页面");
                return;
            }

            var keyword = doc.GetElementbyId("txtValue").GetAttributeValue("Value", "");

            HtmlAgilityPack.HtmlDocument docParent = new HtmlAgilityPack.HtmlDocument();
            docParent.LoadHtml(webBrowser1.Document.Body.InnerHtml);
            var TagElement = docParent.GetElementbyId("dbTag");
            var currentTag = TagElement.SelectSingleNode("./li[@class='recur']").InnerText;



            var rows = table.SelectNodes("./tbody/tr");

            if (rows != null && rows.Count > 1)
            {
                DataTable     dt         = new DataTable("中国知网");
                var           columns    = rows[0].SelectNodes("./td");
                List <string> columnList = new List <string>();

                foreach (HtmlNode column in columns)
                {
                    dt.Columns.Add(string.IsNullOrEmpty(column.InnerText) ? "序号" : column.InnerText);
                    columnList.Add(column.InnerText);
                }
                int rowIndex = 0;
                foreach (HtmlNode row in rows)
                {
                    if (rowIndex > 0)
                    {
                        var newRow = dt.NewRow();

                        var dataColumns = row.SelectNodes("./td");

                        for (int i = 0; i < dataColumns.Count; i++)
                        {
                            newRow[i] = dataColumns[i].InnerText;
                        }
                        dt.Rows.Add(newRow);
                    }
                    rowIndex += 1;
                }
                if (checkBoxX1.Checked)
                {
                    saveFileDialog1.Filter = "(*.xls)|*.xls";
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        NPOIHelper.DataTableToExcel(dt, "中国知网", saveFileDialog1.FileName);
                    }
                }



                switch (currentTag)
                {
                case "文献":
                    foreach (DataRow row in dt.Rows)
                    {
                        var newrow = standardTable.NewRow();
                        newrow["篇名"]  = row["题名"];
                        newrow["作者"]  = row["作者"];
                        newrow["刊名"]  = row["来源"];
                        newrow["年期"]  = row["发表时间"];
                        newrow["下载数"] = row["下载"];
                        standardTable.Rows.Add(newrow);
                    }
                    break;

                case "期刊":
                    foreach (DataRow row in dt.Rows)
                    {
                        var newrow = standardTable.NewRow();
                        newrow["篇名"]  = row["篇名"];
                        newrow["作者"]  = row["作者"];
                        newrow["刊名"]  = row["刊名"];
                        newrow["年期"]  = row["发表时间"];
                        newrow["下载数"] = row["下载"];
                        standardTable.Rows.Add(newrow);
                    }
                    break;

                case "博硕士":
                    foreach (DataRow row in dt.Rows)
                    {
                        var newrow = standardTable.NewRow();
                        newrow["篇名"]  = row["中文题名"];
                        newrow["作者"]  = row["作者"].ToString() + row["数据库"].ToString();
                        newrow["刊名"]  = row["学位授予单位"];
                        newrow["年期"]  = row["学位年度"];
                        newrow["下载数"] = row["下载"];
                        standardTable.Rows.Add(newrow);
                    }
                    break;

                case "会议":
                    foreach (DataRow row in dt.Rows)
                    {
                        var newrow = standardTable.NewRow();
                        newrow["篇名"]  = row["篇名"];
                        newrow["作者"]  = row["作者"];
                        newrow["刊名"]  = row["会议名称"];
                        newrow["年期"]  = row["时间"];
                        newrow["下载数"] = row["下载"];
                        standardTable.Rows.Add(newrow);
                    }
                    break;

                case "报纸":
                    foreach (DataRow row in dt.Rows)
                    {
                        var newrow = standardTable.NewRow();
                        newrow["篇名"]  = row["题名"];
                        newrow["作者"]  = row["作者"];
                        newrow["刊名"]  = row["报纸名称"];
                        newrow["年期"]  = row["日期"];
                        newrow["下载数"] = row["下载"];
                        standardTable.Rows.Add(newrow);
                    }
                    break;

                case "专利":
                    foreach (DataRow row in dt.Rows)
                    {
                        var newrow = standardTable.NewRow();
                        newrow["篇名"]  = row["专利名称"];
                        newrow["作者"]  = row["申请人"];
                        newrow["刊名"]  = row["数据库"];
                        newrow["年期"]  = row["申请日"];
                        newrow["下载数"] = row["下载"];
                        standardTable.Rows.Add(newrow);
                    }
                    break;

                default:
                    break;
                }
                var body = DataTableSerializer.SerializeDataTableXml(standardTable);

                var key = string.Join(",", columnList);
                var db1 = new Model1();
                foreach (DataRow item in standardTable.Rows)
                {
                    var title         = item["篇名"].ToString();
                    var author        = item["作者"].ToString();
                    var journalName   = item["刊名"].ToString();
                    var publishDate   = item["年期"].ToString();
                    var downloadCount = item["下载数"].ToString().Trim();

                    if (db1.Cnkis.FirstOrDefault(o => o.Title == title && o.Author == author) == null)
                    {
                        db1.Cnkis.Add(new Common.Cnki
                        {
                            KeyString       = key,
                            KeyWord         = keyword,
                            Category        = currentTag,
                            Title           = title,
                            Author          = author,
                            JournalName     = journalName,
                            PublishDate     = publishDate,
                            DownloadedCount = string.IsNullOrEmpty(downloadCount) ? 0 : int.Parse(downloadCount)
                        });
                    }
                }
                db1.SaveChanges();
                MessageBox.Show("下载完成");
            }
            else
            {
                MessageBox.Show("无数据可下载");
            }
        }