Example #1
0
        public ActionResult Statistics(int to_day, int to_month, int to_year, Schools school,
                                       int from_day, int from_month, int from_year)
        {
            var from = new DateTime(from_year, from_month, from_day);
            var to   = new DateTime(to_year, to_month, to_day);

            // get matching entries
            var viewmodel = new ConductStatistics
            {
                from   = from.ToShortDateString(),
                to     = to.ToShortDateString(),
                school = school.ToString()
            };

            viewmodel.PopulateStats(from, to, school);

            return(View(viewmodel));
        }
Example #2
0
        public ActionResult ExportStats(string from, string to, Schools school)
        {
            var start = DateTime.Parse(from);
            var end   = DateTime.Parse(to);

            var stats = new ConductStatistics();

            stats.PopulateStats(start, end, school);

            var ms = new MemoryStream();

            using (var fs =
                       new FileStream(
                           AppDomain.CurrentDomain.BaseDirectory + "/Content/templates/NPOITemplate.xls",
                           FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                var templateWorkbook = new HSSFWorkbook(fs, true);
                var sheet            = templateWorkbook.CreateSheet(school.ToString());

                // create fonts
                var boldStyle = templateWorkbook.CreateCellStyle();
                var boldFont  = templateWorkbook.CreateFont();
                boldFont.Boldweight = (short)FontBoldWeight.BOLD;
                boldStyle.SetFont(boldFont);

                var rowcount = 0;
                var row      = sheet.CreateRow(rowcount++);

                row.CreateCell(0).SetCellValue("Behaviour Type");
                row.GetCell(0).CellStyle = boldStyle;
                row.CreateCell(1).SetCellValue("Students");
                row.GetCell(1).CellStyle = boldStyle;
                row.CreateCell(2).SetCellValue("Incidents");
                row.GetCell(2).CellStyle = boldStyle;

                foreach (var stat in stats.demerits.OrderByDescending(x => x.totalIncidents))
                {
                    row = sheet.CreateRow(rowcount++);
                    row.CreateCell(0).SetCellValue(stat.name);
                    row.CreateCell(1).SetCellValue(stat.totalStudents);
                    row.CreateCell(2).SetCellValue(stat.totalIncidents);
                }

                foreach (var stat in stats.merits.OrderByDescending(x => x.totalIncidents))
                {
                    row = sheet.CreateRow(rowcount++);
                    row.CreateCell(0).SetCellValue(stat.name);
                    row.CreateCell(1).SetCellValue(stat.totalStudents);
                    row.CreateCell(2).SetCellValue(stat.totalIncidents);
                }

                // adjust column
                sheet.AutoSizeColumn(0);

                // delete first sheet
                templateWorkbook.RemoveSheetAt(0);
                templateWorkbook.Write(ms);
            }

            // return created file path);
            return(File(ms.ToArray(), "application/vnd.ms-excel", string.Format("Conduct_{0}_{1}.xls", from.Replace("/", ""), to.Replace("/", ""))));
        }