private void TableCreator() { FileInfo file = new FileInfo(path); int exception = 0; try { using (ExcelPackage package = new ExcelPackage(file)) { ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; int rowCount = worksheet.Dimension.Rows; for (int row = 1; row <= rowCount; row++) { Wholesaler_AzImporter az = new Wholesaler_AzImporter(); exception++; if (exception == 10) { } if (row != 1) { az.id = row - 1; az.Sku = worksheet.Cells[row, 1].Value?.ToString().ToUpper(); az.Category = worksheet.Cells[row, 2].Value?.ToString(); az.ItemName = worksheet.Cells[row, 3].Value?.ToString(); az.Image1 = worksheet.Cells[row, 4].Value?.ToString(); az.Image2 = worksheet.Cells[row, 5].Value?.ToString(); az.Image3 = worksheet.Cells[row, 6].Value?.ToString(); az.Image4 = worksheet.Cells[row, 7].Value?.ToString(); az.Image5 = worksheet.Cells[row, 8].Value?.ToString(); az.Image6 = worksheet.Cells[row, 9].Value?.ToString(); az.Image7 = worksheet.Cells[row, 10].Value?.ToString(); az.Image8 = worksheet.Cells[row, 11].Value?.ToString(); az.MainImage = worksheet.Cells[row, 12].Value?.ToString(); az.WholeSale = Convert.ToDouble(worksheet.Cells[row, 13].Value?.ToString()); az.Quantity = Convert.ToInt32(worksheet.Cells[row, 14]?.Value); az.ShortDescription = worksheet.Cells[row, 25].Value?.ToString(); az.Weight = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(worksheet.Cells[row, 15]?.Value))); az.HTMLDescription = worksheet.Cells[row, 24].Value?.ToString(); if (!azImportItems.TryAdd(az.Sku, az)) { azImportItems[az.Sku].Quantity = az.Quantity; } else { azImportItems.TryAdd(az.Sku, az); } } } } } catch (Exception ex) { throw (ex); } }
public bool isAzImporter(string sku) { string internalSku = sku.ToUpper(); for (int i = 1; i < sku.Length; i++) { if (azImporterList.ContainsKey(internalSku.ToUpper())) { Wholesaler_AzImporter a = new Wholesaler_AzImporter(); azImporterList.TryGetValue(internalSku.ToUpper(), out a); azImporter = a; return(true); } else { internalSku = sku.Substring(0, sku.Length - i); } } return(false); }
public void TableExecutor() { TableCreator(); int bulkSize = 1; int exception = 0; foreach (var az in azImportItems) { exception++; Wholesaler_AzImporter azImporter = new Wholesaler_AzImporter(); azImporter.id = bulkSize; azImporter.Sku = az.Value.Sku; azImporter.Category = az.Value.Category; azImporter.ItemName = az.Value.ItemName; azImporter.Image1 = az.Value.Image1; azImporter.Image2 = az.Value.Image2; azImporter.Image3 = az.Value.Image3; azImporter.Image4 = az.Value.Image4; azImporter.Image5 = az.Value.Image5; azImporter.Image6 = az.Value.Image6; azImporter.Image7 = az.Value.Image7; azImporter.Image8 = az.Value.Image8; azImporter.MainImage = az.Value.MainImage; azImporter.WholeSale = Convert.ToDouble(az.Value.WholeSale); azImporter.Quantity = Convert.ToInt32(az.Value.Quantity); azImporter.ShortDescription = az.Value.ShortDescription; azImporter.Weight = az.Value.Weight; azImporter.HTMLDescription = az.Value.HTMLDescription; azImport.Add(azImporter); bulkSize++; } }
public Task <IActionResult> UpdateAzImports() { //var client = new System.Net.WebClient(); string url = @"http://www.azimporter.com/datafeed/dailydatafeed2.csv"; var path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot", "test.csv"); //client.DownloadFile(url, path); var memory = new MemoryStream(); using (var client = new WebClient()) { var content = client.DownloadData(url); string csvDocument = Encoding.ASCII.GetString(content); var file = new FileInfo(path); using (FileStream fs = file.Create()) { //Add some information to the file. fs.Write(content, 0, csvDocument.Length); } var lines = System.IO.File.ReadAllLines(path, Encoding.UTF8).Select(a => a.Split(',')); List <Wholesaler_AzImporter> items = new List <Wholesaler_AzImporter>(); int skipFirst = 1; foreach (var line in lines) { if (line.Length == 17) { int curr = 1; Wholesaler_AzImporter azImporter = new Wholesaler_AzImporter(); foreach (var col in line) { // skip the first line because it is the title of the csv file if (skipFirst == 1) { break; } // The first item is always the sku if (curr == 1) { azImporter.Sku = col.Replace('"', ' '); } // loop until the first lines contains HTTP // Once it does not have HTTP and it can be converted into a double // we have the wholesale // The next should the quantity and it can be turned into an integer // Add the azImporter to the list if (curr == 17) { items.Add(azImporter); } curr++; } skipFirst++; } } //List<AzImporter> items = new List<AzImporter>(); //int fieldCount = 0; //string[] headers; //using (TextFieldParser parser = new TextFieldParser(@"c:\temp\test.csv")) //{ // parser.TextFieldType = FieldType.Delimited; // parser.SetDelimiters(","); // while (!parser.EndOfData) // { // //Process row // string[] fields = parser.ReadFields(); // foreach (string field in fields) // { // //TODO: Process field // } // } //} //using (CsvReader csv = new CsvReader(file.OpenText())) //{ // fieldCount = csv.FieldCount; // headers = csv.GetFieldHeaders(); // while (csv.ReadNextRecord()) // { // for (int i = 0; i < fieldCount; i++) // Console.Write(string.Format("{0} = {1};", // headers[i], csv[i])); // Console.WriteLine(); // } //} using (var workbook = new XLWorkbook()) { //rowCount = 1; Wholesaler_AzImporter azImporter = new Wholesaler_AzImporter(); //foreach (var line in csv.GetRecords<AzImporter>()) //{ // colCount = 1; // //foreach (var col in line) // //{ // // if (rowCount != 1) // // { // // if(colCount == 1) // // { // // azImporter.Sku = col.Replace('"', ' '); // // } // // else if(colCount == 2) // // { // // azImporter.Category = col.Replace('"', ' '); // // } // // else if (colCount == 3) // // { // // azImporter.ItemName = col.Replace('"', ' '); // // } // // else if (colCount == 4) // // { // // azImporter.Image1 = col.Replace('"', ' '); // // } // // else if (colCount == 5) // // { // // azImporter.Image2 = col.Replace('"', ' '); // // } // // else if (colCount == 6) // // { // // azImporter.Image3 = col.Replace('"', ' '); // // } // // else if (colCount == 7) // // { // // azImporter.Image4 = col.Replace('"', ' '); // // } // // else if (colCount == 8) // // { // // azImporter.Image5 = col.Replace('"', ' '); // // } // // else if (colCount == 9) // // { // // azImporter.Image6 = col.Replace('"', ' '); // // } // // else if (colCount == 10) // // { // // azImporter.Image7 = col.Replace('"', ' '); // // } // // else if (colCount == 11) // // { // // azImporter.Image8 = col.Replace('"', ' '); // // } // // else if (colCount == 12) // // { // // azImporter.MainImage = col.Replace('"', ' '); // // } // // else if (colCount == 13) // // { // // azImporter.WholeSale = Convert.ToDouble(col.Replace('"', ' ')); // // } // // else if (colCount == 14) // // { // // azImporter.Quantity = Convert.ToInt32(col.Replace('"', ' ')); // // } // // else if (colCount == 15) // // { // // azImporter.Weight = Convert.ToInt32(col.Replace('"', ' ')); // // } // // else if (colCount == 16) // // { // // azImporter.HTMLDescription = col.Replace('"', ' '); // // } // // } // // colCount++; // //} // rowCount++; // items.Add(azImporter); //} } } memory.Position = 0; //return new FileContentResult(bytes, MimeMapping.GetMimeMapping(f)); return(null); }