private static void SendAPIPushData(DTODataDetail item, DTOAPIData data) { if (!string.IsNullOrEmpty(item.FormatPushTitle) && !string.IsNullOrEmpty(item.FormatPushBody)) { using (var client = new HttpClient()) { Uri url = new Uri(item.LinkPush); client.BaseAddress = new Uri(url.Scheme + "://" + url.Authority); //client.BaseAddress = new Uri("http://localhost:57075"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.Timeout = TimeSpan.FromHours(0.1); DTOAPIPush dto = new DTOAPIPush(); dto.FileName = item.FileName; dto.SpreadsheetID = item.SpreadsheetID; dto.SpreadsheetName = item.SpreadsheetName; dto.FormatPushTitle = item.FormatPushTitle; dto.FormatPushBody = item.FormatPushBody; var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(dto), Encoding.UTF8, "application/json"); var response = client.PostAsync(url.AbsolutePath, content); if (response != null && response.Result.IsSuccessStatusCode) { HttpResponseMessage res = response.Result; } } } }
private static void SendAPIPush(DTODataDetail item, DTOAPIData data) { if (!string.IsNullOrEmpty(item.FormatPushTitle) && !string.IsNullOrEmpty(item.FormatPushBody)) { using (var client = new HttpClient()) { Uri url = new Uri(item.LinkPush); client.BaseAddress = new Uri(url.Scheme + "://" + url.Authority); //client.BaseAddress = new Uri("http://localhost:57075"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.Timeout = TimeSpan.FromHours(0.1); DTOAPIPush dto = new DTOAPIPush(); dto.FileName = item.FileName; List <int> lstRowID = data.ListCells.Select(c => c.Row).Distinct().ToList(); List <string> lstRowTitle = new List <string>(); Dictionary <int, List <string> > dicRowValue = new Dictionary <int, List <string> >(); foreach (var rowid in lstRowID) { lstRowTitle.Add(data.ListRowTitle[rowid]); List <string> lstCell = new List <string>(); foreach (var cell in data.ListCells.Where(c => c.Row == rowid)) { string title = data.ListColumnTitle[cell.Column]; lstCell.Add(string.Format("[{0}: {1}->{2}]", title, cell.ValueFrom, cell.ValueTo)); } dicRowValue.Add(rowid, lstCell); } dto.Title = DateTime.Now.ToString("HH:mm") + " " + string.Join(",", lstRowTitle); dto.Body = string.Empty; foreach (var itemRow in dicRowValue) { string title = data.ListRowTitle[itemRow.Key]; dto.Body += "," + title + string.Join("", itemRow.Value); } if (!string.IsNullOrEmpty(dto.Body)) { dto.Body = dto.Body.Substring(1); } var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(dto), Encoding.UTF8, "application/json"); var response = client.PostAsync(url.AbsolutePath, content); if (response != null && response.Result.IsSuccessStatusCode) { HttpResponseMessage res = response.Result; } } } }
public static void RunData(Action <string> actionlog, bool runtest = false) { if (Data != null) { if ((!Data.IsStop || runtest) && Data.ListDetails != null && Data.ListDetails.Count > 0) { foreach (var item in Data.ListDetails) { if (item.IsFile) { if (System.IO.File.Exists(item.UploadFolder)) { string filetemppath = System.IO.Path.Combine(FolderPath, item.FileName + "_temp" + System.IO.Path.GetExtension(item.UploadFolder)); if (System.IO.File.Exists(filetemppath)) { System.IO.File.Delete(filetemppath); } System.IO.File.Copy(item.UploadFolder, filetemppath); string filecurrent = System.IO.Path.Combine(FolderPath, item.FileName + System.IO.Path.GetExtension(item.UploadFolder)); if (System.IO.File.Exists(filecurrent)) { var itemCurrent = new DTOAPIData { FileName = item.FileName, SpreadsheetID = item.SpreadsheetID, SpreadsheetName = item.SpreadsheetName, ListColumnTitle = new List <string>(), ListRowTitle = new List <string>(), ListCells = new List <DTOAPIDataCell>() }; if (System.IO.Path.GetExtension(filetemppath).ToLower() == ".csv") { #region csv #region current using (var textReader = new System.IO.StreamReader(filecurrent)) { int row, col; while (itemCurrent.ListColumnTitle.Count < item.ColumnValueStart) { itemCurrent.ListColumnTitle.Add(string.Empty); } while (itemCurrent.ListRowTitle.Count < item.RowValueStart) { itemCurrent.ListRowTitle.Add(string.Empty); } var csv = new CsvHelper.CsvReader(textReader); row = 0; while (csv.Read()) { if (row <= item.RowValueEnd) { for (col = 0; col < 52 && col <= item.ColumnValueEnd; col++) { if (item.ColumnTitle == col + 1) { string strTitle = string.Empty; try { strTitle = csv[col]; } catch { } itemCurrent.ListRowTitle.Add(strTitle); } else if (item.RowTitle == row + 1 && col + 1 >= item.ColumnValueStart && col + 1 <= item.ColumnValueEnd) { string strTitle = string.Empty; try { strTitle = csv[col]; } catch { } itemCurrent.ListColumnTitle.Add(strTitle); } else if (row + 1 > item.RowTitle && col + 1 >= item.ColumnValueStart && col + 1 <= item.ColumnValueEnd) { string strValue = string.Empty; try { strValue = csv[col]; } catch { } itemCurrent.ListCells.Add(new DTOAPIDataCell { Row = row + 1, Column = col + 1, ValueFrom = strValue, ValueTo = strValue }); } } } else { break; } row++; } } #endregion #region next using (var textReader = new System.IO.StreamReader(filetemppath)) { int row, col; var csv = new CsvHelper.CsvReader(textReader); row = 0; while (csv.Read()) { if (row <= item.RowValueEnd) { for (col = 0; col < 52 && col <= item.ColumnValueEnd; col++) { if (item.ColumnTitle == col + 1) { itemCurrent.ListColumnTitle.Add(csv[col]); } else if (row + 1 > item.RowTitle && col + 1 >= item.ColumnValueStart && col + 1 <= item.ColumnValueEnd) { var itemCell = itemCurrent.ListCells.FirstOrDefault(c => c.Row == row + 1 && c.Column == col + 1); if (itemCell != null && itemCell.ValueTo != csv[col]) { itemCell.ValueTo = csv[col]; } } } } else { break; } row++; } } #endregion #endregion } else { #region excel using (var package = new OfficeOpenXml.ExcelPackage(new System.IO.FileInfo(filecurrent))) { var worksheet = HelperExcel.GetWorksheetFirst(package); #region current if (worksheet != null && worksheet.Dimension != null) { int row = 1, col = 1; while (itemCurrent.ListColumnTitle.Count < item.ColumnValueStart) { itemCurrent.ListColumnTitle.Add(string.Empty); } while (itemCurrent.ListRowTitle.Count < item.RowValueStart) { itemCurrent.ListRowTitle.Add(string.Empty); } for (row = item.RowValueStart; row <= item.RowValueEnd && row <= worksheet.Dimension.End.Row; row++) { var strTitle = HelperExcel.GetValue(worksheet, row, item.ColumnTitle); itemCurrent.ListRowTitle.Add(strTitle); } for (col = item.ColumnValueStart; col <= item.ColumnValueEnd && col <= worksheet.Dimension.End.Column; col++) { var strTitle = HelperExcel.GetValue(worksheet, item.RowTitle, col); itemCurrent.ListColumnTitle.Add(strTitle); } for (row = item.RowValueStart; row <= item.RowValueEnd && row <= worksheet.Dimension.End.Row; row++) { for (col = item.ColumnValueStart; col <= item.ColumnValueEnd && col <= worksheet.Dimension.End.Column; col++) { var strValue = HelperExcel.GetValue(worksheet, row, col); itemCurrent.ListCells.Add(new DTOAPIDataCell { Row = row, Column = col, ValueFrom = strValue, ValueTo = strValue }); } } } #endregion } using (var package = new OfficeOpenXml.ExcelPackage(new System.IO.FileInfo(filetemppath))) { var worksheet = HelperExcel.GetWorksheetFirst(package); #region next if (worksheet != null && worksheet.Dimension != null) { foreach (var itemCell in itemCurrent.ListCells) { if (itemCell.Row <= worksheet.Dimension.End.Row && itemCell.Column <= worksheet.Dimension.End.Column) { var strValue = HelperExcel.GetValue(worksheet, itemCell.Row, itemCell.Column); if (itemCell.ValueTo != strValue) { itemCell.ValueTo = strValue; } } } } #endregion } #endregion } itemCurrent.ListCells = itemCurrent.ListCells.Where(c => c.ValueTo != c.ValueFrom).ToList(); if (itemCurrent.ListCells.Count > 0) { System.IO.File.Delete(filecurrent); System.IO.File.Copy(filetemppath, filecurrent); itemCurrent.FileUpload = SendFile(item, item.FileName, filecurrent); if (!string.IsNullOrEmpty(itemCurrent.FileUpload)) { SendAPIData(item, itemCurrent); SendAPIPushData(item, itemCurrent); } else { actionlog("upload file fail " + item.UploadFolder); } } } else { System.IO.File.Copy(filetemppath, filecurrent); } } } else { if (System.IO.Directory.Exists(item.UploadFolder)) { string[] files = System.IO.Directory.GetFiles(item.UploadFolder); if (files.Length > 0) { string filetemppath = System.IO.Path.Combine(FolderPath, item.FileName + "_temp" + System.IO.Path.GetExtension(item.UploadFolder)); if (System.IO.File.Exists(filetemppath)) { System.IO.File.Delete(filetemppath); } System.IO.File.Copy(files[0], filetemppath); string filecurrent = System.IO.Path.Combine(FolderPath, item.FileName + System.IO.Path.GetExtension(filetemppath)); if (System.IO.File.Exists(filecurrent)) { var itemCurrent = new DTOAPIData { FileName = item.FileName, SpreadsheetID = item.SpreadsheetID, SpreadsheetName = item.SpreadsheetName, ListColumnTitle = new List <string>(), ListRowTitle = new List <string>(), ListCells = new List <DTOAPIDataCell>() }; if (System.IO.Path.GetExtension(filetemppath).ToLower() == ".csv") { #region csv #region current using (var textReader = new System.IO.StreamReader(filecurrent)) { int row, col; while (itemCurrent.ListColumnTitle.Count < item.ColumnValueStart) { itemCurrent.ListColumnTitle.Add(string.Empty); } while (itemCurrent.ListRowTitle.Count < item.RowValueStart) { itemCurrent.ListRowTitle.Add(string.Empty); } var csv = new CsvHelper.CsvReader(textReader); row = 0; while (csv.Read()) { if (row <= item.RowValueEnd) { for (col = 0; col < 52 && col <= item.ColumnValueEnd; col++) { if (item.ColumnTitle == col + 1) { string strTitle = string.Empty; try { strTitle = csv[col]; } catch { } itemCurrent.ListRowTitle.Add(strTitle); } else if (item.RowTitle == row + 1 && col + 1 >= item.ColumnValueStart && col + 1 <= item.ColumnValueEnd) { string strTitle = string.Empty; try { strTitle = csv[col]; } catch { } itemCurrent.ListColumnTitle.Add(strTitle); } else if (row + 1 > item.RowTitle && col + 1 >= item.ColumnValueStart && col + 1 <= item.ColumnValueEnd) { string strValue = string.Empty; try { strValue = csv[col]; } catch { } itemCurrent.ListCells.Add(new DTOAPIDataCell { Row = row + 1, Column = col + 1, ValueFrom = strValue, ValueTo = strValue }); } } } else { break; } row++; } } #endregion #region next using (var textReader = new System.IO.StreamReader(filetemppath)) { int row, col; var csv = new CsvHelper.CsvReader(textReader); row = 0; while (csv.Read()) { if (row <= item.RowValueEnd) { for (col = 0; col < 52 && col <= item.ColumnValueEnd; col++) { if (item.ColumnTitle == col + 1) { itemCurrent.ListColumnTitle.Add(csv[col]); } else if (row + 1 > item.RowTitle && col + 1 >= item.ColumnValueStart && col + 1 <= item.ColumnValueEnd) { var itemCell = itemCurrent.ListCells.FirstOrDefault(c => c.Row == row + 1 && c.Column == col + 1); if (itemCell != null && itemCell.ValueTo != csv[col]) { itemCell.ValueTo = csv[col]; } } } } else { break; } row++; } } #endregion #endregion } else { using (var package = new OfficeOpenXml.ExcelPackage(new System.IO.FileInfo(filecurrent))) { var worksheet = HelperExcel.GetWorksheetFirst(package); #region current if (worksheet != null && worksheet.Dimension != null) { int row = 1, col = 1; while (itemCurrent.ListColumnTitle.Count < item.ColumnValueStart) { itemCurrent.ListColumnTitle.Add(string.Empty); } while (itemCurrent.ListRowTitle.Count < item.RowValueStart) { itemCurrent.ListRowTitle.Add(string.Empty); } for (row = item.RowValueStart; row <= item.RowValueEnd && row <= worksheet.Dimension.End.Row; row++) { var strTitle = HelperExcel.GetValue(worksheet, row, item.ColumnTitle); itemCurrent.ListRowTitle.Add(strTitle); } for (col = item.ColumnValueStart; col <= item.ColumnValueEnd && col <= worksheet.Dimension.End.Column; col++) { var strTitle = HelperExcel.GetValue(worksheet, item.RowTitle, col); itemCurrent.ListColumnTitle.Add(strTitle); } for (row = item.RowValueStart; row <= item.RowValueEnd && row <= worksheet.Dimension.End.Row; row++) { for (col = item.ColumnValueStart; col <= item.ColumnValueEnd && col <= worksheet.Dimension.End.Column; col++) { var strValue = HelperExcel.GetValue(worksheet, row, col); itemCurrent.ListCells.Add(new DTOAPIDataCell { Row = row, Column = col, ValueFrom = strValue, ValueTo = strValue }); } } } #endregion } using (var package = new OfficeOpenXml.ExcelPackage(new System.IO.FileInfo(filetemppath))) { var worksheet = HelperExcel.GetWorksheetFirst(package); #region next if (worksheet != null && worksheet.Dimension != null) { foreach (var itemCell in itemCurrent.ListCells) { if (itemCell.Row <= worksheet.Dimension.End.Row && itemCell.Column <= worksheet.Dimension.End.Column) { var strValue = HelperExcel.GetValue(worksheet, itemCell.Row, itemCell.Column); if (itemCell.ValueTo != strValue) { itemCell.ValueTo = strValue; } } } } #endregion } } itemCurrent.ListCells = itemCurrent.ListCells.Where(c => c.ValueTo != c.ValueFrom).ToList(); if (itemCurrent.ListCells.Count > 0) { System.IO.File.Delete(filecurrent); System.IO.File.Copy(filetemppath, filecurrent); itemCurrent.FileUpload = SendFile(item, item.FileName, filecurrent); SendAPIData(item, itemCurrent); SendAPIPushData(item, itemCurrent); } } else { System.IO.File.Copy(filetemppath, filecurrent); } if (System.IO.File.Exists(files[0])) { System.IO.File.Delete(files[0]); } } } else { actionlog("not found folder " + item.UploadFolder); } } } } else { actionlog(string.Format("IsStop: {0}, ListDetails: {1}", Data.IsStop, Data.ListDetails == null ? 0 : Data.ListDetails.Count)); } } else { actionlog("not found data"); } }