| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- using Microsoft.AspNetCore.Mvc;
- using SuperCms.Models;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using SuperCms.Controllers;
- using Syncfusion.XlsIO;
- using System.Text;
- namespace SuperCms.Controllers
- {
- public class RegistryController : BaseController
- {
- public async Task<IActionResult> IndexAsync(string txtThoigian, string txtLoaiPT, string cboTinh)
- {
- try
- {
- RegistryViewModel model = new RegistryViewModel();
- if (txtThoigian == null || txtLoaiPT == null || cboTinh == null)
- {
- return View(model);
- }
- string endPoint = "http://app.vr.org.vn/ptpublic/quahankd.aspx";
- var client = new HttpClient();
- // get valid key
- WebRequest request = HttpWebRequest.Create(endPoint);
- WebResponse response1 = request.GetResponse();
- StreamReader reader = new StreamReader(response1.GetResponseStream());
- string responseText = reader.ReadToEnd();
- bool loop = true;
- int i = 0;
- var tableHTML = "";
- var table = "";
- string old = "";
- List<KeyValuePair<string, string>> data = new List<KeyValuePair<string, string>>();
- List<KeyValuePair<string, string>> dataOld = new List<KeyValuePair<string, string>>();
- while (loop)
- {
- //if (i == 0)
- //{
- // data.Add(new KeyValuePair<string, string>("__EVENTTARGET", ""));
- // data.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
- // data.Add(new KeyValuePair<string, string>("cmdTraCuu", "Tra cứu"));
- //}
- //else
- //{
- // data.Add(new KeyValuePair<string, string>("__EVENTTARGET", "DGTimPT$ctl54$ctl" + (i < 10 ? "0" + i.ToString() : i.ToString())));
- // data.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
- //}
- using (var httpClient = new HttpClient())
- {
- if (i == 0)
- {
- List<KeyValuePair<string, string>> dataGet = UtilsController.ParseDataRequest(responseText);
- dataGet.Add(new KeyValuePair<string, string>("cboTinh", cboTinh));
- dataGet.Add(new KeyValuePair<string, string>("txtLoaiPT", txtLoaiPT));
- dataGet.Add(new KeyValuePair<string, string>("txtThoigian", txtThoigian));
- dataGet.Add(new KeyValuePair<string, string>("__EVENTTARGET", ""));
- dataGet.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
- dataGet.Add(new KeyValuePair<string, string>("cmdTraCuu", "Tra cứu"));
- data = dataGet;
- }
- else
- {
- dataOld.Add(new KeyValuePair<string, string>("__EVENTTARGET", "DGTimPT$ctl54$ctl" + (i < 10 ? "0" + i.ToString() : i.ToString())));
- dataOld.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
- dataOld.Add(new KeyValuePair<string, string>("cboTinh", cboTinh));
- dataOld.Add(new KeyValuePair<string, string>("txtLoaiPT", txtLoaiPT));
- dataOld.Add(new KeyValuePair<string, string>("txtThoigian", txtThoigian));
- data = dataOld;
- }
- using (var content = new FormUrlEncodedContent(data))
- {
- content.Headers.Clear();
- content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
- HttpResponseMessage response = await httpClient.PostAsync(endPoint, content);
- var result = response.Content.ReadAsStringAsync().Result;
- dataOld = UtilsController.ParseDataRequest(result);
- if (old != null)
- {
- if (old.Length == result.Length)
- {
- loop = false;
- continue;
- }
- }
- old = result;
- Regex regex1 = new Regex("<table cell[^>]*>[\\s\\S]*?<\\/table>", RegexOptions.IgnoreCase);
- Match match1;
- for (match1 = regex1.Match(result); match1.Success; match1 = match1.NextMatch())
- {
- var result2 = match1.Groups[0].Value;
- Regex regex2 = new Regex("<tr[^>]*>[\\s\\S]*?<\\/tr>", RegexOptions.IgnoreCase);
- Match match2;
- int count = 0;
- for (match2 = regex2.Match(result2); match2.Success; match2 = match2.NextMatch())
- {
- if (count > 0)
- {
- if (!match2.Groups[0].Value.Contains("DGTimPT$ctl"))
- {
- var d = match2.Groups[0].Value;
- table = table + d;
- }
- }
- count++;
- }
- }
- }
- }
- i++;
- }
- //tableHTML = "<html>" + "<body>" +
- // "<table cellspacing=\"0\" width=\"500\" style=\"table-layout:fixed; border-collapse:collapse; width: 500pt\">" +
- // @"<tr><td> Số TT </td>
- // <td> Biển đăng ký</td>
- // <td> Ngày đăng ký</td>
- // <td> Nhãn hiệu </td>
- // <td> Chủ phương tiên</td>
- // <td> Địa chỉ </td>
- // <td> Ngày kiểm định</td>
- // <td> Đơn vị KĐ</td>
- // <td> Số GCN KĐ</td>
- // <td> Thời hạn GCN</td>
- // </tr>" +
- // table +
- // @"</table>" +
- // "</body></html>";
- //String tableHTMLNew = tableHTML.Replace("&", "&");
- //MemoryStream ms = new MemoryStream();
- //using (ExcelEngine excelEngine = new ExcelEngine())
- //{
- // // Step 2 : Instantiate the Excel application object.
- // IApplication application = excelEngine.Excel;
- // application.DefaultVersion = ExcelVersion.Excel2016;
- // // A workbook is created.
- // IWorkbook workbook = application.Workbooks.Create(1);
- // // The first worksheet object in the worksheets collection is accessed.
- // IWorksheet worksheet = workbook.Worksheets[0];
- // byte[] byteArray = Encoding.UTF8.GetBytes(tableHTMLNew);
- // MemoryStream file = new MemoryStream(byteArray);
- // // Imports HTML table into the worksheet from first row and first column.
- // worksheet.ImportHtmlTable(file, 1, 1);
- // worksheet.UsedRange.AutofitColumns();
- // worksheet.UsedRange.AutofitRows();
- // workbook.SaveAs(ms);
- // ms.Position = 0;
- //}
- //return File(ms, "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Export-HTML-Table.xlsx");
- model.table = table;
- return View(model);
- }
- catch (Exception ex)
- {
- return Redirect("/Home");
- }
- }
- public async Task<IActionResult> GetDataAsync(string txtThoigian, string txtLoaiPT, string cboTinh)
- {
- try
- {
- string endPoint = "http://app.vr.org.vn/ptpublic/quahankd.aspx";
- var client = new HttpClient();
- // get valid key
- WebRequest request = HttpWebRequest.Create(endPoint);
- WebResponse response1 = request.GetResponse();
- StreamReader reader = new StreamReader(response1.GetResponseStream());
- string responseText = reader.ReadToEnd();
- bool loop = true;
- int i = 0;
- var tableHTML = "";
- var table = "";
- string old = "";
- List<KeyValuePair<string, string>> data = new List<KeyValuePair<string, string>>();
- List<KeyValuePair<string, string>> dataOld = new List<KeyValuePair<string, string>>();
- while (loop)
- {
- //if (i == 0)
- //{
- // data.Add(new KeyValuePair<string, string>("__EVENTTARGET", ""));
- // data.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
- // data.Add(new KeyValuePair<string, string>("cmdTraCuu", "Tra cứu"));
- //}
- //else
- //{
- // data.Add(new KeyValuePair<string, string>("__EVENTTARGET", "DGTimPT$ctl54$ctl" + (i < 10 ? "0" + i.ToString() : i.ToString())));
- // data.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
- //}
- using (var httpClient = new HttpClient())
- {
- if (i == 0)
- {
- List<KeyValuePair<string, string>> dataGet = UtilsController.ParseDataRequest(responseText);
- dataGet.Add(new KeyValuePair<string, string>("cboTinh", cboTinh));
- dataGet.Add(new KeyValuePair<string, string>("txtLoaiPT", txtLoaiPT));
- dataGet.Add(new KeyValuePair<string, string>("txtThoigian", txtThoigian));
- dataGet.Add(new KeyValuePair<string, string>("__EVENTTARGET", ""));
- dataGet.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
- dataGet.Add(new KeyValuePair<string, string>("cmdTraCuu", "Tra cứu"));
- data = dataGet;
- }
- else
- {
- dataOld.Add(new KeyValuePair<string, string>("__EVENTTARGET", "DGTimPT$ctl54$ctl" + (i < 10 ? "0" + i.ToString() : i.ToString())));
- dataOld.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
- dataOld.Add(new KeyValuePair<string, string>("cboTinh", cboTinh));
- dataOld.Add(new KeyValuePair<string, string>("txtLoaiPT", txtLoaiPT));
- dataOld.Add(new KeyValuePair<string, string>("txtThoigian", txtThoigian));
- data = dataOld;
- }
- using (var content = new FormUrlEncodedContent(data))
- {
- content.Headers.Clear();
- content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
- HttpResponseMessage response = await httpClient.PostAsync(endPoint, content);
- var result = response.Content.ReadAsStringAsync().Result;
- dataOld = UtilsController.ParseDataRequest(result);
- if (old != null)
- {
- if (old.Length == result.Length)
- {
- loop = false;
- continue;
- }
- }
- old = result;
- Regex regex1 = new Regex("<table cell[^>]*>[\\s\\S]*?<\\/table>", RegexOptions.IgnoreCase);
- Match match1;
- for (match1 = regex1.Match(result); match1.Success; match1 = match1.NextMatch())
- {
- var result2 = match1.Groups[0].Value;
- Regex regex2 = new Regex("<tr[^>]*>[\\s\\S]*?<\\/tr>", RegexOptions.IgnoreCase);
- Match match2;
- int count = 0;
- for (match2 = regex2.Match(result2); match2.Success; match2 = match2.NextMatch())
- {
- if (count > 0)
- {
- if (!match2.Groups[0].Value.Contains("DGTimPT$ctl"))
- {
- var d = match2.Groups[0].Value;
- table = table + d;
- }
- }
- count++;
- }
- }
- }
- }
- i++;
- }
- //tableHTML = "<html>" + "<body>" +
- // "<table cellspacing=\"0\" width=\"500\" style=\"table-layout:fixed; border-collapse:collapse; width: 500pt\">" +
- // @"<tr><td> Số TT </td>
- // <td> Biển đăng ký</td>
- // <td> Ngày đăng ký</td>
- // <td> Nhãn hiệu </td>
- // <td> Chủ phương tiên</td>
- // <td> Địa chỉ </td>
- // <td> Ngày kiểm định</td>
- // <td> Đơn vị KĐ</td>
- // <td> Số GCN KĐ</td>
- // <td> Thời hạn GCN</td>
- // </tr>" +
- // table +
- // @"</table>" +
- // "</body></html>";
- //String tableHTMLNew = tableHTML.Replace("&", "&");
- //MemoryStream ms = new MemoryStream();
- //using (ExcelEngine excelEngine = new ExcelEngine())
- //{
- // // Step 2 : Instantiate the Excel application object.
- // IApplication application = excelEngine.Excel;
- // application.DefaultVersion = ExcelVersion.Excel2016;
- // // A workbook is created.
- // IWorkbook workbook = application.Workbooks.Create(1);
- // // The first worksheet object in the worksheets collection is accessed.
- // IWorksheet worksheet = workbook.Worksheets[0];
- // byte[] byteArray = Encoding.UTF8.GetBytes(tableHTMLNew);
- // MemoryStream file = new MemoryStream(byteArray);
- // // Imports HTML table into the worksheet from first row and first column.
- // worksheet.ImportHtmlTable(file, 1, 1);
- // worksheet.UsedRange.AutofitColumns();
- // worksheet.UsedRange.AutofitRows();
- // workbook.SaveAs(ms);
- // ms.Position = 0;
- //}
- //return File(ms, "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Export-HTML-Table.xlsx");
- RegistryViewModel model = new RegistryViewModel();
- model.table = tableHTML;
- return RedirectToAction("/Index", model);
- }
- catch (Exception ex)
- {
- return Redirect("/Home");
- }
- }
- }
- }
|