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 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> data = new List>(); List> dataOld = new List>(); while (loop) { //if (i == 0) //{ // data.Add(new KeyValuePair("__EVENTTARGET", "")); // data.Add(new KeyValuePair("__EVENTARGUMENT", "")); // data.Add(new KeyValuePair("cmdTraCuu", "Tra cứu")); //} //else //{ // data.Add(new KeyValuePair("__EVENTTARGET", "DGTimPT$ctl54$ctl" + (i < 10 ? "0" + i.ToString() : i.ToString()))); // data.Add(new KeyValuePair("__EVENTARGUMENT", "")); //} using (var httpClient = new HttpClient()) { if (i == 0) { List> dataGet = UtilsController.ParseDataRequest(responseText); dataGet.Add(new KeyValuePair("cboTinh", cboTinh)); dataGet.Add(new KeyValuePair("txtLoaiPT", txtLoaiPT)); dataGet.Add(new KeyValuePair("txtThoigian", txtThoigian)); dataGet.Add(new KeyValuePair("__EVENTTARGET", "")); dataGet.Add(new KeyValuePair("__EVENTARGUMENT", "")); dataGet.Add(new KeyValuePair("cmdTraCuu", "Tra cứu")); data = dataGet; } else { dataOld.Add(new KeyValuePair("__EVENTTARGET", "DGTimPT$ctl54$ctl" + (i < 10 ? "0" + i.ToString() : i.ToString()))); dataOld.Add(new KeyValuePair("__EVENTARGUMENT", "")); dataOld.Add(new KeyValuePair("cboTinh", cboTinh)); dataOld.Add(new KeyValuePair("txtLoaiPT", txtLoaiPT)); dataOld.Add(new KeyValuePair("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("]*>[\\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("]*>[\\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 = "" + "" + // "
" + // @" // // // // // // // // // // " + // table + // @"
Số TT Biển đăng ký Ngày đăng ký Nhãn hiệu Chủ phương tiên Địa chỉ Ngày kiểm định Đơn vị KĐ Số GCN KĐ Thời hạn GCN
" + // ""; //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 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> data = new List>(); List> dataOld = new List>(); while (loop) { //if (i == 0) //{ // data.Add(new KeyValuePair("__EVENTTARGET", "")); // data.Add(new KeyValuePair("__EVENTARGUMENT", "")); // data.Add(new KeyValuePair("cmdTraCuu", "Tra cứu")); //} //else //{ // data.Add(new KeyValuePair("__EVENTTARGET", "DGTimPT$ctl54$ctl" + (i < 10 ? "0" + i.ToString() : i.ToString()))); // data.Add(new KeyValuePair("__EVENTARGUMENT", "")); //} using (var httpClient = new HttpClient()) { if (i == 0) { List> dataGet = UtilsController.ParseDataRequest(responseText); dataGet.Add(new KeyValuePair("cboTinh", cboTinh)); dataGet.Add(new KeyValuePair("txtLoaiPT", txtLoaiPT)); dataGet.Add(new KeyValuePair("txtThoigian", txtThoigian)); dataGet.Add(new KeyValuePair("__EVENTTARGET", "")); dataGet.Add(new KeyValuePair("__EVENTARGUMENT", "")); dataGet.Add(new KeyValuePair("cmdTraCuu", "Tra cứu")); data = dataGet; } else { dataOld.Add(new KeyValuePair("__EVENTTARGET", "DGTimPT$ctl54$ctl" + (i < 10 ? "0" + i.ToString() : i.ToString()))); dataOld.Add(new KeyValuePair("__EVENTARGUMENT", "")); dataOld.Add(new KeyValuePair("cboTinh", cboTinh)); dataOld.Add(new KeyValuePair("txtLoaiPT", txtLoaiPT)); dataOld.Add(new KeyValuePair("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("]*>[\\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("]*>[\\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 = "" + "" + // "
" + // @" // // // // // // // // // // " + // table + // @"
Số TT Biển đăng ký Ngày đăng ký Nhãn hiệu Chủ phương tiên Địa chỉ Ngày kiểm định Đơn vị KĐ Số GCN KĐ Thời hạn GCN
" + // ""; //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"); } } } }