IPEasyUtility.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Linq;
  3. using OpenQA.Selenium;
  4. namespace wispro.sp.utility
  5. {
  6. public class IPEasyUtility
  7. {
  8. public static void DownloadReport(string strId,string filename)
  9. {
  10. string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
  11. bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
  12. string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
  13. string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
  14. OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
  15. options.AddUserProfilePreference("download.default_directory", strFileSavePath);
  16. options.AddUserProfilePreference("intl.accept_languages", "nl");
  17. options.AddUserProfilePreference("disable-popup-blocking", "true");
  18. if (isheadless)
  19. {
  20. options.AddArgument("headless");
  21. }
  22. using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
  23. {
  24. driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(50);
  25. driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(500);
  26. //进入登录界面
  27. driver.Navigate().GoToUrl("http://47.106.221.167/Login.aspx");
  28. //输入用户名和密码
  29. driver.FindElement(By.Id("txtUser")).SendKeys(Account);
  30. driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
  31. //点击登录按钮
  32. driver.FindElement(By.Id("btnLogin")).Click();
  33. //关闭提示遮罩层
  34. driver.FindElement(By.Id("jpwClose")).Click();
  35. //点击顶部菜单栏中的报表管理菜单
  36. driver.FindElement(By.Name("970d33d5-c728-41b8-a060-4330610706b9")).Click();
  37. //点击左侧 自定义报表 菜单
  38. driver.FindElement(By.Name("642fa96f-1e1f-46fd-aaa4-cb461ee8df5b")).Click();
  39. //切换到自定义报表Frame
  40. driver.SwitchTo().Frame(1);
  41. //调用报表导出JS
  42. ((IJavaScriptExecutor)driver).ExecuteScript($"Report.Export('{strId}');");
  43. //切换到弹出的导出报表窗口,点击导出按钮
  44. driver.SwitchTo().DefaultContent();
  45. var ihg_export = driver.FindElement(By.Name("ihg_export"));
  46. driver.SwitchTo().Frame(ihg_export);
  47. driver.FindElement(By.Id("btnSubmit")).Click();
  48. string strFilePath = System.IO.Path.Combine(strFileSavePath, filename);
  49. while (true)
  50. {
  51. System.IO.FileInfo file = new System.IO.FileInfo(strFilePath);
  52. if (!file.Exists || file.Length == 0) {
  53. System.Threading.Thread.Sleep(5000);
  54. }
  55. else
  56. {
  57. break;
  58. }
  59. }
  60. }
  61. }
  62. /// <summary>
  63. /// 根据报表名称导出报表
  64. /// </summary>
  65. /// <param name="ReportName">报告名称</param>
  66. /// <param name="isModifyDate">是否手动修改日期范围</param>
  67. public static void DownloadReport(string ReportName,bool isModifyDate)
  68. {
  69. string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
  70. bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
  71. string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
  72. string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
  73. OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
  74. options.AddUserProfilePreference("download.default_directory", strFileSavePath);
  75. options.AddUserProfilePreference("intl.accept_languages", "nl");
  76. options.AddUserProfilePreference("disable-popup-blocking", "true");
  77. if (isheadless)
  78. {
  79. options.AddArgument("headless");
  80. }
  81. using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
  82. {
  83. driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(50);
  84. driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(500);
  85. //进入登录界面
  86. driver.Navigate().GoToUrl("http://47.106.221.167/Login.aspx");
  87. //输入用户名和密码
  88. driver.FindElement(By.Id("txtUser")).SendKeys(Account);
  89. driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
  90. //点击登录按钮
  91. driver.FindElement(By.Id("btnLogin")).Click();
  92. //关闭提示遮罩层
  93. driver.FindElement(By.Id("jpwClose")).Click();
  94. //点击顶部菜单栏中的报表管理菜单
  95. driver.FindElement(By.Name("970d33d5-c728-41b8-a060-4330610706b9")).Click();
  96. //点击左侧 自定义报表 菜单
  97. driver.FindElement(By.Name("642fa96f-1e1f-46fd-aaa4-cb461ee8df5b")).Click();
  98. //切换到自定义报表Frame
  99. driver.SwitchTo().Frame(1);
  100. var inputSearch = driver.FindElement(By.Id("customizedList_TxtSheetSearchKey"));
  101. inputSearch.SendKeys(ReportName);
  102. var btnSearch = driver.FindElement(By.ClassName("btn-search"));
  103. btnSearch.Click();
  104. var reportRecord = driver.FindElement(By.XPath($"//td[contains(text(),'{ReportName}')]"));
  105. reportRecord.Click();
  106. System.Threading.Thread.Sleep(1000);
  107. if (isModifyDate)
  108. {
  109. var btnEdit = driver.FindElement(By.ClassName("edit"));
  110. btnEdit.Click();
  111. System.Threading.Thread.Sleep(500);
  112. var doItemFinished = driver.FindElement(By.XPath("//span[contains(@objcvalue,'_date') and contains(@id,'ST;')]"));
  113. doItemFinished.Click();
  114. System.Threading.Thread.Sleep(500);
  115. var startDate = driver.FindElement(By.XPath("//input[contains(@id,'dt_s_')]"));
  116. startDate.Click();
  117. System.Threading.Thread.Sleep(500);
  118. startDate.SendKeys($"{DateTime.Now.AddMonths(-1).Year }-{DateTime.Now.AddMonths(-1).Month}-01");
  119. var endDate = driver.FindElement(By.XPath("//input[contains(@id,'dt_e_')]"));//.Id("dt_e_4F8FE88D-9040-45F1-9723-45699BCD4CAF"));
  120. endDate.Click();
  121. System.Threading.Thread.Sleep(500);
  122. endDate.SendKeys(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1).ToString("yyyy-MM-dd"));
  123. var addDate = driver.FindElement(By.XPath("//a[contains(@onclick,'ReportSub.ConditionDTSelect')]"));
  124. addDate.Click();
  125. System.Threading.Thread.Sleep(500);
  126. var btnSave = driver.FindElement(By.Id("save"));
  127. btnSave.Click();
  128. System.Threading.Thread.Sleep(3000);
  129. }
  130. var linkExport = driver.FindElement(By.XPath("//a[contains(@onclick,'-') and @class='tbexcel']"));
  131. linkExport.Click();
  132. //切换到弹出的导出报表窗口,点击导出按钮
  133. driver.SwitchTo().DefaultContent();
  134. var ihg_export = driver.FindElement(By.Name("ihg_export"));
  135. driver.SwitchTo().Frame(ihg_export);
  136. driver.FindElement(By.Id("btnSubmit")).Click();
  137. string strFilePath = System.IO.Path.Combine(strFileSavePath, $"{ReportName.Trim()}.xlsx");
  138. while (true)
  139. {
  140. System.IO.FileInfo file = new System.IO.FileInfo(strFilePath);
  141. if (!file.Exists || file.Length == 0)
  142. {
  143. System.Threading.Thread.Sleep(5000);
  144. }
  145. else
  146. {
  147. break;
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }