public static void saveDate(OutputData od) { Form1.programStatus = "сохраняю данные в эксель (" + od.events.Count + " событий)"; Excel.Application app; Excel.Workbooks wbs; Excel.Workbook wb; object misval = System.Reflection.Missing.Value; app = new Excel.Application(); wb = app.Workbooks.Add(); Excel.Worksheet wsheet = (Excel.Worksheet)wb.Sheets.Add(); for (int i = 1; i <= od.events.Count; i++) { wsheet.Cells[i, 1] = od.events[i - 1].Date; wsheet.Cells[i, 2] = od.events[i - 1].Time; wsheet.Cells[i, 3] = od.events[i - 1].Country; wsheet.Cells[i, 4] = od.events[i - 1].Importance; wsheet.Cells[i, 5] = od.events[i - 1].Title; wsheet.Cells[i, 6] = od.events[i - 1].Additional; wsheet.Cells[i, 7] = od.events[i - 1].Fact; wsheet.Cells[i, 8] = od.events[i - 1].Predict; wsheet.Cells[i, 9] = od.events[i - 1].Before; } string dir = Directory.GetCurrentDirectory() + "/Results/"; if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } app.ActiveWorkbook.SaveAs(dir + "investParser_" + DateTime.Now.ToLongTimeString().Replace(":", "-") + ".xls", Excel.XlFileFormat.xlWorkbookNormal); wb.Close(); app.Quit(); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wsheet); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wb); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(app); GC.Collect(); GC.WaitForPendingFinalizers(); Form1.programStatus = od.events.Count.ToString() + " событий записано. ищите файл в папке с программой"; }
public static void parseSaveData(List <string> htmls, CancellationToken token) { Task <OutputData>[] tasks = new Task <OutputData> [htmls.Count]; for (int i = 0; i < htmls.Count; i++) { int ind = i; tasks[ind] = new Task <OutputData>(() => parseEvents(htmls[ind], token)); } int ready = 0; int threads = 0; while (ready < htmls.Count && !token.IsCancellationRequested) { if (threads < 4) { tasks[ready++].Start(); threads++; } else { Task.WaitAny(tasks); threads--; Form1.programStatus = String.Format("обрабатываю данные ({0}/{1})", ready, htmls.Count); } } if (token.IsCancellationRequested) { return; } Task.WaitAll(tasks); OutputData od = new OutputData(); foreach (var t in tasks) { od.events.AddRange(t.Result.events); } ExcelWriter.saveDate(od); }
public static OutputData parseEvents(string html, CancellationToken token) { OutputData OD = new OutputData(); try { HtmlAgilityPack.HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); var tbody = doc.DocumentNode.Element("table").Element("tbody"); var trs = tbody.Elements("tr"); string date = ""; foreach (var tr in trs) { if (token.IsCancellationRequested) { break; } try { try { if (tr.Element("td").Attributes["class"].Value == "theDay") { date = tr.Element("td").InnerText.Trim(); continue; } } catch { } string time = "", country = "", imp = "", title = "", fact = "", forecast = "", prev = "", add = ""; var tds = tr.SelectNodes("td"); time = tds[0].InnerText; country = tds[1].Element("span").Attributes["title"].Value; try { imp = tds[2].SelectNodes("i[@class='newSiteIconsSprite grayFullBullishIcon middle']").Count.ToString(); } catch { imp = tds[2].InnerText.Trim(); } title = tds[3].InnerText.Trim(); title = title.Replace(" ", "").Trim(); try { add = tds[3].Element("span").Attributes["title"].Value; } catch { } try { fact = tds[4].InnerText.Trim(); fact = fact.Replace(" ", ""); forecast = tds[5].InnerText.Trim(); forecast = forecast.Replace(" ", ""); prev = tds[6].InnerText.Trim(); prev = prev.Replace(" ", ""); } catch { } OD.addEvent(date, time, country, imp, title, "", fact, forecast, prev, add); } catch (Exception e) { //Logging.writeLog(e.Message); //Logging.writeLog(e.StackTrace); } } } catch (Exception ex) { Logging.writeLog(ex.Message); Logging.writeLog(ex.StackTrace); } return(OD); }