This example shows how to use the XtraPrinting library to export several MVC GridView Extensions into a single XLS document.
You can export several grids into a single document in the following way:
-
Create a PrintableComponentLinkBase object for every grid. Call the GridViewExtension.CreatePrintableObject method to create an IPrintable object from the GridView based on its GridViewSettings.
GridViewSettings categoriesGridSettings = new GridViewSettings(); categoriesGridSettings.Name = "gvCategories"; categoriesGridSettings.KeyFieldName = "CategoryID"; categoriesGridSettings.Columns.Add("CategoryID"); categoriesGridSettings.Columns.Add("CategoryName"); categoriesGridSettings.Columns.Add("Description"); var link1 = new PrintableComponentLinkBase(ps); link1.Component = GridViewExtension.CreatePrintableObject(categoriesGridSettings, MyModel.GetCategories());
-
Call the CreateDocument method to create a document from the link, so it can be displayed or printed.
var compositeLink = new CompositeLinkBase(ps); compositeLink.Links.AddRange(new object[] { link1, link2 }); compositeLink.CreateDocument();
-
Call the PrintingSystemBase.ExportToXls method to export the document.
MemoryStream stream = new MemoryStream(); link.PrintingSystemBase.ExportToXls(stream); stream.Position = 0; FileStreamResult result = new FileStreamResult(stream, "application/xls"); result.FileDownloadName = "MyData.xls";