public ActionResult ExportToExcel(string jsonGroups) { var pricesdt = new System.Data.DataTable("Prices"); var groups = Newtonsoft.Json.JsonConvert.DeserializeObject <List <int?> >(jsonGroups); var productprices = ProductPrices.GetByGroupIDs(groups); var varientPrices = ProductVarientPrices.GetByGroupIDs(groups); var prices = productprices.Union(varientPrices).Where(item => item.PriceID.HasValue || item.VarientID.HasValue); pricesdt.Columns.Add("ProductID", typeof(string)); pricesdt.Columns.Add("PriceID", typeof(string)); pricesdt.Columns.Add("VarientID", typeof(string)); pricesdt.Columns.Add("PriceCode", typeof(string)); pricesdt.Columns.Add("Title", typeof(string)); pricesdt.Columns.Add("PriceType", typeof(string)); pricesdt.Columns.Add("Price", typeof(string)); pricesdt.Columns.Add("NewPrice", typeof(string)); foreach (var item in prices) { pricesdt.Rows.Add(item.ProductID, item.PriceID, item.VarientID, item.PriceCode, item.Title, item.PriceType, item.Price, ""); } var grid = new GridView(); grid.Font.Name = "tahoma"; grid.Font.Size = new FontUnit(11, UnitType.Pixel); grid.DataSource = pricesdt; grid.DataBind(); Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment; filename=Prices.xls"); Response.ContentType = "application/ms-excel"; Response.Charset = ""; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); grid.RenderControl(htw); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); return(View("MyView")); }
private static void SaveVarients(EditProduct editProduct, int productID) { if (editProduct.Varients.Count == 0) { return; } var curList = ProductVarients.GetByProductID(productID); foreach (var varient in editProduct.Varients) { if (!curList.Any(item => item.ID == varient.ID)) { if (varient.Price <= 0) { continue; } var productVarient = Mapper.Map <ProductVarient>(varient); productVarient.ProductID = productID; productVarient.Title = productVarient.Title; productVarient.PriceCode = productVarient.PriceCode; ProductVarients.Insert(productVarient); foreach (var item in varient.Attributes) { var productVarientAttribute = Mapper.Map <ProductVarientAttribute>(item); productVarientAttribute.ProductVarientID = productVarient.ID; ProductVarientAttributes.Insert(productVarientAttribute); } var varientPrice = new ProductVarientPrice(); varientPrice.ProductVarientID = productVarient.ID; varientPrice.Count = varient.Count; varientPrice.Price = varient.Price * (ExtensionMethods.IsRial ? 1 : 10); varientPrice.PriceType = varient.PriceType; ProductVarientPrices.Insert(varientPrice); } else { curList.Remove(curList.Single(cls => cls.ID == varient.ID)); if (varient.Price <= 0) { continue; } var varientPrice = new ProductVarientPrice(); varientPrice.ProductVarientID = varient.ID; varientPrice.Count = varient.Count; varientPrice.Price = varient.Price * (ExtensionMethods.IsRial ? 1 : 10); varientPrice.PriceType = varient.PriceType; ProductVarientPrices.Insert(varientPrice); } } foreach (var item in curList) { ProductVarients.DeleteWithAttributes(item.ID); } }