SIGN UP MEMBER LOGIN:    
ARTICLE

Encapsulating the GridView Exporters

Posted by Felipe Ramos Articles | Current Affairs September 16, 2010
The objective of this article is to extend a GridView control exporting functionality and at the same time create an encapsulated library to do so.
Reader Level:
Download Files:
 


Introduction

There are several articles on the web on how to export a GridView or DataGrid into excel. The object of this article is to extend that functionality into a loosely implemented exporting factory. Because we don't want to repeat other material we will add exporting support for Excel, HTML, and PDF. The PDF exporter will take advantage of the iTextSharp library (Version 5.0.4.0).

Interface

Interfaces create a contract with your class, stating that they will implement a particular method or function. For our simple library the Export method will take in two parameters; HttpResponse and GridView. The interface will be important because the exporting classes will be internal and all the implementation will be hidden.

image1.gif

PdfExporter

The PdfExporter will have a reference to the iTextShap library and two fields to hold our Document and PdfWriter. The classes were sealed because we don't want any other class to inherit functionality and reduce the compiler overhead of traversing the class hiarchy looking for sub classes. There are two more code segments that may stand out from the class. The first is that a destructor was implemented.

~PdfExporter()
 {
     if (m_writer != null)
         m_writer.Close();
      …
 }

The other option was to use the IDispose interface, but I am going to trust the GC in this case and way for my class to go out of scope to make sure all the memory was released. The second code change is the use of the Response.OutputStream for our PdfWriter.GetInstance function.

m_writer = PdfWriter.GetInstance(m_doc, response.OutputStream);

This will take care of having to actually writer a byte array or keep track of a MemoryStream.

image2.gif

Excel/HtmlExporters

Some of the issues I had using the HtmlTextWriter for exporting were with the GridView.RenderControls expecting a form tag as the container. To resolve the container issue a wrapper was created to convert the row cells into HtmlTableCells. The entire code to process the GridView is below.

HtmlTableHelper htmlHelper = new HtmlTableHelper();
htmlHelper.AddCells(htmlHelper.GetRow(), HtmlTableHelper.GetList(gridview.HeaderRow.Cells));
htmlHelper.Table.Border = 1;

foreach (GridViewRow row in gridview.Rows)
{
        htmlHelper.AddCells(htmlHelper.GetRow(), HtmlTableHelper.GetList(row.Cells));
}

image3.gif

ExportFactory

The ExportFactory only contains one function called GetExporter with a return type IExport.

public static IExport GetExporter(ExportFormat Format)
        {
            switch (Format)
            {
                case ExportFormat.PDF:
                    return new PdfExporter();
                case ExportFormat.Html:
                    return new HtmlExporter();
                case ExportFormat.Excel:
                    return new ExcelExporter();
                default:
                    throw new Exception(string.Format("Export Forma: {0} not supported.", Format.ToString()));
            }
        }

We are returning the interface because we don't want to expose our implementation classes so the implementing code will look like:

ExportFactory.GetExporter(ExportFormat.PDF).Export(Response, gvProviderSearchResults);

Note

I hope that the different exporters and concepts covered assist in the reader's next project. Please leave comments and suggestions as there are better or more efficient ways to perform a task in programming.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor