private void SaveSkuGenerateHistory(Dictionary <string, ulong> index) { var existingHistory = GetSkuIndicesFromHistory(); foreach (KeyValuePair <string, ulong> keyValuePair in index) { SkuIndex newIndex = new SkuIndex() { Sku = keyValuePair.Key, EndIndex = keyValuePair.Value, CreateDateTime = DateTime.Now }; existingHistory.Add(newIndex); } File.WriteAllText(indexFilename, JsonConvert.SerializeObject(existingHistory)); }
private void btnBatchGenerate_Click(object sender, EventArgs e) { try { List <OrderInfo> orderItems = new List <OrderInfo>(); ISheet sheet; using (var stream = new FileStream(txtOrderFileName.Text, FileMode.Open)) { stream.Position = 0; XSSFWorkbook xssWorkbook = new XSSFWorkbook(stream); sheet = xssWorkbook.GetSheetAt(0); IRow headerRow = sheet.GetRow(0); int cellCount = headerRow.LastCellNum; /*for (int j = 0; j < cellCount; j++) * { * ICell cell = headerRow.GetCell(j); * if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) continue; * { * dtTable.Columns.Add(cell.ToString()); * } * }*/ for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); if (row == null) { continue; } if (row.Cells.All(d => d.CellType == CellType.Blank)) { continue; } OrderInfo item = new OrderInfo(); item.GO = row.GetCell(0).ToString(); item.ProductCode = row.GetCell(1).ToString(); item.Sku = row.GetCell(2).ToString(); item.Style = row.GetCell(7).ToString(); //H item.Size = row.GetCell(9).ToString(); //J item.Color = row.GetCell(11).ToString(); //L item.Qty = Convert.ToInt32(row.GetCell(21).ToString()); //L orderItems.Add(item); } List <SkuIndex> skuIndices = GetSkuIndicesFromHistory(); Dictionary <string, ulong> Index = skuIndices.GroupBy(c => c.Sku).Select(g => new { Sku = g.Key, Index = g.Max(row => row.EndIndex) + 1 }).ToDictionary(x => x.Sku, x => x.Index); StringBuilder sb = new StringBuilder(); string header = "GO,Product Code,SKU,Style,Size,Color,Human Readable,Unique ID, Url, EPC"; sb.AppendLine(header); foreach (OrderInfo orderItem in orderItems) { ulong startIndex = 1; // if(skuIndices.Any(c=>c.Sku==orderItem.Sku)) if (Index.ContainsKey(orderItem.Sku)) { startIndex = Index[orderItem.Sku]; } uint orderItemQty = (uint)orderItem.Qty; string csv = GenerateSampleDataV2(orderItem, orderItem.Sku, startIndex, orderItemQty); sb.Append(csv); ulong itemQty = startIndex + orderItemQty - 1; if (Index.ContainsKey(orderItem.Sku)) { Index[orderItem.Sku] = itemQty; } else { Index.Add(orderItem.Sku, itemQty); } } DialogResult dialogResult = saveFileDialog1.ShowDialog(); if (dialogResult == DialogResult.OK) { string csvFilename = saveFileDialog1.FileName; using (var sw = File.CreateText(csvFilename)) { sw.WriteLine(sb.ToString()); sw.Close(); } MessageBox.Show("Data is generated successfully!"); } //Save back to index file foreach (KeyValuePair <string, ulong> keyValuePair in Index) { SkuIndex newSkuIndex = new SkuIndex() { CreateDateTime = DateTime.Now, EndIndex = keyValuePair.Value, Sku = keyValuePair.Key }; skuIndices.Add(newSkuIndex); } SaveSkuGenerateHistory(Index); //Process data int a = 1; } } catch (Exception exception) { MessageBox.Show(exception.Message); } }