protected void btnExecute_Click(object sender, EventArgs e) { //Define workbook to store null initially Workbook workbook = null; string param = "SalesByCategory"; string path = MapPath("."); path = path.Substring(0, path.LastIndexOf("\\")); SalesByCategory salesByCategory = new SalesByCategory(path); //Create the workbook based on the custom method of a class workbook = salesByCategory.CreateSalesByCategory(); //Save the excel file workbook.Save(HttpContext.Current.Response, param + ".xls", ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003)); // note by Vit - end response to avoid unneeded html after xls Response.End(); if (ddlFileVersion.SelectedItem.Value == "XLS") { ////Save file and send to client browser using selected format workbook.Save(HttpContext.Current.Response, "CustomerLabels.xls", ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003)); } else { workbook.Save(HttpContext.Current.Response, "CustomerLabels.xlsx", ContentDisposition.Attachment, new OoxmlSaveOptions(SaveFormat.Xlsx)); } //end response to avoid unneeded html HttpContext.Current.Response.End(); }
//-------------------------------------------------------------------------- #region ** implementation // when the current quarter changes, update filters, total sales, and goals void _quarters_CurrentChanged(object sender, EventArgs e) { // update filters SalesByCustomer.Refresh(); SalesByCategory.Refresh(); // calculate sales for this quarter and for the previous quarter var qtrList = _quarters.Source as IList; var qtrThis = Quarters.CurrentItem as string; var qtrPrev = Quarters.CurrentPosition < qtrList.Count - 1 ? qtrList[Quarters.CurrentPosition + 1] as string : string.Empty; _quarterSales = 0; decimal _prevQuarterSales = 0; foreach (Order o in _orders) { if (o.Quarter == qtrThis) { _quarterSales += o.Amount; } else if (o.Quarter == qtrPrev) { _prevQuarterSales += o.Amount; } } // compute sales goals (2% gold, 15% platinum) _goalGold = _prevQuarterSales * (decimal)1.02; _goalPlat = _prevQuarterSales * (decimal)1.15; // raise PropertyChanged event to update bindings OnPropertyChanged(); }
// create SalesByCategory collection (filtered by Quarter) void CreateSalesByCategory() { var lsc = new List <SalesByCategory>(); // group by quarter var qByQtr = from o in _orderDetails group o by o.Order.Quarter into g select new { Quarter = g.Key, OrderDetails = g }; foreach (var oq in qByQtr) { // group by category var qByCust = from o in oq.OrderDetails group o by o.Product.Category into g select new { Category = g.Key, Quarter = oq.Quarter, OrderDetails = g }; foreach (var oc in qByCust) { var sbc = new SalesByCategory(); sbc.Category = oc.Category; sbc.Quarter = oc.Quarter; sbc.Amount = (from o in oc.OrderDetails select o.Amount).Sum(); lsc.Add(sbc); } } // create ICollectionView _salesByCategory = new CollectionViewSource(); _salesByCategory.Source = lsc; // sort by category name var sd = new SortDescription("Category.CategoryName", ListSortDirection.Ascending); SalesByCategory.SortDescriptions.Add(sd); // filter by current Quarter SalesByCategory.Filter = (item) => { var sbc = item as SalesByCategory; return(string.Equals(sbc.Quarter, (string)Quarters.CurrentItem)); }; }