protected void btnUploadFile_Click(object sender, EventArgs e) { if (fupImportFile.HasFile) { ProductCsvImporter productCsvImporter = new ProductCsvImporter(StoreContext.CurrentStore.Id.Value); ProductCsvImportResult result = productCsvImporter.ImportProducts(fupImportFile.FileContent); StringBuilder html = new StringBuilder(); if (result.Messages.Count > 0) { html.Append(result.Messages.ToDelimitedString("<br />")); } foreach (var status in WA.Enum <ProductImportStatus> .GetValues()) { var lines = result.CsvLines.FindAll(x => x.Status == status); if (lines.Count > 0) { html.AppendFormat(@"<h3>[ {0:N0} ] <a href=""#"" onclick=""jQuery('#lines{1}').toggle(); return false;"">{1}</a></h3>", lines.Count, status); html.AppendFormat(@"<div id=""lines{0}"" style=""display: none;"">", status); html.Append(@"<table class=""grid gridLight""> <thead> <tr> <th style=""text-align: right; width: 82px;"">CSV Line #</th> <th>Name</th> <th>Sku</th> <th>UrlName</th> <th>Import Notes</th> </tr> </thead> <tbody>"); foreach (var line in lines) { html.AppendFormat(@"<tr> <td style=""text-align: right;"">{0}</td> <td>{2}</td> <td>{3}</td> <td>{4}</td> <td>{1}</td> </tr>", line.CsvLineNumber, line.StatusMsg, line.ProductName, line.ProductSku, line.ProductUrlName); } html.Append(@"</tbody> </table>"); html.Append("</div>"); } } ShowFlash(html.ToString()); } }
protected void btnDownloadCsv_Click(object sender, EventArgs e) { var productList = DataModel.ProductCollection.GetAll(StoreContext.CurrentStore.Id.Value); productList.Sort((left, right) => left.Id.Value.CompareTo(right.Id.Value)); List <CsvProductInfo> csvProducts = new List <CsvProductInfo>(); foreach (Product p in productList) { var csv = new CsvProductInfo() { ImportAction = "", ProductId = p.Id.Value, Name = p.Name, Sku = p.Sku, UrlName = p.Slug, Price = p.Price, Weight = p.Weight, SeoTitle = p.SeoTitle, SeoDescription = p.SeoDescription, SeoKeywords = p.SeoKeywords, IsActive = p.IsActive, CategoryNames = p.GetCategories(true).ConvertAll(c => c.Name).ToDelimitedString(", "), PhotoFilenames = p.GetAllPhotosInSortOrder().ConvertAll(x => x.Filename).ToDelimitedString(", "), DigitalFilename = p.DigitalFilename, TaxableItem = p.IsTaxable, ShowPrice = p.IsPriceDisplayed, AvailableForPurchase = p.IsAvailableForPurchase, EnableInventoryManagement = p.InventoryIsEnabled, StockLevel = p.InventoryQtyInStock, AllowNegativeStock = p.InventoryAllowNegativeStockLevel }; var descriptors = p.GetProductDescriptors(); if (descriptors.Count >= 1) { csv.Desc1Name = descriptors[0].Name; csv.Desc1Html = descriptors[0].TextHtmlDecoded; } if (descriptors.Count >= 2) { csv.Desc2Name = descriptors[1].Name; csv.Desc2Html = descriptors[1].TextHtmlDecoded; } if (descriptors.Count >= 3) { csv.Desc3Name = descriptors[2].Name; csv.Desc3Html = descriptors[2].TextHtmlDecoded; } if (descriptors.Count >= 4) { csv.Desc4Name = descriptors[3].Name; csv.Desc4Html = descriptors[3].TextHtmlDecoded; } if (descriptors.Count >= 5) { csv.Desc5Name = descriptors[4].Name; csv.Desc5Html = descriptors[4].TextHtmlDecoded; } csvProducts.Add(csv); } Response.Clear(); Response.ClearHeaders(); Response.ContentType = "text/csv"; Response.AddHeader("Content-Disposition", "attachment; filename=Product-Export.csv"); var exporter = new ProductCsvImporter(StoreContext.CurrentStore.Id.Value); exporter.ExportProducts(csvProducts, Response.OutputStream); Response.Flush(); Response.End(); }