Now let me show you a brief example as to how you can use the DPL library to export our RadGrid control.
As you can see below the control declaration is very simple in this case:
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand" AllowPaging="true"> <ExportSettings IgnorePaging="true" /> <MasterTableView CommandItemDisplay="Top"> <CommandItemSettings ShowExportToExcelButton="true" ShowExportToWordButton="true" /> </MasterTableView></telerik:RadGrid>static GridItemType[] supportedItemTypes = new GridItemType[] { GridItemType.Header, GridItemType.AlternatingItem, GridItemType.Item };protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e){ if (e.CommandName == RadGrid.ExportToExcelCommandName) { this.PreRender += (s, a) => { ExportToXlsx(); }; e.Canceled = true; }}private void ExportToXlsx() { string fileName = "RadGrid.xlsx"; string filePath = Server.MapPath(fileName); IgnorePaging(); //more info about this method later Workbook workbook = new Workbook(); Worksheet worksheet = workbook.Worksheets.Add(); GridItem item; var gridColumns = RadGrid1.MasterTableView.RenderColumns; var gridItems = RadGrid1.MasterTableView.GetItems(supportedItemTypes); for (int itemNum = 0; itemNum < gridItems.Length; itemNum++) { item = gridItems[itemNum]; for (int cellNum = 0; cellNum < item.Cells.Count; cellNum++) { worksheet.Cells[itemNum, cellNum].SetValue(GetCellText(item.Cells[cellNum].Text)); } } GenerateFile(workbook, fileName); }for (int colNum = 0; colNum < gridColumns.Length; colNum++){ if (!gridColumns[colNum].Visible) { worksheet.Columns[colNum].SetWidth(new ColumnWidth(0, false)); }}private string GetCellText(string text){ return text.Replace(" ", "");}private void IgnorePaging(){ if (RadGrid1.ExportSettings.IgnorePaging) { RadGrid1.AllowPaging = false; RadGrid1.Rebind(); }}private void GenerateFile(object structure, string fileName){ Workbook workbook = structure as Workbook; byte[] output; using (MemoryStream ms = new MemoryStream()) { if (workbook != null) { XlsxFormatProvider provider = new XlsxFormatProvider(); provider.Export(workbook, ms); } ms.Position = 0; output = ms.ToArray(); } Response.ContentType = "application/excel"; Response.AddHeader("content-disposition", "attachment; filename=" + fileName); Response.BinaryWrite(output); Response.Flush(); Response.Close();}
For Q3 2014 we will finish the full-blown integration of the DPL library with RadGrid, RadEditor, RadTreeList, and RadPivotGrid. You will be able to export any of these controls with a simple button click. Of course, the DPL is here to stay so it will still be available as a standalone component even when we finish the integration process, so if you prefer a fully custom approach you will still have the right tools at hand.
As always, I would like to hear your opinion on this matter. Feel free to leave your feedback in the comment section below.
Daniel Peichev is Software Developer at one of Telerik's ASP.NET AJAX teams, where he primarily works on exporting functionality for RadGrid and RadTreeList. Daniel is interested in ASP.NET, Javascript, CSS and the cutting edge Microsoft technologies and products.