public ActionResult ExportExcel(BsGridRepositorySettings <ContributorSearchModel> settings, List <BsGridRowData <int> > items) { var rows = _gridRepository.GetItems(settings, items.Select(x => x.Id).ToList()); try { var builder = new BsGridExcelBuilder <ContributorRowModel>("BForms Contributors.xlsx", rows); builder.ConfigureHeader(header => { header.Order(settings.OrderColumns); header.Style.Font.Bold = true; header.Style.FillColor = BsGridExcelColor.Ivory; header.For(x => x.StartDate) .Text("Contributor since") .Style(style => style.Font.Italic = true); }) .ConfigureRows((row, style) => { if (row.Role == ProjectRole.TeamLeader) { style.Font.Bold = true; } if (row.Role == ProjectRole.Tester) { style.Font.Italic = true; } }) .ConfigureColumns(columns => { columns.For(x => x.Name) .Order(1); columns.For(x => x.Enabled) .Text(x => x.Enabled ? Resource.Yes : Resource.No) .Style((row, style) => style.FillColor = row.Enabled ? BsGridExcelColor.LightGreen : BsGridExcelColor.Red); columns.For(x => x.Role) .Order(2) .Text(x => x.Role.ToString()) .Style(style => style.FillColor = BsGridExcelColor.Lavender); columns.For(x => x.StartDate) .Text(x => x.StartDate.ToMonthNameDate()) .Style(style => style.Font.Italic = true); }); return(new BsExcelResult <ContributorRowModel>("BForms Contributors.xlsx", builder)); } catch (Exception ex) { var controllerName = (string)Request.RequestContext.RouteData.Values["controller"]; var actionName = (string)Request.RequestContext.RouteData.Values["action"]; return(View("Error", new HandleErrorInfo(ex, controllerName, actionName))); } }
/// <summary> /// Execute the Excel Result. /// </summary> /// <param name="context">Controller context.</param> public override void ExecuteResult(ControllerContext context) { if (this.builder == null && this.items == null) { throw new Exception("Excel builder is null or items collection is null"); } if (this.builder == null) { this.builder = new BsGridExcelBuilder <T>(); this.builder.AddSheet <T>(items, this.fileName); } var stream = this.builder.ToStream(); WriteStream(stream, this.fileName); }
public BsExcelResult(string fileName, BsGridExcelBuilder <T> builder) { this.fileName = fileName; this.builder = builder; }
public ActionResult ExportExcel(BsGridRepositorySettings<ContributorSearchModel> settings, List<BsGridRowData<int>> items) { var rows = _gridRepository.GetItems(settings, items.Select(x => x.Id).ToList()); try { var builder = new BsGridExcelBuilder<ContributorRowModel>(); var lightThemeSheet = builder.AddSheet<ContributorRowModel>(rows, "Contributors Light"); lightThemeSheet.ConfigureHeader(header => { header.Order(settings.OrderColumns); header.Style.Font.Bold = true; header.Style.FillColor = BsGridExcelColor.Ivory; header.For(x => x.StartDate) .Text("Contributor since") .Style(style => style.Font.Italic = true); }) .ConfigureRows((row, style) => { if (row.Role.DisplayValue == ProjectRole.TeamLeader) { style.Font.Bold = true; } if (row.Role.DisplayValue == ProjectRole.Tester) { style.Font.Italic = true; } }) .ConfigureColumns(columns => { columns.For(x => x.Name) .Text(x=>x.Name.DisplayValue) .Order(1); columns.For(x => x.Enabled) .Text(x => x.Enabled ? Resource.Yes : Resource.No) .Style((row, style) => style.FillColor = row.Enabled ? BsGridExcelColor.LightGreen : BsGridExcelColor.Red); columns.For(x => x.Role) .Order(2) .Text(x => x.Role.DisplayValue.ToString()) .Style(style => style.FillColor = BsGridExcelColor.Lavender); columns.For(x => x.Contributions) .Text(x => x.Contributions.DisplayValue); columns.For(x => x.StartDate) .Text(x => x.StartDate.ToMonthNameDate()) .Style(style => style.Font.Italic = true); }); var darkThemeSheet = builder.AddSheet<ContributorRowModel>(rows, "Contributors Dark"); darkThemeSheet.ConfigureHeader(header => { header.Order(settings.OrderColumns); header.Style.Font.Bold = true; header.Style.Font.Italic = true; header.Style.FillColor = BsGridExcelColor.DarkTeal; header.For(x => x.StartDate) .Text("Contributor since"); }) .ConfigureColumns(columns => { columns.For(x => x.Name) .Text(x => x.Name.DisplayValue) .Style(style => style.FillColor = BsGridExcelColor.DarkYellow) .Order(1); columns.For(x => x.Enabled) .Text(x => x.Enabled ? Resource.Yes : Resource.No) .Style((row, style) => style.FillColor = row.Enabled ? BsGridExcelColor.DarkGreen : BsGridExcelColor.DarkRed); columns.For(x => x.Role) .Order(2) .Text(x => x.Role.DisplayValue.ToString()); columns.For(x => x.Contributions) .Text(x => x.Contributions.DisplayValue); columns.For(x => x.StartDate) .Text(x => x.StartDate.ToMonthNameDate()) .Style(style => style.Font.Italic = true); }); return new BsExcelResult<ContributorRowModel>("BForms Contributors.xlsx", builder); } catch (Exception ex) { var controllerName = (string)Request.RequestContext.RouteData.Values["controller"]; var actionName = (string)Request.RequestContext.RouteData.Values["action"]; return View("Error", new HandleErrorInfo(ex, controllerName, actionName)); } }