CodeBits: Export ICollection to Excel compatable format

By Tommy at June 16, 2009 18:00
Filed Under: CodeBits

I've expanded the generic collection handler to include allowing export to excel without the need for the gridview hack.

 

Little background: The gridview hack is to save a collection to the page first in a gridview. From there it is pretty easy to post the data back to the page as a cvs file. However that requires the gridview to be loaded first with all the data and a second post to occur. It is also not user friendly to display the gridview on a page if it has more then 15 columns or so. This function will help avoid that. Just add it to any class that inherits the CollectionBase class.

 

Side Note: A full postback must occur. So if you're going to do the export from a button within an AJAX update panel, make sure the panel has the button set for a full postback.

      
    public void ExportToXls(string fileName)
    {
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.Clear();

        // Force as inline, otherwise browsers will think this is an .aspx file or not give it a filename
        HttpContext.Current.Response.AddHeader("content-disposition", "inline; filename=" + fileName);
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        HttpContext.Current.Response.Charset = string.Empty;

        HttpContext.Current.Response.Flush();

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                Table table = new Table();
                table.GridLines = GridLines.Both;

                TableRow header = new TableRow();

                // Use refection to give us a header row with the property names
                foreach (PropertyInfo propertyInfo in this[0].GetType().GetProperties())
                {
                    TableCell cell = new TableCell();
                    cell.Text = propertyInfo.Name;
                    header.Cells.Add(cell);                       
                }

                // Add some style to the header
                header.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#008080");
                header.Style.Add(HtmlTextWriterStyle.Color, "#ffffff");
                header.Style.Add(HtmlTextWriterStyle.TextAlign, "center");

                table.Rows.Add(header);

                for (int i = 0; i < this.Count; i++)
                {
                    TableRow dataRow = new TableRow();

                    foreach (PropertyInfo propertyInfo in this[i].GetType().GetProperties())
                    {
                        TableCell cell = new TableCell();

                        // Error check incase the property is null
                        if (propertyInfo.GetValue(this[i], null) == null)
                        {
                            cell.Text = string.Empty;
                        }
                        else
                        {
                            cell.Text = propertyInfo.GetValue(this[i], null).ToString();
                        }

                        dataRow.Cells.Add(cell);
                       
                    }

                    table.Rows.Add(dataRow);
                }

                table.RenderControl(htw);
                HttpContext.Current.Response.Write(sw.ToString());
            }
        }

        HttpContext.Current.Response.End();
    }  

DataCollectionV2.cs

Comments are closed