private void ExportToExcel_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Excel文件|*.xlsx|Excel 2003文件|*.xls"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { var fileName = saveFileDialog.FileName; BinaryFormatter bf = new BinaryFormatter(); FileStream fs = new FileStream("RawItem.bry", FileMode.Open); var dataList = bf.Deserialize(fs) as AnalyzeData[]; var excelItems = from list in dataList from item in list.Items select new { Title = item.CleanTitle, Text = item.CleanText, MediaName = item.ReproducedMediaName, Url = item.Url, PubDate = item.PubDate, Forward = item.CurrentCount == null?0:item.CurrentCount.ForwardCount, Reply = item.CurrentCount == null ? 0 : item.CurrentCount.ReplyCount, View = item.CurrentCount == null ? 0 : item.CurrentCount.ViewCount, }; Workbook book = new Workbook(); var sheet = book.Worksheets.Add("结果"); ExcelUtility.FillCollection(sheet, excelItems); book.Save(fileName); fs.Dispose(); } MessageBox.Show("导出成功"); }
private void SearchPeopleBtn_Click(object sender, EventArgs e) { SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "Excel文件|*.xlsx"; if (dialog.ShowDialog() == DialogResult.OK) { Workbook book = new Workbook(); List <SinaPeople> _items = new List <SinaPeople>(); var querys = WeiboPeopleSearchListbox.Items.Cast <PeopleSearchQuery>().ToArray(); foreach (var peopleSearchQuery in querys) { List <string> args = new List <string>(); args.Add("&age=39y"); args.Add("&age=40y"); List <string> gender = new List <string>(); gender.Add("&gender=man"); gender.Add("&gender=women"); foreach (string str1 in args) { foreach (string str2 in gender) { SinaPeople[] temp1 = Crawler.Core.Utility.WeiboUtility.SearchPeople( peopleSearchQuery.Keyword, peopleSearchQuery.StartPage, peopleSearchQuery.EndPage, peopleSearchQuery.AddtionQuery + str1 + str2, SearchCert.Cert, peopleSearchQuery.Age, peopleSearchQuery.Gender, peopleSearchQuery.Attr); SinaPeople[] temp2 = Crawler.Core.Utility.WeiboUtility.SearchPeople( peopleSearchQuery.Keyword, peopleSearchQuery.StartPage, peopleSearchQuery.EndPage, peopleSearchQuery.AddtionQuery + str1 + str2, SearchCert.Regular, peopleSearchQuery.Age, peopleSearchQuery.Gender, peopleSearchQuery.Attr); _items.AddRange(temp1); _items.AddRange(temp2); } Worksheet sheet = book.Worksheets.Add(peopleSearchQuery.Keyword + str1); ExcelUtility.FillCollection(sheet, _items.ToArray()); _items.Clear(); book.Save(dialog.FileName); } } book.Save(dialog.FileName); MessageBox.Show("导出成功"); } }
private void SearchWeiboBtn_Click(object sender, EventArgs e) { SaveFileDialog dialog = new SaveFileDialog(); Int32 saveCountDown = 1; dialog.Filter = "Excel文件|*.xlsx"; if (dialog.ShowDialog() == DialogResult.OK) { try { Workbook book = new Workbook(); var querys = WeiboSearchKeywordListbox.Items.Cast <WeiboSearchQuery>().ToArray(); foreach (var query in querys) { int count = 0; Worksheet sheet = book.Worksheets.Add(query.Keyword); var composer = QueryComposerFactory.GetQueryComposer(query); Boolean appendExcel = false; while (!composer.Empty) { var weiboSearchQuery = composer.Next(); List <Item> _items = new List <Item>(); Item[] items = null; // 如果请求失败则继续请求 while (true) { try { List <string> args = new List <string>(); args.Add("&age=39y"); args.Add("&age=40y"); List <string> gender = new List <string>(); gender.Add("&gender=man"); gender.Add("&gender=women"); foreach (string str1 in args) { foreach (string str2 in gender) { Item[] temp1 = Crawler.Core.Utility.WeiboUtility.Search( weiboSearchQuery.Keyword, weiboSearchQuery.StartPage, weiboSearchQuery.EndPage, out count, weiboSearchQuery.Option, true, weiboSearchQuery.isOrigin, weiboSearchQuery.StartDate, weiboSearchQuery.EndDate, weiboSearchQuery.AddtionQuery + str1 + str2); Item[] temp2 = Crawler.Core.Utility.WeiboUtility.Search( weiboSearchQuery.Keyword, weiboSearchQuery.StartPage, weiboSearchQuery.EndPage, out count, weiboSearchQuery.Option, false, weiboSearchQuery.isOrigin, weiboSearchQuery.StartDate, weiboSearchQuery.EndDate, weiboSearchQuery.AddtionQuery + str1 + str2); _items.AddRange(temp1); _items.AddRange(temp2); } } items = _items.ToArray(); break; } catch (Exception ee) { Logger.Warn("Weibo Search Request failed " + ee.ToString()); Thread.Sleep(TimeSpan.FromSeconds(10)); } } composer.ReportStatus(ref items, count); var projectionItem = items.Select( item => new { Title = item.CleanTitle, Text = item.CleanText, MediaName = item.ReproducedMediaName, Url = item.Url, PubDate = item.PubDate, Forward = item.CurrentCount == null ? 0 : item.CurrentCount.ForwardCount, Reply = item.CurrentCount == null ? 0 : item.CurrentCount.ReplyCount, View = item.CurrentCount == null ? 0 : item.CurrentCount.ViewCount, Source = item.Source, Author = item.AuthorName, Cert = item.AuthorCertificated.ToString(), Region = (int)item.Lat, }); ExcelUtility.FillCollection(sheet, projectionItem, appendExcel); appendExcel = true; saveCountDown--; if (saveCountDown <= 0) { saveCountDown = 1; book.Save(dialog.FileName); } } } book.Save(dialog.FileName); MessageBox.Show("导出成功"); } catch (Exception unexpected) { Logger.Error("SinaSearch failed " + unexpected.ToString()); throw; } } }