public ActionResult ExportStats(Schools school, int year) { var stats = new ECAStatistic { school = school, year = year }; stats.CalculateStats(repository); var ms = new MemoryStream(); using (var fs = new FileStream( AppDomain.CurrentDomain.BaseDirectory + "/Content/templates/NPOITemplate.xls", FileMode.Open, FileAccess.Read, FileShare.Read)) { HSSFWorkbook 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++); // show general stats first var namecell = row.CreateCell(0); namecell.SetCellValue("Name"); namecell.CellStyle = boldStyle; var malecell = row.CreateCell(1); malecell.SetCellValue("Male"); malecell.CellStyle = boldStyle; var femalecell = row.CreateCell(2); femalecell.SetCellValue("Female"); femalecell.CellStyle = boldStyle; foreach (var entry in stats.entries.OrderBy(x => x.name)) { row = sheet.CreateRow(rowcount++); row.CreateCell(0).SetCellValue(entry.name); row.CreateCell(1).SetCellValue(entry.male); row.CreateCell(2).SetCellValue(entry.female); } // resize sheet.AutoSizeColumn(0); sheet.AutoSizeColumn(1); sheet.AutoSizeColumn(2); // delete first sheet templateWorkbook.RemoveSheetAt(0); templateWorkbook.Write(ms); } // return created file path); return(File(ms.ToArray(), "application/vnd.ms-excel", string.Format("Statistics_ECA_{0}_{1}.xls", school, year))); }
public ActionResult StatisticContent(Schools id, int year) { var stats = new ECAStatistic { school = id, year = year }; stats.CalculateStats(repository); return(View(stats)); }