Exporting a WebDataGrid to Word, Excel, XPS or PDF

Following the recent posts about exporting data from a grid , this time I will show you how to do the same with the Infragistics ASP.NET controls. The target is a WebDataGrid and the goal – saving the data locally into today’s most widely used document formats: Microsoft Word/Excel files and PDF documents. As you shall see deeper into this post , all of this is done in an extremely simple way.

Let’s get on with it !

Set it up

To begin working with the appropriate controls , you’ll first need to reference the Infragistics Documents libraries – here are the ones I used.

Infragistics DLL Web ASP Document

You can find the DLLs by going to the folder where you installed your Infragistics products. Navigate to ASP.NET/CLR/  and pick the appropriate libraries. Once you’ve added them to your project , your References should look something like the ones in the picture on the left.







Now , in order to use the custom controls , add a ScriptManager control to your web page. It should be located in the AJAX Extensions section of your Visual Studio toolbox. With that out of the way , you’re good to now add the Infragistics controls. Go ahead and expand the IG tab in your toolbox – drag a WebDataGrid to begin with. If for some reason the control is not in the tab , you can manually add it:

Infragistics Document Export ASP Visual Studio Toolbox


While in the Choose Toolbox Items window , now’s also the time to add the specific document controls. In my example , I’ve added WebDocumentExporter , WebExcelExporter and WebWordExporter. With these added to your toolbox , you can easily drag each one onto your page.

When you’re done with adding the export controls you require , it’s time to see how to actually use them. The sample I’ve built is made to be easy to understand and adapt to your own needs – here we go.





Using the controls

Now when you’ve done all the dragging around , your .aspx file should look something like this:


   1: <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
   2:     CodeBehind="Default.aspx.cs" Inherits="DocumentGeneration._Default" %>
   4: <%@ Register Assembly="Infragistics4.Web.v11.2, Version=11.2.20112.2055, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb"
   5:     Namespace="Infragistics.Web.UI.GridControls" TagPrefix="ig" %>
   7: <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
   8: </asp:Content>
   9: <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
  10:     <asp:ScriptManager ID="ScriptManager1" runat="server" />
  11:     <ig:WebDataGrid ID="dataGrid" runat="server" Height="350px" Width="400px" />
  12:         <asp:Label ID="Label1" runat="server" Text="Format"></asp:Label>
  13:         <asp:RadioButtonList ID="formats" runat="server">
  14:             <asp:ListItem Text="PDF" Value="PDF" Selected="True" />
  15:             <asp:ListItem Text="XPS" Value="XPS"/>
  16:             <asp:ListItem Text="Word" Value="Word"/>
  17:             <asp:ListItem Text="Excel" Value="Excel"/>
  18:         </asp:RadioButtonList>
  19:     <asp:Button ID="exportButton" runat="server" Text="Export"/>
  21:     <ig:WebDocumentExporter ID="documentExporter" runat="server" />
  22:     <ig:WebExcelExporter ID="excelExporter" runat="server" />
  23:     <ig:WebWordExporter ID="wordExporter" runat="server" />
  24: </asp:Content>

I’ve added a list of radio buttons to select which format to export to , as well as a button to trigger the export method call. Now let’s take a look at the Code-Behind file , where all the “magic” happens.


   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using System.Web.UI;
   6: using System.Web.UI.WebControls;
   7: using Infragistics.Web.UI.GridControls;
   9: namespace DocumentGeneration {
  10:     public partial class _Default : System.Web.UI.Page {
  12:         protected override void OnInit(EventArgs e) {
  13:             base.OnInit(e);
  14:             this.exportButton.Click += new EventHandler(exportButton_Click);
  15:         }
  17:         protected void Page_Load(object sender, EventArgs e) {
  18:             List<SimpleItem> data = new List<SimpleItem>();
  19:             for (int i = 1; i <= 30; i++) {
  20:                 data.Add(new SimpleItem { Text1 = "Item " + i, Text2 = "Number " + i });
  21:             }
  22:             this.dataGrid.DataSource = data;
  23:             this.dataGrid.DataBind();
  24:         }
  26:         void exportButton_Click(object sender, EventArgs e) {
  27:             String fileName = "Export1";
  28:             switch (formats.SelectedValue) {
  29:                 case "PDF":
  30:                     this.documentExporter.Format = FileFormat.PDF;
  31:                     this.documentExporter.DownloadName = fileName;
  32:                     this.documentExporter.Export(this.dataGrid); break;
  33:                 case "XPS":
  34:                     this.documentExporter.Format = FileFormat.XPS;
  35:                     this.documentExporter.DownloadName = fileName;
  36:                     this.documentExporter.Export(this.dataGrid); break;
  37:                 case "Word":
  38:                     this.wordExporter.DownloadName = fileName;
  39:                     this.wordExporter.Export(this.dataGrid); break;
  40:                 case "Excel":
  41:                     this.excelExporter.DownloadName = fileName;
  42:                     this.excelExporter.WorkbookFormat = Infragistics.Documents.Excel.WorkbookFormat.Excel97To2003;
  43:                     this.excelExporter.Export(this.dataGrid); break;
  44:             }
  45:         }
  47:     }
  48: }

Lines 17 to 24 create a data source for our grid – a list containing some basic objects. In event handler for the button , you see the actual exporting procedures. Yes , that’s right – in 15 lines of code you convert an entire grid into 4 different formats ! The setup above has the most vital requirements for an export – you can tweak a lot more options: check the APIs for WebWordExporter , WebDocumentExporter and WebExcelExporter.


If you’re looking for more insight on exporting matters , you can read both the first and the second part of Damyan’s posts about exporting into PDF or XPS in a MVC project. You could also see my other post about exporting to an Excel workbook , using the MVC wrappers for the Infragistics jQuery ExcelExporter.

You can also download a sample project by clicking on this link.

Comments  (1 )

on Thu, Mar 22 2012 10:24 PM

i need