Advertisement
Venciity

ReadWordsWritePronunciation

Mar 5th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. using System.IO;
  2. using System.Linq;
  3. using System.Data;
  4. using OfficeOpenXml;
  5. using System.Collections.Generic;
  6. using System;
  7. using OpenQA.Selenium;
  8. using OpenQA.Selenium.Firefox;
  9.  
  10. namespace ConsoleApplication1
  11. {
  12.     public class Program
  13.     {
  14.         public static void Main()
  15.         {
  16.             var profile = new FirefoxProfile();
  17.             profile.AcceptUntrustedCertificates = true;
  18.             var browser = new FirefoxDriver(profile);
  19.             // Click the spell buton only one time
  20.             browser.Navigate().GoToUrl("http://dictionary.reference.com/browse/start");
  21.             var spellButton = browser.FindElementByClassName("pronounce-Spell-button");
  22.             spellButton.Click();
  23.  
  24.             var existingFile = new FileInfo("Dictionary.xlsx");
  25.             // Open and read the XlSX file.
  26.             using (var package = new ExcelPackage(existingFile))
  27.             {
  28.                 // Get the work book in the file
  29.                 ExcelWorkbook workBook = package.Workbook;
  30.                 if (workBook.Worksheets.Count > 0)
  31.                 {
  32.                     ExcelWorksheet sheet = workBook.Worksheets.First();
  33.                     int lastRow = sheet.Cells.Count() / 2;
  34.                     var cells = sheet.Cells[string.Format("B2:B{0}", lastRow)];
  35.                     var cellsToWrite = sheet.Cells[string.Format("A2:A{0}", lastRow)];
  36.                     int cellsCount = cells.Count();
  37.                     for (int i = 1; i < 200; i++)
  38.                     {
  39.                         var currentCell = cells[i, 2];
  40.                         var currentCellToWrite = cellsToWrite[i, 1];
  41.                         int currentCellWordsCount = currentCell.Text.Split().Count();
  42.                         if (currentCellWordsCount == 1)
  43.                         {
  44.                             browser.Navigate().GoToUrl("http://dictionary.reference.com/browse/" + currentCell.Value);
  45.                             try
  46.                             {
  47.                                 var element = browser.FindElementByClassName("ipapron");
  48.                                 Console.WriteLine($"{i}.{element.Text}");
  49.                                 currentCellToWrite.Value = element.Text;
  50.                             }
  51.                             catch (Exception)
  52.                             {
  53.                                 Console.WriteLine($"{i}.Error!");
  54.                                 currentCellToWrite.Style.Font.Bold = true;
  55.                                 //currentCellToWrite.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.LightGray;
  56.                                 //currentCellToWrite.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Red);
  57.                                 currentCellToWrite.Value = "#Error#";
  58.                             }
  59.  
  60.                         }
  61.                         else
  62.                         {
  63.                             Console.WriteLine($"{i}.*** words count > 1");
  64.                             currentCellToWrite.Style.Font.Bold = true;
  65.                             currentCellToWrite.Value = "***";
  66.                         }
  67.                     }
  68.  
  69.                     package.Save();
  70.                 }
  71.             }
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement