This demo is showing how to build the grid model and use it to export the data in various formats (xls, pdf, txt) using external libraries and custom code.
Drag a column header and drop it here to group by that column
Id
Person
Food
Date
Price
Country
Chef
Location
3675
Mary
Shepherd's pie
11/19/2022
18 USD
La Croisette
Pepper Tomato
Diner
3671
Jeremy
Pizza
6/13/2010
79 USD
Regent
Demeter Harvest
Tavern
3667
John
Cheesecake
9/25/2015
100 USD
Winterspring
Josh Baskin
Restaurant
3663
Michael
Hot Beverage
2/13/2012
20 USD
Jisina
Pepper Tomato
Visit
3659
Gabrielle
French toast
5/19/2016
45 USD
Goldshire
Omu Man
Tavern
3655
Lucy
Big Salad
4/3/2019
70 USD
Stormwind
Omu Man
Tavern
3651
Cheyenne
Big Salad
11/1/2017
70 USD
Westfall
Omu Man
Visit
3647
Jonah
Shepherd's pie
4/27/2020
23 USD
Sylvanaar
Joanna Stan
Visit
3643
Fernando
Soup
5/7/2010
50 USD
Ghidrimesti
Chef Chef
University
3639
Audrey
Shepherd's pie
5/17/2011
70 USD
Piccadilly
Charles Duchemin
University
Total
Max: 11/19/2022
Min: 18
Export the content of a grid to excel, pdf and txt file (csv).
To create an excel file we're using the NPOI library.
For exporting to Pdf the iText7 library is used.
To get the entire source code you can download this demo.
if (priceColumn != null) { priceColumn.ClientFormatFunc = (lunch, property) => { var value = lunch.GetType().GetProperty("Price")?.GetValue(lunch).ToString(); if (!string.IsNullOrEmpty(value)) { value += " USD"; }
return value; };
priceColumn.Width = 200; }
return columns; }
[HttpPost] public IActionResult ExportGridToExcel(GridParams g, IEnumerable<ExpColumn> expColumns, bool? allPages) { if (allPages.HasValue && allPages.Value) { g.Paging = false; }
var gridModel = BuildGridModel(g);
var workbook = new GridExcelBuilder(expColumns.Where(o => !o.Hidden)).Build(gridModel);
using (var stream = new MemoryStream()) { workbook.Write(stream); stream.Close(); return File(stream.ToArray(), "application/vnd.ms-excel", "lunches.xls"); } }
[HttpPost] public IActionResult ExportGridToTxt(GridParams g, IEnumerable<ExpColumn> expColumns, bool? allPages) { if (allPages.HasValue && allPages.Value) { g.Paging = false; }
var gridModel = BuildGridModel(g);
var res = new GridTxtBuilder(expColumns.Where(o => !o.Hidden)).Build(gridModel);
using (var memoryStream = new MemoryStream()) { using (var writer = new StreamWriter(memoryStream)) { writer.WriteLine(res); }
[HttpPost] public IActionResult ExportGridToPdf(GridParams g, IEnumerable<ExpColumn> expColumns, bool? allPages) { if (allPages.HasValue && allPages.Value) { g.Paging = false; }
var gridModel = BuildGridModel(g);
var builder = new GridPdfBuilder(addServerFormatting(expColumns.Where(o => !o.Hidden)));
var workStream = new MemoryStream(); var writer = new PdfWriter(workStream); var pdf = new PdfDocument(writer); var document = new Document(pdf);
document.Add(new Paragraph("Hello World!")); document.Add(new Paragraph("Export to pdf example (using iText7)")); document.Add(new Paragraph(new Text("\n")));
var table = builder.Build(gridModel); document.Add(table); writer.SetCloseStream(false); document.Close();
var byteInfo = workStream.ToArray(); workStream.Write(byteInfo, 0, byteInfo.Length); workStream.Position = 0;
/// <summary> /// Demonstrates the simplest way of creating an excel workbook /// it exports all the lunches records, without taking into account any sorting/paging that is done on the client side /// </summary> /// <returns></returns> [HttpPost] public IActionResult ExportAllToExcel() { var workbook = new HSSFWorkbook(); var sheet = workbook.CreateSheet("sheet1");
var res = Db.Lunches.ToArray();
var items = res.Select( o => new { o.Id, o.Person, FoodName = o.Food.Name, o.Location, CountryName = o.Country.Name, ChefName = o.Chef.FullName }).ToList();